Subversion Repositories basico

Rev

Rev 180 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: sap.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: SAP service

import time
import traceback
from shutil import which
# ~ from datetime.datetime import now

from .service import Service
from .env import LPATH

# Default settings for SAP module
LOGIN_PAGE_URL = "https://accounts.sap.com"
LOGOUT_PAGE_URL = "https://accounts.sap.com/ui/logout"
ODATA_NOTE_URL = "https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='%s',Version='0',Language='E')"
ODATA_NOTE_URL_LONGTEXT = "https://launchpad.support.sap.com/services/odata/svt/snogwscorr/TrunkSet(SapNotesNumber='%s',Version='0',Language='E')?$expand=LongText"
SAP_NOTE_URL = "https://launchpad.support.sap.com/#/notes/%s"
SAP_NOTE_URL_PDF = "https://launchpad.support.sap.com/services/pdf/notes/%s/E"
TIMEOUT = 10

DSTART = None
DEND = None
DYNTMOUT = 0
AVERAGE = 0

"""
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/RefBy"
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/RefTo"
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/CorrIns"
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/Patch"
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/Sp"
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/SoftCom"
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/Attach"
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/LongText"
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/Languages"
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/SideCau"
<link href="TrunkSet(SapNotesNumber='0002258035',Version='4',Language='E')/SideSol"

"""



