Subversion Repositories basico

Rev

Rev 45 | 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
24 t00mlabs 11
 
4 t00mlabs 12
from .service import Service
13
 
41 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"
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"
4 t00mlabs 18
SAP_NOTE_URL = "https://launchpad.support.sap.com/#/notes/%s"
41 t00mlabs 19
SAP_NOTE_URL_PDF = "https://launchpad.support.sap.com/services/pdf/notes/%s/E"
42 t00mlabs 20
 
21
 
4 t00mlabs 22
class SAP(Service):
23
    def initialize(self):
24
        '''
25
        Setup AppLogic Service
26
        '''
27
        self.__init_config_section()
28
 
29
 
30
    def __init_config_section(self):
38 t00mlabs 31
        prefs = self.get_service('Settings')
4 t00mlabs 32
        self.config = self.app.get_config()
33
        if self.config.has_section(self.section):
34
            options = self.config.options(self.section)
35
            if len(options) == 0:
36
                self.log.debug("Section %s empty. Initializing with default values" % self.section)
38 t00mlabs 37
                settings = prefs.get_default_settings()
4 t00mlabs 38
                self.config[self.section] = settings
32 t00mlabs 39
                self.log.debug("Default parameters loaded:")
40
                for key in settings:
41
                    self.log.debug("\tKey: %s - Value: %s" % (key, settings[key]))
4 t00mlabs 42
                self.save_config()
43
 
44
 
34 t00mlabs 45
    def analyze_sapnote_metadata(self, sid, content):
28 t00mlabs 46
        '''
34 t00mlabs 47
        Get metadata details from SAP Note
28 t00mlabs 48
        '''
41 t00mlabs 49
        #~ self.log.debug("\t%s" % content)
4 t00mlabs 50
        try:
38 t00mlabs 51
            utils = self.get_service('Utils')
52
            f = utils.feedparser_parse(content)
28 t00mlabs 53
            sid = f.entries[0].d_sapnotesnumber
54
            sapnote = {}
55
            sapnote['id'] = sid
56
            sapnote['componentkey'] = f.entries[0].d_componentkey
41 t00mlabs 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
28 t00mlabs 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
33 t00mlabs 70
            self.log.info ("\tSAP Note %s analyzed successfully" % sid)
28 t00mlabs 71
        except Exception as error:
72
            sapnote = {}
34 t00mlabs 73
            self.log.error("\tError while analyzing data for SAP Note %s" % sid)
38 t00mlabs 74
            #~ self.log.error("\t%s" % error)
28 t00mlabs 75
 
76
        return sapnote
77
 
78
 
79
    def fetch(self, sid):
34 t00mlabs 80
        db = self.get_service('DB')
28 t00mlabs 81
        valid = False
73 t00mlabs 82
        #~ self.log.debug("%3d/%3d - Fetching SAP Note %s" % (self.notes_fetched+1, self.notes_total, sid))
28 t00mlabs 83
 
34 t00mlabs 84
        if not db.is_stored(sid):
73 t00mlabs 85
            self.log.debug("%3d/%3d - SAP Note %s must be downloaded" % (self.notes_fetched+1, self.notes_total, sid))
28 t00mlabs 86
            content = self.download(sid)
73 t00mlabs 87
            if len(content) > 0:
88
                self.log.debug("%3d/%3d - SAP Note %s fetched" % (self.notes_fetched+1, self.notes_total, sid))
89
            else:
90
                self.log.debug("%3d/%3d - SAP Note %s not feched" % (self.notes_fetched+1, self.notes_total, sid))
28 t00mlabs 91
        else:
73 t00mlabs 92
            self.log.debug("%3d/%3d - SAP Note %s will be analyzed again" % (self.notes_fetched+1, self.notes_total, sid))
34 t00mlabs 93
            content = db.get_sapnote_content(sid)
28 t00mlabs 94
 
73 t00mlabs 95
        self.fetched()
96
 
34 t00mlabs 97
        sapnote = self.analyze_sapnote_metadata(sid, content)
