Subversion Repositories basico

Rev

Rev 4 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4 Rev 74
Line 3... Line 3...
3
# File: sap.py
3
# File: sap.py
4
# Author: Tomás Vírseda
4
# Author: Tomás Vírseda
5
# License: GPL v3
5
# License: GPL v3
6
# Description: SAP service
6
# Description: SAP service
7
7
8
import sys
-
 
9
import time
8
import time
10
import json
-
 
11
import traceback
9
import traceback
12
from shutil import which
10
from shutil import which
13
from cgi import escape
-
 
14
import selenium
-
 
15
from selenium import webdriver
-
 
16
from selenium.webdriver.support.wait import WebDriverWait
-
 
17
from selenium import webdriver
-
 
18
from selenium.webdriver.common.keys import Keys
-
 
19
#~ from selenium.webdriver.support.ui import WebDriverWait
-
 
20
from selenium.common.exceptions import WebDriverException
-
 
21
from selenium.common.exceptions import NoSuchElementException
-
 
22
import feedparser
-
 
23
-
 
24
11
25
from .service import Service
12
from .service import Service
26
13
27
# Default settings for SAP module
-
 
28
LOGIN_PAGE_URL = "https://accounts.sap.com"
-
 
29
LOGOUT_PAGE_URL = "https://accounts.sap.com/ui/logout"
-
 
30
ODATA_NOTE_URL = "https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='%s',Version='0',Language='E')" #$expand=LongText" #?$expand=LongText,RefTo,RefBy"
-
 
-
 
14
#~ ODATA_NOTE_URL = "https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='%s',Version='0',Language='E')" #$expand=LongText" #?$expand=LongText,RefTo,RefBy"
-
 
15
ODATA_NOTE_URL = "https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='%s',Version='0',Language='en')"
-
 
16
#~ ODATA_NOTE_URL = "https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='%s',Version='0',Language='E')?$expand=LongText,Languages,RefTo,RefBy"
-
 
17
#~ ODATA_NOTE_URL = "https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='%s',Version='0',Language='E')?$expand=LongText,Languages,RefTo,RefBy"
31
SAP_NOTE_URL = "https://launchpad.support.sap.com/#/notes/%s"
18
SAP_NOTE_URL = "https://launchpad.support.sap.com/#/notes/%s"
32
TIMEOUT = 5
-
 
-
 
19
SAP_NOTE_URL_PDF = "https://launchpad.support.sap.com/services/pdf/notes/%s/E"
33
20
34
21
35
class SAP(Service):
22
class SAP(Service):
36
    def initialize(self):
23
    def initialize(self):
37
        '''
24
        '''
38
        Setup AppLogic Service
25
        Setup AppLogic Service
39
        '''
26
        '''
40
        self.windriver = None
-
 
41
        self.sapnotes = {}
-
 
42
        self.stats = {}
-
 
43
        self.stats['maincomp'] = {}
-
 
44
        self.stats['cats'] = {}
-
 
45
        self.stats['component'] = set()
-
 
46
        self.stats['category'] = set()
-
 
47
        self.stats['priority'] = set()
-
 
48
        self.stats['type'] = set()
-
 
49
        self.stats['version'] = set()
-
 
50
        #~ self.stats['releaseon'] = set()
-
 
51
        self.__init_config_section()
27
        self.__init_config_section()
52
28
53
29
54
    def __init_config_section(self):
30
    def __init_config_section(self):
-
 
31
        prefs = self.get_service('Settings')
55
        self.config = self.app.get_config()
32
        self.config = self.app.get_config()
56
        if self.config.has_section(self.section):
33
        if self.config.has_section(self.section):
57
            options = self.config.options(self.section)
34
            options = self.config.options(self.section)
58
            if len(options) == 0:
35
            if len(options) == 0:
59
                self.log.debug("Section %s empty. Initializing with default values" % self.section)
36
                self.log.debug("Section %s empty. Initializing with default values" % self.section)
60
                settings = self.get_default_settings()
-
 
-
 
