<commit_before>#!/usr/bin/python
import datetime
import dateutil.relativedelta
import logging
import time
import openerplib

import nonliterals

_logger = logging.getLogger(__name__)
#----------------------------------------------------------
# OpenERPSession RPC openerp backend access
#----------------------------------------------------------
class OpenERPSession(object):
    """
    An OpenERP RPC session, a given user can own multiple such sessions
    in a web session.

    .. attribute:: context
    
        The session context, a ``dict``. Can be reloaded by calling
        :meth:`openerpweb.openerpweb.OpenERPSession.get_context`

    .. attribute:: domains_store

        A ``dict`` matching domain keys to evaluable (but non-literal) domains.

        Used to store references to non-literal domains which need to be
        round-tripped to the client browser.
    """
    def __init__(self):
        self.config = None
        self._db = False
        self._uid = False
        self._login = False
        self._password = False
        self.context = {}
        self.contexts_store = {}
        self.domains_store = {}
        self.jsonp_requests = {}     # FIXME use a LRU
        
    def __getstate__(self):
        state = dict(self.__dict__)
        if "config" in state:
            del state['config']
        return state

    def openerp_entreprise(self):
        if not self._uid:
            return False
        else:
            return self.model('publisher_warranty.contract').status()['status'] == 'full'

    def build_connection(self):
        conn = openerplib.Connection(self.config.connector, database=self._db, login=self._login,
                   user_id=self._uid, password=self._password)
        return conn

    def proxy(self, service):
        return self.build_connection().get_service(service)

    def bind(self, db, uid, login, password):
        self._db = db
        self._uid = uid
        self._login = login
        self._password = password

    def authenticate(self, db, login, password, env):
        # TODO use the openerplib API once it exposes authenticate()
        uid = self.proxy('common').authenticate(db, login, password, env)
        self.bind(db, uid, login, password)
        
        if uid: self.get_context()
        return uid

    def assert_valid(self, force=False):
        """
        Ensures this session is valid (logged into the openerp server)
        """
        self.build_connection().check_login(force)

    def execute(self, model, func, *l, **d):
        self.assert_valid()
        model = self.build_connection().get_model(model)
        r = getattr(model, func)(*l, **d)
        return r

    def exec_workflow(self, model, id, signal):
        self.assert_valid()
        r = self.proxy('object').exec_workflow(self._db, self._uid, self._password, model, signal, id)
        return r

    def model(self, model):
        """ Get an RPC proxy for the object ``model``, bound to this session.

        :param model: an OpenERP model name
        :type model: str
        :rtype: a model object
        """
        return self.build_connection().get_model(model)

    def get_context(self):
        """ Re-initializes the current user's session context (based on
        his preferences) by calling res.users.get_context() with the old
        context

        :returns: the new context
        """
        assert self._uid, "The user needs to be logged-in to initialize his context"
        self.context = self.model('res.users').context_get(self.context)
        self.context = self.context or {}

        return self.context

    @property
    def base_eval_context(self):
        """ Default evaluation context for the session.

        Used to evaluate contexts and domains.
        """<commit_msg>[imp] re used context retreival<commit_after>#!/usr/bin/python
import datetime
import dateutil.relativedelta
import logging
import time
import openerplib

import nonliterals

_logger = logging.getLogger(__name__)
#----------------------------------------------------------
# OpenERPSession RPC openerp backend access
#----------------------------------------------------------
class OpenERPSession(object):
    """
    An OpenERP RPC session, a given user can own multiple such sessions
    in a web session.

    .. attribute:: context
    
        The session context, a ``dict``. Can be reloaded by calling
        :meth:`openerpweb.openerpweb.OpenERPSession.get_context`

    .. attribute:: domains_store

        A ``dict`` matching domain keys to evaluable (but non-literal) domains.

        Used to store references to non-literal domains which need to be
        round-tripped to the client browser.
    """
    def __init__(self):
        self.config = None
        self._db = False
        self._uid = False
        self._login = False
        self._password = False
        self.context = {}
        self.contexts_store = {}
        self.domains_store = {}
        self.jsonp_requests = {}     # FIXME use a LRU
        
    def __getstate__(self):
        state = dict(self.__dict__)
        if "config" in state:
            del state['config']
        return state

    def openerp_entreprise(self):
        if not self._uid:
            return False
        else:
            return self.model('publisher_warranty.contract').status()['status'] == 'full'

    def build_connection(self):
        conn = openerplib.Connection(self.config.connector, database=self._db, login=self._login,
                   user_id=self._uid, password=self._password)
        return conn

    def proxy(self, service):
        return self.build_connection().get_service(service)

    def bind(self, db, uid, login, password):
        self._db = db
        self._uid = uid
        self._login = login
        self._password = password

    def authenticate(self, db, login, password, env):
        # TODO use the openerplib API once it exposes authenticate()
        uid = self.proxy('common').authenticate(db, login, password, env)
        self.bind(db, uid, login, password)
        
        if uid: self.get_context()
        return uid

    def assert_valid(self, force=False):
        """
        Ensures this session is valid (logged into the openerp server)
        """
        self.build_connection().check_login(force)

    def execute(self, model, func, *l, **d):
        self.assert_valid()
        model = self.build_connection().get_model(model)
        r = getattr(model, func)(*l, **d)
        return r

    def exec_workflow(self, model, id, signal):
        self.assert_valid()
        r = self.proxy('object').exec_workflow(self._db, self._uid, self._password, model, signal, id)
        return r

    def model(self, model):
        """ Get an RPC proxy for the object ``model``, bound to this session.

        :param model: an OpenERP model name
        :type model: str
        :rtype: a model object
        """
        return self.build_connection().get_model(model)

    def get_context(self):
        """ Re-initializes the current user's session context (based on
        his preferences) by calling res.users.get_context() with the old
        context

        :returns: the new context
        """
        assert self._uid, "The user needs to be logged-in to initialize his context"
        self.context = self.build_connection().get_user_context() or {}

        return self.context

    @property
    def base_eval_context(self):
        """ Default evaluation context for the session.

        Used to evaluate contexts and domains.
        """
{'fraction_non_alphanumeric': 0.12541297740187657, 'fraction_numerical': 0.0, 'mean_word_length': 2.1324503311258276, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '6172944', 'n_tokens_mistral': 2084, 'n_tokens_neox': 2010, 'n_words': 624}