Subversion Repositories basico

Rev

Rev 126 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
# File: sap.py
4
# Author: Tomás Vírseda
5
# License: GPL v3
6
# Description: SAP service
7
 
8
import time
9
import traceback
10
from shutil import which
11
 
12
from .service import Service
13
 
74 t00mlabs 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"
77 t00mlabs 15
#~ ODATA_NOTE_URL_LONGTEXT = "https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='0000000001',Version='0',Language='E')/LongText"
74 t00mlabs 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"
126 t00mlabs 18
 
19
# Default settings for SAP module
20
LOGIN_PAGE_URL = "https://accounts.sap.com"
21
LOGOUT_PAGE_URL = "https://accounts.sap.com/ui/logout"
22
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"
130 t00mlabs 23
ODATA_NOTE_URL_LONGTEXT = "view-source:https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='%s',Version='0',Language='E')/LongText"
4 t00mlabs 24
SAP_NOTE_URL = "https://launchpad.support.sap.com/#/notes/%s"
74 t00mlabs 25
SAP_NOTE_URL_PDF = "https://launchpad.support.sap.com/services/pdf/notes/%s/E"
126 t00mlabs 26
TIMEOUT = 5
4 t00mlabs 27
 
77 t00mlabs 28
"""
29
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/RefBy"
30
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/RefTo"
31
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/CorrIns"
32
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/Patch"
33
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/Sp"
34
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/SoftCom"
35
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/Attach"
36
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/LongText"
37
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/Languages"
38
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/SideCau"
39
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/SideSol"
4 t00mlabs 40
 
77 t00mlabs 41
"""
42
 
80 t00mlabs 43
 
4 t00mlabs 44
class SAP(Service):
45
    def initialize(self):
46
        '''
47
        Setup AppLogic Service
48
        '''
126 t00mlabs 49
        self.settings = self.get_service('Settings')
4 t00mlabs 50
        self.__init_config_section()
51
 
52
 
53
    def __init_config_section(self):
126 t00mlabs 54
        settings = self.settings.load()
55
        settings[self.section]
56
        try:
57
            settings[self.section]['LOGIN_PAGE_URL']
58
        except:
59
            settings[self.section]['LOGIN_PAGE_URL'] = LOGIN_PAGE_URL
4 t00mlabs 60
 
126 t00mlabs 61
        try:
62
            settings[self.section]['LOGOUT_PAGE_URL']
63
        except:
64
            settings[self.section]['LOGOUT_PAGE_URL'] = LOGOUT_PAGE_URL
4 t00mlabs 65
 
126 t00mlabs 66
        try:
67
            settings[self.section]['ODATA_NOTE_URL']
68
        except:
69
            settings[self.section]['ODATA_NOTE_URL'] = ODATA_NOTE_URL
70
 
71
        try:
72
            settings[self.section]['SAP_NOTE_URL']
73
        except:
74
            settings[self.section]['SAP_NOTE_URL'] = SAP_NOTE_URL
75
 
76
        try:
77
            settings[self.section]['SAP_NOTE_URL_PDF']
78
        except:
79
            settings[self.section]['SAP_NOTE_URL_PDF'] = SAP_NOTE_URL_PDF
80
 
81
        try:
82
            settings[self.section]['TIMEOUT']
83
        except:
84
            settings[self.section]['TIMEOUT'] = TIMEOUT
85
 
86
        self.settings.save(settings)
87
 
88
 
74 t00mlabs 89
    def analyze_sapnote_metadata(self, sid, content):
90
        '''
91
        Get metadata details from SAP Note
92
        '''
93
        #~ self.log.debug("\t%s" % content)
4 t00mlabs 94
        try:
74 t00mlabs 95
            utils = self.get_service('Utils')
96
            f = utils.feedparser_parse(content)
97
            sid = f.entries[0].d_sapnotesnumber
98
            sapnote = {}
99
            sapnote['id'] = sid
100
            sapnote['componentkey'] = f.entries[0].d_componentkey
101
            comptxt = f.entries[0].d_componenttext