37
                settings = prefs.get_default_settings()
61
                self.config[self.section] = settings
38
                self.config[self.section] = settings
-
 
39
                self.log.debug("Default parameters loaded:")
-
 
40
                for key in settings:
-
 
41
                    self.log.debug("\tKey: %s - Value: %s" % (key, settings[key]))
62
                self.save_config()
42
                self.save_config()
63
43
64
44
65
    def ajax_complete(self, driver):
-
 
-
 
45
    def analyze_sapnote_metadata(self, sid, content):
-
 
46
        '''
-
 
47
        Get metadata details from SAP Note
-
 
48
        '''
-
 
49
        #~ self.log.debug("\t%s" % content)
66
        try:
50
        try:
67
            return 0 == driver.execute_script("return jQuery.active")
-
 
68
        except WebDriverException:
-
 
69
            pass
-
 
70
-
 
71
-
 
72
    def wait_for_page_load(self, driver):
-
 
73
        driver.implicitly_wait(0.5)
-
 
74
        try:
-
 
75
            WebDriverWait(driver, 30).until(lambda d: (d.execute_script('return document.readyState') == 'complete'))
-
 
76
            #~ WebDriverWait(driver, 30).until(lambda d: (d.execute_script('return window.performance.timing.loadEventEnd') > 0))
-
 
-
 
51
            utils = self.get_service('Utils')
-
 
52
            f = utils.feedparser_parse(content)
-
 
53
            sid = f.entries[0].d_sapnotesnumber
-
 
54
            sapnote = {}
-
 
55
            sapnote['id'] = sid
-
 
56
            sapnote['componentkey'] = f.entries[0].d_componentkey
-
 
57
            comptxt = f.entries[0].d_componenttext
-
 
58
            if comptxt == "Please use note 1433157 for finding the right component":
-
 
59
                comptxt = ""
-
 
60
            sapnote['componenttxt'] = comptxt
-
 
61
            sapnote['category'] = f.entries[0].d_category_detail['value']
-
 
62
            sapnote['language'] = f.entries[0].d_languagetext_detail['value']
-
 
63
            sapnote['title'] = f.entries[0].d_title_detail['value']
-
 
64
            sapnote['priority'] = f.entries[0].d_priority_detail['value']
-
 
65
            sapnote['releaseon'] = f.entries[0].d_releasedon
-
 
66
            sapnote['type'] = f.entries[0].d_type_detail['value']
-
 
67
            sapnote['version'] = f.entries[0].d_version_detail['value']
-
 
68
            sapnote['feedupdate'] = f.entries[0].updated
-
 
69
            sapnote['bookmark'] = False
-
 
70
            self.log.info ("\tSAP Note %s analyzed successfully" % sid)
77
        except Exception as error:
71
        except Exception as error:
78
            #~ self.log.warning(self.get_traceback())
-
 
79
            self.log.warning(error)
-
 
80
            time.sleep(5)
-
 
81
-
 
82
-
 
83
    def check_webdriver(self):
-
 
84
        found = False
-
 
85
        driver = self.get_webdriver()
-
 
86
        if driver:
-
 
87
            driver.quit()
-
 
88
            found = True
-
 
89
-
 
90
        return found
-
 
-
 
72
            sapnote = {}
-
 
73
            self.log.error("\tError while analyzing data for SAP Note %s" % sid)
-
 
74
            #~ self.log.error("\t%s" % error)
91
75
-
 
76
        return sapnote
92
77
93
    def build_stats(self, bag=None):
-
 
94
        if bag is None:
-
 
95
            bag = self.sapnotes
-
 
96
        self.dstats = {}
-
 
97
        self.compstats = {}
-
 
98
        alltasks = set()
-
 
99
78
100
        for sid in bag:
-
 
101
            # tasks
-
 
102
            try:
-
 
103
                tasks = self.sapnotes[sid]['tasks']
-
 
104
                for task in tasks:
-
 
105
                    alltasks.add(task)
-
 
106
            except: pass
-
 
107
            self.tasks = self.app.get_service('Tasks')
