<commit_before>from osv import osv, fields
import time
import datetime
import xmlrpclib
import netsvc
import urllib2
import base64
from base_external_referentials import external_osv
from tools.translate import _

class Connection(object):
    def __init__(self, location, username, password, debug=False):
        #Append / if not there
        if not location[-1] == '/':
            location += '/' 
        self.corelocation = location
        self.location = location + "index.php/api/xmlrpc"
        self.username = username
        self.password = password
        self.debug = debug
        self.result = {}
        self.logger = netsvc.Logger()
        
    def try_connect(self):
        self.session = self.ser.login(self.username, self.password)
        if self.debug:
            self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_INFO, _("Login Successful"))
        return True
    
    def connect(self):
        if not self.location[-1] == '/':
            self.location += '/'
        if self.debug:
            self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_INFO, _("Attempting connection with Settings:%s,%s,%s") % (self.location, self.username, self.password))
        self.ser = xmlrpclib.ServerProxy(self.location)
        try:
            return self.try_connect()
        except Exception, e:
            self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_WARNING, _("Webservice Failure, sleeping 1 second before next attempt"))
            time.sleep(1)
            try:
                return self.try_connect()
            except Exception, e:
                self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_WARNING, _("Webservice Failure, sleeping 3 second before next attempt"))
                time.sleep(3)
                try:
                    return self.try_connect()
                except Exception, e:
                    self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_WARNING, _("Webservice Failure, sleeping 6 second before next attempt"))
                    time.sleep(6)
                    try:
                        return self.try_connect()
                    except Exception, e:
                        self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_ERROR, _("Error in connecting") % (e))
                        raise
            
    def try_call(self, method, arguments):
        if self.debug:
            self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_INFO, _("Calling Method:%s,Arguments:%s") % (method, arguments))
        res = self.ser.call(self.session, method, arguments)
        if self.debug:
            self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_INFO, _("Query Returned:%s") % (res))
        return res
        
    
    def call(self, method, *arguments): 
        if arguments:
            arguments = list(arguments)[0]
        else:
            arguments = []
        try:
            return self.try_call(method, arguments)
        except Exception, e:
            self.logger.notifyChannel(_("Magento Call"), netsvc.LOG_WARNING, _("Webservice Failure, sleeping 1 second before next attempt"))
            time.sleep(1)
            try:
                return self.try_call(method, arguments)
            except Exception, e:
                self.logger.notifyChannel(_("Magento Call"), netsvc.LOG_WARNING, _("Webservice Failure, sleeping 3 seconds before next attempt"))
                time.sleep(3)
                try:
                    return self.try_call(method, arguments)
                except Exception, e:
                    self.logger.notifyChannel(_("Magento Call"), netsvc.LOG_WARNING, _("Webservice Failure, sleeping 6 seconds before next attempt"))
                    time.sleep(6)
                    try:
                        return self.try_call(method, arguments)
                    except Exception, e:
                        self.logger.notifyChannel(_("Magento Call"), netsvc.LOG_ERROR, _("Method: %s\nArguments:%s\nError:%s") % (method, arguments, e))
                        raise
    
    def fetch_image(self, imgloc):
        full_loc = self.corelocation + imgloc
        try:
            img = urllib2.urlopen(full_loc)
            return base64.b64encode(img.read())
        except Exception, e:
            pass

class magerp_osv(external_osv.external_osv):
    _MAGE_FIELD = 'magento_id'
    _MAGE_P_KEY = False
    _LIST_METHOD = False
    _GET_METHOD = False
    _CREATE_METHOD = False
    _UPDATE_METHOD = False
    _DELETE_METHOD = False
    _mapping = {}
    DEBUG = False<commit_msg>[IMP] much better loggin of Magento login and WS execution; factored code<commit_after>from osv import osv, fields
import time
import datetime
import xmlrpclib
import netsvc
import urllib2
import base64
from base_external_referentials import external_osv
from tools.translate import _

class Connection(object):
    def __init__(self, location, username, password, debug=False):
        #Append / if not there
        if not location[-1] == '/':
            location += '/' 
        self.corelocation = location
        self.location = location + "index.php/api/xmlrpc"
        self.username = username
        self.password = password
        self.debug = debug
        self.result = {}
        self.logger = netsvc.Logger()

    
    def connect(self):
        if not self.location[-1] == '/':
            self.location += '/'
        if self.debug:
            self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_INFO, _("Attempting connection with Settings:%s,%s,%s") % (self.location, self.username, self.password))
        self.ser = xmlrpclib.ServerProxy(self.location)
        for sleep_time in [1, 3, 6]:
            try:
                self.session = self.ser.login(self.username, self.password)
                if self.debug:
                    self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_INFO, _("Login Successful"))
                return True
            except Exception, e:
                self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_ERROR, _("Error in connecting:%s") % (e))
                self.logger.notifyChannel(_("Magento Call"), netsvc.LOG_WARNING, _("Webservice Failure, sleeping %s second before next attempt") % (sleep_time))
                time.sleep(sleep_time)
        raise

    
    def call(self, method, *arguments): 
        if arguments:
            arguments = list(arguments)[0]
        else:
            arguments = []
        for sleep_time in [1, 3, 6]:
            try:
                if self.debug:
                    self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_INFO, _("Calling Method:%s,Arguments:%s") % (method, arguments))
                res = self.ser.call(self.session, method, arguments)
                if self.debug:
                    self.logger.notifyChannel(_("Magento Connection"), netsvc.LOG_INFO, _("Query Returned:%s") % (res))
                return res
            except Exception, e:
                self.logger.notifyChannel(_("Magento Call"), netsvc.LOG_ERROR, _("Method: %s\nArguments:%s\nError:%s") % (method, arguments, e))
                self.logger.notifyChannel(_("Magento Call"), netsvc.LOG_WARNING, _("Webservice Failure, sleeping %s second before next attempt") % (sleep_time))
                time.sleep(sleep_time)
        raise


    def fetch_image(self, imgloc):
        full_loc = self.corelocation + imgloc
        try:
            img = urllib2.urlopen(full_loc)
            return base64.b64encode(img.read())
        except Exception, e:
            pass

class magerp_osv(external_osv.external_osv):
    _MAGE_FIELD = 'magento_id'
    _MAGE_P_KEY = False
    _LIST_METHOD = False
    _GET_METHOD = False
    _CREATE_METHOD = False
    _UPDATE_METHOD = False
    _DELETE_METHOD = False
    _mapping = {}
    DEBUG = False
{'fraction_non_alphanumeric': 0.10494999367008483, 'fraction_numerical': 0.0050639321433092795, 'mean_word_length': 2.132434575733545, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4573817', 'n_tokens_mistral': 2197, 'n_tokens_neox': 2122, 'n_words': 526}