102
            if comptxt == "Please use note 1433157 for finding the right component":
103
                comptxt = ""
104
            sapnote['componenttxt'] = comptxt
105
            sapnote['category'] = f.entries[0].d_category_detail['value']
106
            sapnote['language'] = f.entries[0].d_languagetext_detail['value']
107
            sapnote['title'] = f.entries[0].d_title_detail['value']
108
            sapnote['priority'] = f.entries[0].d_priority_detail['value']
109
            sapnote['releaseon'] = f.entries[0].d_releasedon
110
            sapnote['type'] = f.entries[0].d_type_detail['value']
111
            sapnote['version'] = f.entries[0].d_version_detail['value']
112
            sapnote['feedupdate'] = f.entries[0].updated
113
            sapnote['bookmark'] = False
114
            self.log.info ("\tSAP Note %s analyzed successfully" % sid)
4 t00mlabs 115
        except Exception as error:
74 t00mlabs 116
            sapnote = {}
117
            self.log.error("\tError while analyzing data for SAP Note %s" % sid)
118
            #~ self.log.error("\t%s" % error)
4 t00mlabs 119
 
74 t00mlabs 120
        return sapnote
4 t00mlabs 121
 
122
 
74 t00mlabs 123
    def fetch(self, sid):
124
        db = self.get_service('DB')
125
        valid = False
126
        #~ self.log.debug("%3d/%3d - Fetching SAP Note %s" % (self.notes_fetched+1, self.notes_total, sid))
4 t00mlabs 127
 
74 t00mlabs 128
        if not db.is_stored(sid):
129
            self.log.debug("%3d/%3d - SAP Note %s must be downloaded" % (self.notes_fetched+1, self.notes_total, sid))
130
            content = self.download(sid)
131
            if len(content) > 0:
132
                self.log.debug("%3d/%3d - SAP Note %s fetched" % (self.notes_fetched+1, self.notes_total, sid))
4 t00mlabs 133
            else:
74 t00mlabs 134
                self.log.debug("%3d/%3d - SAP Note %s not feched" % (self.notes_fetched+1, self.notes_total, sid))
4 t00mlabs 135
        else:
74 t00mlabs 136
            self.log.debug("%3d/%3d - SAP Note %s will be analyzed again" % (self.notes_fetched+1, self.notes_total, sid))
137
            content = db.get_sapnote_content(sid)
4 t00mlabs 138
 
74 t00mlabs 139
        self.fetched()
4 t00mlabs 140
 
74 t00mlabs 141
        sapnote = self.analyze_sapnote_metadata(sid, content)
142
        if len(sapnote) > 0:
143
            db = self.get_service('DB')
144
            db.add(sapnote)
145
            db.store(sid, content)
146
            valid = True
4 t00mlabs 147
 
74 t00mlabs 148
        return valid, sid
4 t00mlabs 149
 
150
 
82 t00mlabs 151
 
126 t00mlabs 152
    #~ def apply_preferences(self, *args):
153
        #~ self.gui = self.app.get_service('GUI')
154
        #~ settings = self.get_default_settings()
155
        #~ new_settings = {}
156
        #~ for key in settings:
157
            #~ widget = self.gui.get_widget(key)
158
            #~ value = widget.get_text()
159
            #~ new_settings[key] = value
82 t00mlabs 160
        #~ self.log.debug(new_settings)
126 t00mlabs 161
        #~ self.config[self.section] = new_settings
162
        #~ self.save_config()
163
        #~ self.log.debug("Settings saved")
82 t00mlabs 164
 
165
 
74 t00mlabs 166
    def start_fetching(self, total):
167
        self.notes_fetched = 0
168
        self.notes_total = total
4 t00mlabs 169
 
170
 
74 t00mlabs 171
    def fetched(self):
172
        self.notes_fetched += 1
4 t00mlabs 173
 
174
 
74 t00mlabs 175
    def stop_fetching(self):
176
        self.notes_fetched = 0
177
        self.notes_total = 0
4 t00mlabs 178
 