-
 
108
            self.tasks.save_tasks_from_stats(alltasks)
-
 
-
 
79
    def fetch(self, sid):
-
 
80
        db = self.get_service('DB')
-
 
81
        valid = False
-
 
82
        #~ self.log.debug("%3d/%3d - Fetching SAP Note %s" % (self.notes_fetched+1, self.notes_total, sid))
109
83
110
            # components
-
 
111
            compkey = self.sapnotes[sid]['componentkey']
-
 
112
            comptxt = self.sapnotes[sid]['componenttxt']
-
 
113
            component = escape("%s (%s)" % (compkey, comptxt))
-
 
114
            sep = compkey.find('-')
-
 
115
            if sep > 0:
-
 
116
                maincomp = compkey[0:sep]
-
 
-
 
84
        if not db.is_stored(sid):
-
 
85
            self.log.debug("%3d/%3d - SAP Note %s must be downloaded" % (self.notes_fetched+1, self.notes_total, sid))
-
 
86
            content = self.download(sid)
-
 
87
            if len(content) > 0:
-
 
88
                self.log.debug("%3d/%3d - SAP Note %s fetched" % (self.notes_fetched+1, self.notes_total, sid))
117
            else:
89
            else:
118
                maincomp = compkey
-
 
119
-
 
120
            # categories
-
 
121
            category = category = escape(self.sapnotes[sid]['category'])
-
 
122
            try:
-
 
123
                cont = self.stats['cats'][category]
-
 
124
                self.stats['cats'][category] = cont + 1
-
 
125
            except:
-
 
126
                self.stats['cats'][category] = 1
-
 
127
-
 
128
            # Build al (sub)keys from given component key
-
 
129
            # useful for display stats with pygal
-
 
130
            compkeys = compkey.split('-')
-
 
131
            total = len(compkeys)
-
 
132
            key = ''
-
 
133
            i = 0
-
 
134
            for subkey in compkeys:
-
 
135
                key = key + '-' + subkey
-
 
136
                if key[:1] == '-':
-
 
137
                    key = key[1:]
-
 
138
-
 
139
                # update stats
-
 
140
                try:
-
 
141
                    count = self.compstats[key]
-
 
142
                    self.compstats[key] = count + 1
-
 
143
                except Exception as error:
-
 
144
                    self.compstats[key] = 1
-
 
145
-
 
146
            try:
-
 
147
                cont = self.stats['maincomp'][maincomp]
-
 
148
                self.stats['maincomp'][maincomp] = cont + 1
-
 
149
            except:
-
 
150
                self.stats['maincomp'][maincomp] = 1
-
 
151
-
 
152
            category = escape(self.sapnotes[sid]['category'])
-
 
153
            priority = escape(self.sapnotes[sid]['priority'])
-
 
154
            ntype = escape(self.sapnotes[sid]['type'])
-
 
155
            version = escape(self.sapnotes[sid]['version'])
-
 
156
            releaseon = escape(self.sapnotes[sid]['releaseon'])
-
 
157
            self.stats['component'].add(component)
-
 
158
            self.stats['category'].add(category)
-
 
159
            self.stats['priority'].add(priority)
-
 
160
            self.stats['type'].add(ntype)
-
 
161
            self.stats['version'].add(version)
-
 
162
            #~ self.stats['releaseon'].add(releaseon)
-
 
163
            #~ self.stats[''].add(version)
-
 
164
        #~ self.log.debug(self.compstats)
-
 
165
        #~ self.log.debug("==")
-
 
166
        #~ self.log.debug(self.stats)
-
 
167
        self.log.debug(self.stats['maincomp'])
-
 
168
-
 
169
-
 
170
-
 
171
    def get_stats(self):
-
 
172
        return self.stats
-
 
173
-
 
174
-
 
175
    def download_note(self, sapnote, driver):
-
 
176
        downloaded = False
-
 
177
        try:
-
 
178
            self.log.debug("Downloading SAP Note %s" % sapnote)
-
 
