Subversion Repositories basico

Rev

Rev 4 | 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"
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"
74 t00mlabs 19
SAP_NOTE_URL_PDF = "https://launchpad.support.sap.com/services/pdf/notes/%s/E"
4 t00mlabs 20
 
21
 
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):
74 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)
74 t00mlabs 37
                settings = prefs.get_default_settings()
4 t00mlabs 38
                self.config[self.section] = settings
74 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
 
74 t00mlabs 45
    def analyze_sapnote_metadata(self, sid, content):
46
        '''
47
        Get metadata details from SAP Note
48
        '''
49
        #~ self.log.debug("\t%s" % content)
4 t00mlabs 50
        try:
74 t00mlabs 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)
4 t00mlabs 71
        except Exception as error:
74 t00mlabs 72
            sapnote = {}
73
            self.log.error("\tError while analyzing data for SAP Note %s" % sid)
74
            #~ self.log.error("\t%s" % error)
4 t00mlabs 75
 
74 t00mlabs 76
        return sapnote
4 t00mlabs 77
 
78
 
74 t00mlabs 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))
4 t00mlabs 83
 
74 t00mlabs 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))
4 t00mlabs 89
            else:
74 t00mlabs 90
                self.log.debug("%3d/%3d - SAP Note %s not feched" % (self.notes_fetched+1, self.notes_total, sid))
4 t00mlabs 91
        else:
74 t00mlabs 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)
4 t00mlabs 94
 
74 t00mlabs 95
        self.fetched()
4 t00mlabs 96
 
74 t00mlabs 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
4 t00mlabs 103
 
74 t00mlabs 104
        return valid, sid
4 t00mlabs 105
 
106
 
74 t00mlabs 107
    def start_fetching(self, total):
108
        self.notes_fetched = 0
109
        self.notes_total = total
4 t00mlabs 110
 
111
 
74 t00mlabs 112
    def fetched(self):
113
        self.notes_fetched += 1
4 t00mlabs 114
 
115
 
74 t00mlabs 116
    def stop_fetching(self):
117
        self.notes_fetched = 0
118
        self.notes_total = 0
4 t00mlabs 119
 
120
 
74 t00mlabs 121
    def download(self, sapnote=None):
4 t00mlabs 122
        try:
74 t00mlabs 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 = ''
4 t00mlabs 133
        except Exception as error:
74 t00mlabs 134
            #~ self.log.error("\tSAP Note %s coud not be downloaded" % sapnote)
135
            self.log.error(error)
136
            content = ''
4 t00mlabs 137
 
74 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):
74 t00mlabs 160
        if len(sapnotes) == 0:
161
            self.log.debug("No SAP Notes selected. No task will be linked.")
162
            return
4 t00mlabs 163
 
74 t00mlabs 164
        db = self.get_service('DB')
165
        store = db.get_notes()
4 t00mlabs 166
 
74 t00mlabs 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) )
4 t00mlabs 171
 
172
 
74 t00mlabs 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)
4 t00mlabs 183
 
184
 
74 t00mlabs 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)
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):
74 t00mlabs 205
        db = self.get_service('DB')
206
        db.load_notes()
207
        db.build_stats()