179
 
74 t00mlabs 180
    def download(self, sapnote=None):
4 t00mlabs 181
        try:
74 t00mlabs 182
            driver = self.get_service('Driver')
183
            #~ browser = driver.open()
184
            #~ self.log.debug("\tDownloading SAP Note %s:" % sapnote)
185
            #~ self.log.debug(ODATA_NOTE_URL % sapnote)
126 t00mlabs 186
            ODATA_NOTE_URL = self.settings.get('SAP', 'ODATA_NOTE_URL')
74 t00mlabs 187
            browser = driver.load(ODATA_NOTE_URL % sapnote)
80 t00mlabs 188
            time.sleep(10)
74 t00mlabs 189
            content = browser.page_source
130 t00mlabs 190
            #~ browser = driver.load(ODATA_NOTE_URL_LONGTEXT % sapnote)
191
            #~ time.sleep(10)
192
            #~ longtext = browser.page_source
193
            #~ self.log.debug(longtext)
194
 
74 t00mlabs 195
            #~ self.log.debug("\t%3d - SAP Note %s downloaded" % (sapnote, driver.)
196
            #~ else:
197
            #~ content = ''
4 t00mlabs 198
        except Exception as error:
74 t00mlabs 199
            #~ self.log.error("\tSAP Note %s coud not be downloaded" % sapnote)
200
            self.log.error(error)
201
            content = ''
4 t00mlabs 202
 
74 t00mlabs 203
        return content
4 t00mlabs 204
 
205
 
206
    def get_linked_projects(self, sapnote):
207
        try:
208
            projects = self.sapnotes[sapnote]['projects']
209
        except Exception as error:
210
            projects = []
211
 
212
        return projects
213
 
214
 
215
    def get_linked_tasks(self, sapnote):
216
        try:
217
            tasks = self.sapnotes[sapnote]['tasks']
218
        except Exception as error:
219
            tasks = []
220
        self.log.debug("Tasks: %s" % tasks)
221
        return tasks
222
 
223
 
224
    def link_to_task(self, sapnotes, tasks):
74 t00mlabs 225
        if len(sapnotes) == 0:
226
            self.log.debug("No SAP Notes selected. No task will be linked.")
227
            return
4 t00mlabs 228
 
74 t00mlabs 229
        db = self.get_service('DB')
230
        store = db.get_notes()
4 t00mlabs 231
 
74 t00mlabs 232
        for tid in sapnotes:
233
            sid = "0"*(10 - len(tid)) + tid
234
            store[sid]['tasks'] = tasks
235
            self.log.info("Linked SAP Note %s to task(s): %s" % (sid, tasks) )
4 t00mlabs 236
 
237
 
74 t00mlabs 238
    def set_bookmark(self, bag):
239
        db = self.get_service('DB')
240
        sapnotes = db.get_notes()
241
        mylist = []
242
        for tid in bag:
243
            sid = "0"*(10 - len(tid)) + tid
244
            sapnotes[sid]['bookmark'] = True
245
            mylist.append(sapnotes[sid])
246
            self.log.info("SAP Note %s bookmarked" % sid)
247
        db.add_list(mylist)
4 t00mlabs 248
 
249
 
74 t00mlabs 250
    def set_no_bookmark(self, bag):
251
        db = self.get_service('DB')
252
        sapnotes = db.get_notes()
253
        mylist = []
254
        for tid in bag:
255
            sid = "0"*(10 - len(tid)) + tid
256
            sapnotes[sid]['bookmark'] = False
257
            mylist.append(sapnotes[sid])
258
            self.log.info("SAP Note %s unbookmarked" % sid)
259
        db.add_list(mylist)
4 t00mlabs 260
 
261
 
262
    def is_bookmark(self, sapnote):
263
        try:
264
            return self.sapnotes[sapnote]['bookmark']
265
        except:
266
            return False
267
 
268
 
269
    def run(self):
74 t00mlabs 270
        db = self.get_service('DB')
271
        db.load_notes()
272
        db.build_stats()