179
            self.log.debug("Waiting for page %s..." % (ODATA_NOTE_URL % sapnote))
-
 
180
            driver.get(ODATA_NOTE_URL % sapnote)
-
 
181
            #wait for ajax items to load
-
 
182
            #~ WebDriverWait(driver, 60).until(self.ajax_complete,  "Timeout waiting for page to load")
-
 
183
            time.sleep(5)
-
 
184
            self.log.debug("Page loaded")
-
 
185
            downloaded = self.add_note(sapnote, driver.page_source)
-
 
186
        except:
-
 
187
            self.log.error("SAP Note %s coud not be downloaded" % sapnote)
-
 
188
-
 
189
        return downloaded
-
 
190
-
 
191
-
 
192
    def connect(self):
-
 
193
        driver = self.get_webdriver()
-
 
194
        if driver is not None:
-
 
195
            self.login_sso(driver)
-
 
196
            self.log.debug ("Connectig to SAP...")
-
 
197
-
 
198
        return driver
-
 
199
-
 
200
-
 
201
    def get_default_settings(self):
-
 
202
        settings = {}
-
 
203
        settings['CNF_SAP_SUser'] = 'SXXXXXXXXXX'
-
 
204
        settings['CNF_SAP_SPass'] = 'MyP455w0rD'
-
 
205
        settings['CNF_SAP_Login'] = LOGIN_PAGE_URL
-
 
206
        settings['CNF_SAP_Logout'] = LOGOUT_PAGE_URL
-
 
207
        settings['CNF_SAP_OData_Notes'] = ODATA_NOTE_URL
-
 
208
        settings['CNF_SAP_Notes_URL'] = SAP_NOTE_URL
-
 
209
        settings['CNF_SAP_CONN_TIMEOUT'] = TIMEOUT
-
 
210
-
 
211
        return settings
-
 
212
-
 
213
-
 
214
    def get_custom_settings(self):
-
 
215
        settings = {}
-
 
216
        settings['CNF_SAP_SUser'] = self.get_config_value('CNF_SAP_SUser')
-
 
217
        settings['CNF_SAP_SPass'] = self.get_config_value('CNF_SAP_SPass')
-
 
218
        settings['CNF_SAP_Login'] = self.get_config_value('CNF_SAP_Login')
-
 
219
        settings['CNF_SAP_Logout'] = self.get_config_value('CNF_SAP_Logout')
-
 
220
        settings['CNF_SAP_OData_Notes'] = self.get_config_value('CNF_SAP_OData_Notes')
-
 
221
        settings['CNF_SAP_Notes_URL'] = self.get_config_value('CNF_SAP_Notes_URL')
-
 
222
-
 
223
        return settings
-
 
224
-
 
225
-
 
226
    def get_webdriver(self):
-
 
227
        '''
-
 
228
        FIXME: check supported webdrivers and return the webdriver
-
 
229
        which best fits:
-
 
230
        1.- PhantomJS
-
 
231
        2.- Firefox
-
 
232
        '''
-
 
233
-
 
234
        driver = None
-
 
235
        self.log.debug("Loading webdriver...")
-
 
236
-
 
237
        phantomjs = which('phantomjs')
-
 
238
-
 
239
        if phantomjs is not None:
-
 
240
            driver = webdriver.PhantomJS()
-
 
241
            self.log.debug("PhantomJS driver available")
-
 
-
 
90
                self.log.debug("%3d/%3d - SAP Note %s not feched" % (self.notes_fetched+1, self.notes_total, sid))
242
        else:
91
        else:
243
            driver = webdriver.Firefox()
-
 
244
            self.log.debug("Firefox driver available")
-
 
-
 
92
            self.log.debug("%3d/%3d - SAP Note %s will be analyzed again" % (self.notes_fetched+1, self.notes_total, sid))
-
 
93
            content = db.get_sapnote_content(sid)
245
94
246
        return driver
-
 
-
 
95
        self.fetched()
247
96
248
        #~ if sys.platform == 'linux':
-
 
249
            #~ # First we try with PhantomJS
-
 