28 t00mlabs 98
        if len(sapnote) > 0:
34 t00mlabs 99
            db = self.get_service('DB')
100
            db.add(sapnote)
101
            db.store(sid, content)
28 t00mlabs 102
            valid = True
103
 
104
        return valid, sid
105
 
106
 
73 t00mlabs 107
    def start_fetching(self, total):
108
        self.notes_fetched = 0
109
        self.notes_total = total
110
 
111
 
112
    def fetched(self):
113
        self.notes_fetched += 1
114
 
115
 
116
    def stop_fetching(self):
117
        self.notes_fetched = 0
118
        self.notes_total = 0
119
 
120
 
33 t00mlabs 121
    def download(self, sapnote=None):
28 t00mlabs 122
        try:
35 t00mlabs 123
            driver = self.get_service('Driver')
73 t00mlabs 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 = ''
35 t00mlabs 133
        except Exception as error:
73 t00mlabs 134
            #~ self.log.error("\tSAP Note %s coud not be downloaded" % sapnote)
35 t00mlabs 135
            self.log.error(error)
28 t00mlabs 136
            content = ''
4 t00mlabs 137
 
28 t00mlabs 138
        return content
4 t00mlabs 139
 
140
 
141
    def get_linked_projects(self, sapnote):
142
        try:
143
            projects = self.sapnotes[sapnote]['projects']
144
        except Exception as error:
145
            projects = []
146
 
147
        return projects
148
 
149
 
150
    def get_linked_tasks(self, sapnote):
151
        try:
152
            tasks = self.sapnotes[sapnote]['tasks']
153
        except Exception as error:
154
            tasks = []
155
        self.log.debug("Tasks: %s" % tasks)
156
        return tasks
157
 
158
 
159
    def link_to_task(self, sapnotes, tasks):
44 t00mlabs 160
        if len(sapnotes) == 0:
161
            self.log.debug("No SAP Notes selected. No task will be linked.")
162
            return
163
 
42 t00mlabs 164
        db = self.get_service('DB')
165
        store = db.get_notes()
44 t00mlabs 166
 
42 t00mlabs 167
        for tid in sapnotes:
168
            sid = "0"*(10 - len(tid)) + tid
43 t00mlabs 169
            store[sid]['tasks'] = tasks
42 t00mlabs 170
            self.log.info("Linked SAP Note %s to task(s): %s" % (sid, tasks) )
4 t00mlabs 171
 
172
 
39 t00mlabs 173
    def set_bookmark(self, bag):
38 t00mlabs 174
        db = self.get_service('DB')
39 t00mlabs 175
        sapnotes = db.get_notes()
176
        mylist = []
45 t00mlabs 177
        for tid in bag:
178
            sid = "0"*(10 - len(tid)) + tid
39 t00mlabs 179
            sapnotes[sid]['bookmark'] = True
180
            mylist.append(sapnotes[sid])
181
            self.log.info("SAP Note %s bookmarked" % sid)
182
        db.add_list(mylist)
4 t00mlabs 183
 
184
 
39 t00mlabs 185
    def set_no_bookmark(self, bag):
38 t00mlabs 186
        db = self.get_service('DB')
39 t00mlabs 187
        sapnotes = db.get_notes()
188
        mylist = []
45 t00mlabs 189
        for tid in bag:
190
            sid = "0"*(10 - len(tid)) + tid
39 t00mlabs 191
            sapnotes[sid]['bookmark'] = False
192
            mylist.append(sapnotes[sid])
193
            self.log.info("SAP Note %s unbookmarked" % sid)
194
        db.add_list(mylist)
4 t00mlabs 195
 
196
 
197
    def is_bookmark(self, sapnote):
198
        try:
199
            return self.sapnotes[sapnote]['bookmark']
200
        except:
201
            return False
202
 
203
 
204
    def run(self):
34 t00mlabs 205
        db = self.get_service('DB')
206
        db.load_notes()
207
        db.build_stats()