class SAP(Service):
    def initialize(self):
        '''
        Setup AppLogic Service
        '''

        self.settings = self.get_service('Settings')
        self.__init_config_section()


    def __init_config_section(self):
        settings = self.settings.load()
        settings[self.section]
        try:
            settings[self.section]['LOGIN_PAGE_URL']
        except:
            settings[self.section]['LOGIN_PAGE_URL'] = LOGIN_PAGE_URL

        try:
            settings[self.section]['LOGOUT_PAGE_URL']
        except:
            settings[self.section]['LOGOUT_PAGE_URL'] = LOGOUT_PAGE_URL

        try:
            settings[self.section]['ODATA_NOTE_URL']
        except:
            settings[self.section]['ODATA_NOTE_URL'] = ODATA_NOTE_URL

        try:
            settings[self.section]['ODATA_NOTE_URL_LONGTEXT']
        except:
            settings[self.section]['ODATA_NOTE_URL_LONGTEXT'] = ODATA_NOTE_URL_LONGTEXT

        try:
            settings[self.section]['SAP_NOTE_URL']
        except:
            settings[self.section]['SAP_NOTE_URL'] = SAP_NOTE_URL

        try:
            settings[self.section]['SAP_NOTE_URL_PDF']
        except:
            settings[self.section]['SAP_NOTE_URL_PDF'] = SAP_NOTE_URL_PDF

        try:
            settings[self.section]['TIMEOUT']
        except:
            settings[self.section]['TIMEOUT'] = TIMEOUT

        self.settings.save(settings)


    def analyze_sapnote_metadata(self, sid, content):
        '''
        Get metadata details from SAP Note
        '''

        try:
            utils = self.get_service('Utils')
            f = utils.feedparser_parse(content)
            sid = f.entries[0].d_sapnotesnumber
            sapnote = {}
            sapnote['id'] = sid
            sapnote['componentkey'] = f.entries[0].d_componentkey
            comptxt = f.entries[0].d_componenttext
            if comptxt == "Please use note 1433157 for finding the right component":
                comptxt = ""
            sapnote['componenttxt'] = comptxt
            sapnote['category'] = f.entries[0].d_category_detail['value']
            sapnote['language'] = f.entries[0].d_languagetext_detail['value']
            sapnote['title'] = f.entries[0].d_title_detail['value']
            sapnote['priority'] = f.entries[0].d_priority_detail['value']
            sapnote['releaseon'] = f.entries[0].d_releasedon
            sapnote['type'] = f.entries[0].d_type_detail['value']
            sapnote['version'] = f.entries[0].d_version_detail['value']
            sapnote['feedupdate'] = f.entries[0].updated
            sapnote['bookmark'] = False
            self.debug ("\tSAP Note %s analyzed successfully" % sid)
        except Exception as error:
            sapnote = {}
            self.log.error("\tError while analyzing data for SAP Note %s" % sid)

        return sapnote


    def analyze_sapnote_metadata_details(self, sid, content):
        '''
        Get metadata details from SAP Note
        '''

        try:
            utils = self.get_service('Utils')
            f = utils.feedparser_parse(content)
            sid = f.entries[0].d_sapnotesnumber
            sapnote = {}
            sapnote['id'] = sid
            sapnote['componentkey'] = f.entries[0].d_componentkey
            comptxt = f.entries[0].d_componenttext
            if comptxt == "Please use note 1433157 for finding the right component":
                comptxt = ""
            sapnote['componenttxt'] = comptxt
            sapnote['category'] = f.entries[0].d_category_detail['value']
            sapnote['language'] = f.entries[0].d_languagetext_detail['value']
            sapnote['title'] = f.entries[0].d_title_detail['value']
            sapnote['priority'] = f.entries[0].d_priority_detail['value']
            sapnote['releaseon'] = f.entries[0].d_releasedon
            sapnote['type'] = f.entries[0].d_type_detail['value']
            sapnote['version'] = f.entries[0].d_version_detail['value']
            sapnote['feedupdate'] = f.entries[0].updated
            sapnote['bookmark'] = False
            self.debug ("\tSAP Note %s analyzed successfully" % sid)
        except Exception as error:
            sapnote = {}
            self.log.error("\tError while analyzing data for SAP Note %s" % sid)

        return sapnote

    def fetch(self, driver, sid):
        db = self.get_service('DB')
        valid = False

        if not db.is_stored(sid):
            self.debug("%3d/%3d - SAP Note %s must be downloaded" % (self.notes_fetched+1, self.notes_total, sid))
            # ~ DSTART = datetime.now()
            content = self.download(driver, sid)
            if len(content) > 0:
                self.debug("%3d/%3d - SAP Note %s fetched" % (self.notes_fetched+1, self.notes_total, sid))
            else:
                self.debug("%3d/%3d - SAP Note %s not feched" % (self.notes_fetched+1, self.notes_total, sid))
        else:
            self.debug("%3d/%3d - SAP Note %s will be analyzed again" % (self.notes_fetched+1, self.notes_total, sid))
            content = db.get_sapnote_content(sid)

        self.fetched()

        sapnote = self.analyze_sapnote_metadata_details(sid, content)
        if len(sapnote) > 0:
            db = self.get_service('DB')
            db.add(sapnote)
            db.store(sid, content)
            valid = True
            # ~ DEND = datetime.now()
            # ~ DYNTMOUT = DEND - DSTART
            # ~ self.debug(DYNTMOUT.seconds)
        return valid, sid


    def start_fetching(self, total):
        self.notes_fetched = 0
        self.notes_total = total


    def fetched(self):
        self.notes_fetched += 1


    def stop_fetching(self):
        self.notes_fetched = 0
        self.notes_total = 0

    def browse(self, sid):
        webdriver = self.get_service('Driver')
        driver = webdriver.open()
        url = SAP_NOTE_URL % sid
        webdriver.load(driver, url)


    def download(self, driver, sapnote=None):
        try:
            webdriver = self.get_service('Driver')
            ODATA_NOTE_URL = self.settings.get('SAP', 'ODATA_NOTE_URL')
            timeout = self.settings.get('SAP', 'TIMEOUT')
            self.debug("Downloading SAP Note %s" % sapnote)
            browser = webdriver.load(driver, ODATA_NOTE_URL % sapnote)
            time.sleep(timeout)
            content = browser.page_source
            fsn = LPATH['CACHE'] + sapnote + '.xml'
            with open(fsn, 'w') as fxml:
                fxml.write(content)
        except Exception as error:
            self.log.error(error)
            content = ''

        return content


    def set_bookmark(self, bag):
        db = self.get_service('DB')
        sapnotes = db.get_notes()
        mylist = []
        for tid in bag:
            sid = "0"*(10 - len(tid)) + tid
            sapnotes[sid]['bookmark'] = True
            mylist.append(sapnotes[sid])
            self.debug("SAP Note %s bookmarked" % sid)
        db.add_list(mylist)


    def set_no_bookmark(self, bag):
        db = self.get_service('DB')
        sapnotes = db.get_notes()
        mylist = []
        for tid in bag:
            sid = "0"*(10 - len(tid)) + tid
            sapnotes[sid]['bookmark'] = False
            mylist.append(sapnotes[sid])
            self.debug("SAP Note %s unbookmarked" % sid)
        db.add_list(mylist)


    def is_bookmark(self, sapnote):
        try:
            return self.sapnotes[sapnote]['bookmark']
        except:
            return False


    def run(self):
        db = self.get_service('DB')
        db.load_notes()
        db.build_stats()