250
            #~ try:
-
 
251
                #~ driver = webdriver.PhantomJS()
-
 
252
                #~ self.log.debug("PhantomJS available")
-
 
253
            #~ except:
-
 
254
                #~ self.log.warning("PhantomJS not available")
-
 
255
                #~ try:
-
 
256
                    #~ driver = webdriver.Firefox()
-
 
257
                    #~ self.log.debug ("Firefox available")
-
 
258
                #~ except:
-
 
259
                    #~ self.log.error("Firefox not available")
-
 
260
                    #~ driver = None
-
 
261
        #~ elif sys.platform == 'win32':
-
 
262
            #~ try:
-
 
263
                #~ driver = webdriver.Ie("C:\\windows\\system32\\IEDriverServer.exe")
-
 
264
                #~ self.log.debug ("IE available")
-
 
265
            #~ except:
-
 
266
                #~ self.log.error("IE not available")
-
 
267
                #~ driver = None
-
 
268
        #~ return driver
-
 
-
 
97
        sapnote = self.analyze_sapnote_metadata(sid, content)
-
 
98
        if len(sapnote) > 0:
-
 
99
            db = self.get_service('DB')
-
 
100
            db.add(sapnote)
-
 
101
            db.store(sid, content)
-
 
102
            valid = True
269
103
-
 
104
        return valid, sid
270
105
271
    def logout(self, driver):
-
 
272
        driver.get(LOGOUT_PAGE_URL)
-
 
273
        driver.quit()
-
 
274
106
-
 
107
    def start_fetching(self, total):
-
 
108
        self.notes_fetched = 0
-
 
109
        self.notes_total = total
275
110
276
    def login_sso(self, driver):
-
 
277
        '''
-
 
278
        Login into SAP Support
-
 
279
        '''
-
 
280
        self.log.debug("Waiting for page...")
-
 
281
        myuser = self.get_config_value('CNF_SAP_SUser')
-
 
282
        mypass = self.get_config_value('CNF_SAP_SPass')
-
 
283
        myloginpage = self.get_config_value('CNF_SAP_Login')
-
 
284
111
285
        driver.get(myloginpage)
-
 
286
        self.wait_for_page_load(driver)
-
 
287
        self.log.debug("Page loaded")
-
 
288
        username = driver.find_element_by_id('j_username')
-
 
289
        username.send_keys(myuser)
-
 
290
        password = driver.find_element_by_id('j_password')
-
 
291
        password.send_keys(mypass)
-
 
292
        login = driver.find_element_by_id('logOnFormSubmit')
-
 
293
        login.click()
-
 
294
        # FIXME: Wait time as editable property
-
 
295
        #~ time.sleep(5)
-
 
-
 
112
    def fetched(self):
-
 
113
        self.notes_fetched += 1
296
114
297
115
298
    def feedparser_parse(self, thing):
-
 
299
        try:
-
 
300
            return feedparser.parse(thing)
-
 
301
        except TypeError:
-
 
302
            if 'drv_libxml2' in feedparser.PREFERRED_XML_PARSERS:
-
 
303
                feedparser.PREFERRED_XML_PARSERS.remove('drv_libxml2')
-
 
304
                return feedparser.parse(thing)
-
 
305
            else:
-
 
306
                self.log.error(self.get_traceback())
-
 
307
                raise
-
 
-
 
116
    def stop_fetching(self):
-
 
117
        self.notes_fetched = 0
-
 
118
        self.notes_total = 0
308
119
309
120
310
-
 
311
    def get_notes(self):
-
 
312
        '''
-
 
313
        Return all sapnotes
-
 
314
        '''
-
 
315
        return self.sapnotes
-
 
316
-
 
317
-
 
318
    def get_total(self):
-
 
319
        '''
-
 
320
        Return total sapnotes
-
 
321
        '''
-
 
322
        return len(self.sapnotes)
-
 
323
-
 
324
-
 
325
    def load_notes(self):
-
 
326
        '''
-
 
327
        If notes.json exists, load notes
-
 
328
        '''
-
 
-
 
121
    def download(self, sapnote=None):
329
        try:
122
        try:
330
            fnotes = self.get_file('SAP')
-
 
331
            with open(fnotes, 'r') as fp:
-
 
332
                self.sapnotes = json.load(fp)
-
 
333
                self.log.debug ("Loaded %d notes" % len(self.sapnotes))
-
 
-
 
123
            driver = self.get_service('Driver')
-
 
124
            #~ browser = driver.open()
-
 
125
            #~ self.log.debug("\tDownloading SAP Note %s:" % sapnote)
-
 
126
            #~ self.log.debug(ODATA_NOTE_URL % sapnote)
-
 
127
            browser = driver.load(ODATA_NOTE_URL % sapnote)
-
 
128
            time.sleep(5)
-
 
129
            content = browser.page_source
-
 
130
            #~ self.log.debug("\t%3d - SAP Note %s downloaded" % (sapnote, driver.)
-
 
131
            #~ else:
-
 
132
            #~ content = ''
334
        except Exception as error:
133
        except Exception as error:
335
            self.log.info("SAP Notes database not found. Creating a new one")
-
 
336
            self.save_notes()
-
 
337
-
 
338
-
 
339
    def get_node(self, sid):
-
 
340
        try:
-
 
341
            return self.sapnotes[sid]
-
 
342
        except Exception as KeyError:
-
 
343
            return None
-
 
344
-
 
345
-
 
346
    def get_note(self, note):
-
 
347
        '''
-
 
348
        Get xml from SAP Support Notes service
-
 
349
        '''
-
 
350
        self.driver.get(ODATA_NOTE_URL % note)
-
 
351
        return self.driver.page_source
-
 
352
-
 
353
-
 
354
    def add_note(self, sapnote, content):
-
 
355
        '''
-
 
356
        Get header details from SAP Note
-
 
357
        '''
-
 
358
        try:
-
 
359
            f= self.feedparser_parse(content)
-
 
360
            f = feedparser.parse(content)
-
 
361
            sid = f.entries[0].d_sapnotesnumber
-
 
362
            self.sapnotes[sid] = {}
-
 
363
            self.sapnotes[sid]['id'] = sid
-
 
364
            self.sapnotes[sid]['componentkey'] = f.entries[0].d_componentkey
-
 
365
            self.sapnotes[sid]['componenttxt'] = f.entries[0].d_componenttext
-
 
366
            self.sapnotes[sid]['category'] = f.entries[0].d_category_detail['value']
-
 
367
            self.sapnotes[sid]['language'] = f.entries[0].d_languagetext_detail['value']
-
 
368
            self.sapnotes[sid]['title'] = f.entries[0].d_title_detail['value']
-
 
369
            self.sapnotes[sid]['priority'] = f.entries[0].d_priority_detail['value']
-
 
370
            self.sapnotes[sid]['releaseon'] = f.entries[0].d_releasedon
-
 
371
            self.sapnotes[sid]['type'] = f.entries[0].d_type_detail['value']
-
 
372
            self.sapnotes[sid]['version'] = f.entries[0].d_version_detail['value']
-
 
373
            self.sapnotes[sid]['feedupdate'] = f.entries[0].updated
-
 
374
            self.sapnotes[sid]['bookmark'] = False
-
 
375
            self.log.info ("Added SAP Note %s" % sapnote)
-
 
376
            return True
-
 
377
        except Exception as error:
-
 
378
            self.log.error("SAP Note %s could not be analyzed" % sapnote)
-
 
379
            return False
-
 
380
-
 
381
-
 
382
    def browse_notes(self, sapnotes):
-
 
383
        driver = webdriver.Firefox()
-
 
384
        self.login_sso(driver)
-
 
385
        for sapnote in sapnotes:
-
 
386
            self.log.debug("Browsing SAP Note %s" % sapnote)
-
 
387
            body = driver.find_element_by_tag_name("body")
-
 
388
            body.send_keys(Keys.CONTROL + 't')
-
 
389
            driver.get(SAP_NOTE_URL % sapnote)
-
 
390
            self.wait_for_page_load(driver)
-
 
391
-
 
392
-
 
393
    def save_notes(self, filename='', bag={}):
-
 
394
        '''
-
 
395
        Save SAP Notes to file
-
 
396
        '''
-
 
397
        if len(filename) == 0:
-
 
398
            filename = self.get_file('SAP')
-
 
399
            bag = self.sapnotes
-
 
400
-
 
401
        fnotes = open(filename, 'w')
-
 
402
        json.dump(bag, fnotes)
-
 
403
        fnotes.close()
-
 
404
        self.log.info ("Saved %d notes to %s" % (len(bag), filename))
-
 
405
-
 
406
-
 
407
-
 
408
-
 
409
    def apply_preferences(self, *args):
-
 
410
        self.gui = self.app.get_service('GUI')
-
 
411
        settings = self.get_default_settings()
-
 
412
        new_settings = {}
-
 
413
        for key in settings:
-
 
414
            widget = self.gui.get_widget(key)
-
 
415
            value = widget.get_text()
-
 
416
            new_settings[key] = value
-
 
417
        #~ self.log.debug(new_settings)
-
 
418
        self.config[self.section] = new_settings
-
 
419
        self.save_config()
-
 
420
        self.log.debug("Settings saved")
-
 
421
-
 
422
-
 
423
-
 
424
    def default_preferences(self, *args):
-
 
425
        self.gui = self.app.get_service('GUI')
-
 
426
        settings = self.get_default_settings()
-
 
427
        for key in settings:
-
 
428
            widget = self.gui.get_widget(key)
-
 
429
            widget.set_text(settings[key])
-
 
-
 
134
            #~ self.log.error("\tSAP Note %s coud not be downloaded" % sapnote)
-
 
135
            self.log.error(error)
-
 
136
            content = ''
430
137
431
        #~ self.log.debug(settings)
-
 
432
        self.config[self.section] = settings
-
 
433
        self.save_config()
-
 
434
        infobar = self.gui.get_widget('infobar')
-
 
435
        infobar.set_markup("<b>Settings reverted to default</b>")
-
 
-
 
138
        return content
436
139
437
140
438
    def get_linked_projects(self, sapnote):
141
    def get_linked_projects(self, sapnote):
439
        try:
142
        try:
440
            projects = self.sapnotes[sapnote]['projects']
143
            projects = self.sapnotes[sapnote]['projects']
Line 449... Line 152...
449
            tasks = self.sapnotes[sapnote]['tasks']
152
            tasks = self.sapnotes[sapnote]['tasks']
450
        except Exception as error:
153
        except Exception as error:
451
            tasks = []
154
            tasks = []
452
        self.log.debug("Tasks: %s" % tasks)
155
        self.log.debug("Tasks: %s" % tasks)
453
        return tasks
156
        return tasks
454
-
 
455
-
 
456
    #~ def link_to_project(self, sapnotes, projects):
-
 
457
        #~ for sapnote in sapnotes:
-
 
458
            #~ try:
-
 
459
                #~ self.sapnotes[sapnote]['projects'] = projects
-
 
460
                #~ self.log.debug("Linked SAP Note %s to project(s): %s" % (sapnote, projects) )
-
 
461
            #~ except:
-
 
462
                #~ self.log.error(self.get_traceback())
-
 
463
157
464
158
465
    def link_to_task(self, sapnotes, tasks):
159
    def link_to_task(self, sapnotes, tasks):
466
        for sapnote in sapnotes:
-
 
467
            try:
-
 
468
                self.sapnotes[sapnote]['tasks'] = tasks
-
 
469
                self.log.info("Linked SAP Note %s to task(s): %s" % (sapnote, tasks) )
-
 
470
            except:
-
 
471
                self.log.error(self.get_traceback())
-
 
-
 
160
        if len(sapnotes) == 0:
-
 
161
            self.log.debug("No SAP Notes selected. No task will be linked.")
-
 
162
            return
472
163
-
 
164
        db = self.get_service('DB')
-
 
165
        store = db.get_notes()
473
166
474
    def import_sapnotes(self, bag):
-
 
475
        for sid in bag:
-
 
476
            # Check if SAP Note exists in main database
-
 
477
            found = self.get_node(sid)
-
 
478
            if found is None:
-
 
479
                self.sapnotes[sid] = bag[sid]
-
 
480
            else:
-
 
481
                # Import only tasks
-
 
482
                try:
-
 
483
                    imptasks = bag[sid]['tasks']
-
 
484
                    tasks = self.sapnotes[sid]['tasks']
-
 
485
                    tasks.extend(imptasks)
-
 
486
                    self.sapnotes[sid]['tasks'] = tasks
-
 
487
                except: pass
-
 
488
                # Import other metadata
-
 
-
 
167
        for tid in sapnotes:
-
 
168
            sid = "0"*(10 - len(tid)) + tid
-
 
169
            store[sid]['tasks'] = tasks
-
 
170
            self.log.info("Linked SAP Note %s to task(s): %s" % (sid, tasks) )
489
171
490
172
491
    def set_bookmark(self, sapnotes):
-
 
492
        for sapnote in sapnotes:
-
 
493
            self.log.info("SAP Note %s bookmarked: True" % sapnote)
-
 
494
            self.sapnotes[sapnote]['bookmark'] = True
-
 
495
        self.save_notes()
-
 
-
 
173
    def set_bookmark(self, bag):
-
 
174
        db = self.get_service('DB')
-
 
175
        sapnotes = db.get_notes()
-
 
176
        mylist = []
-
 
177
        for tid in bag:
-
 
178
            sid = "0"*(10 - len(tid)) + tid
-
 
179
            sapnotes[sid]['bookmark'] = True
-
 
180
            mylist.append(sapnotes[sid])
-
 
181
            self.log.info("SAP Note %s bookmarked" % sid)
-
 
182
        db.add_list(mylist)
496
183
497
184
498
    def set_no_bookmark(self, sapnotes):
-
 
499
        for sapnote in sapnotes:
-
 
500
            self.log.info("SAP Note %s bookmarked: False" % sapnote)
-
 
501
            self.sapnotes[sapnote]['bookmark'] = False
-
 
502
        self.save_notes()
-
 
-
 
185
    def set_no_bookmark(self, bag):
-
 
186
        db = self.get_service('DB')
-
 
187
        sapnotes = db.get_notes()
-
 
188
        mylist = []
-
 
189
        for tid in bag:
-
 
190
            sid = "0"*(10 - len(tid)) + tid
-
 
191
            sapnotes[sid]['bookmark'] = False
-
 
192
            mylist.append(sapnotes[sid])
-
 
193
            self.log.info("SAP Note %s unbookmarked" % sid)
-
 
194
        db.add_list(mylist)
503
195
504
196
505
    def is_bookmark(self, sapnote):
197
    def is_bookmark(self, sapnote):
506
        try:
198
        try:
507
            return self.sapnotes[sapnote]['bookmark']
199
            return self.sapnotes[sapnote]['bookmark']
508
        except:
200
        except:
509
            return False
201
            return False
510
-
 
511
-
 
512
    def delete_sapnote(self, sapnote):
-
 
513
        deleted = False
-
 
514
        try:
-
 
515
            del (self.sapnotes[sapnote])
-
 
516
            deleted = True
-
 
517
        except:
-
 
518
            deleted = False
-
 
519
-
 
520
        return deleted
-
 
521
202
522
203
523
    def run(self):
204
    def run(self):
524
        self.load_notes()
-
 
525
        self.build_stats()
-
 
526
-
 
527
-
 
528
    def quit(self):
-
 
529
        self.driver.quit()
-
 
530
-
 
531
-
 
532
    def end(self):
-
 
533
        self.save_notes()
-
 
534
-
 
-
 
205
        db = self.get_service('DB')
-
 
206
        db.load_notes()
-
 
207
        db.build_stats()