dbapi.py
branchstable
changeset 8463 a964c40adbe3
parent 8404 3dcb117fb3b0
child 8537 e30d0a7f0087
child 8571 7e5246889148
equal deleted inserted replaced
8461:8af7c6d86efb 8463:a964c40adbe3
    56     """some monkey patching necessary when an application has to deal with
    56     """some monkey patching necessary when an application has to deal with
    57     several connections to different repositories. It tries to hide buggy class
    57     several connections to different repositories. It tries to hide buggy class
    58     attributes since classes are not designed to be shared among multiple
    58     attributes since classes are not designed to be shared among multiple
    59     registries.
    59     registries.
    60     """
    60     """
    61     defaultcls = cwvreg.VRegistry.REGISTRY_FACTORY[None]
    61     defaultcls = cwvreg.CWRegistryStore.REGISTRY_FACTORY[None]
    62 
    62 
    63     etypescls = cwvreg.VRegistry.REGISTRY_FACTORY['etypes']
    63     etypescls = cwvreg.CWRegistryStore.REGISTRY_FACTORY['etypes']
    64     orig_etype_class = etypescls.orig_etype_class = etypescls.etype_class
    64     orig_etype_class = etypescls.orig_etype_class = etypescls.etype_class
    65     @monkeypatch(defaultcls)
    65     @monkeypatch(defaultcls)
    66     def etype_class(self, etype):
    66     def etype_class(self, etype):
    67         """return an entity class for the given entity type.
    67         """return an entity class for the given entity type.
    68         Try to find out a specific class for this kind of entity or
    68         Try to find out a specific class for this kind of entity or
    73             return usercls
    73             return usercls
    74         usercls.e_schema = self.schema.eschema(etype)
    74         usercls.e_schema = self.schema.eschema(etype)
    75         return usercls
    75         return usercls
    76 
    76 
    77 def multiple_connections_unfix():
    77 def multiple_connections_unfix():
    78     etypescls = cwvreg.VRegistry.REGISTRY_FACTORY['etypes']
    78     etypescls = cwvreg.CWRegistryStore.REGISTRY_FACTORY['etypes']
    79     etypescls.etype_class = etypescls.orig_etype_class
    79     etypescls.etype_class = etypescls.orig_etype_class
    80 
    80 
    81 
    81 
    82 class ConnectionProperties(object):
    82 class ConnectionProperties(object):
    83     def __init__(self, cnxtype=None, lang=None, close=True, log=False):
    83     def __init__(self, cnxtype=None, lang=None, close=True, log=False):
    91     """get a proxy object to the CubicWeb repository, using a specific RPC method.
    91     """get a proxy object to the CubicWeb repository, using a specific RPC method.
    92 
    92 
    93     Only 'in-memory' and 'pyro' are supported for now. Either vreg or config
    93     Only 'in-memory' and 'pyro' are supported for now. Either vreg or config
    94     argument should be given
    94     argument should be given
    95     """
    95     """
    96     assert method in ('pyro', 'inmemory')
    96     assert method in ('pyro', 'inmemory', 'zmq')
    97     assert vreg or config
    97     assert vreg or config
    98     if vreg and not config:
    98     if vreg and not config:
    99         config = vreg.config
    99         config = vreg.config
   100     if method == 'inmemory':
   100     if method == 'inmemory':
   101         # get local access to the repository
   101         # get local access to the repository
   102         from cubicweb.server.repository import Repository
   102         from cubicweb.server.repository import Repository
   103         return Repository(config, vreg=vreg)
   103         from cubicweb.server.utils import TasksManager
       
   104         return Repository(config, TasksManager(), vreg=vreg)
       
   105     elif method == 'zmq':
       
   106         from cubicweb.zmqclient import ZMQRepositoryClient
       
   107         return ZMQRepositoryClient(config, vreg=vreg)
   104     else: # method == 'pyro'
   108     else: # method == 'pyro'
   105         # resolve the Pyro object
   109         # resolve the Pyro object
   106         from logilab.common.pyro_ext import ns_get_proxy, get_proxy
   110         from logilab.common.pyro_ext import ns_get_proxy, get_proxy
   107         pyroid = database or config['pyro-instance-id'] or config.appid
   111         pyroid = database or config['pyro-instance-id'] or config.appid
   108         try:
   112         try:
   143 
   147 
   144     :login:
   148     :login:
   145       the user login to use to authenticate.
   149       the user login to use to authenticate.
   146 
   150 
   147     :host:
   151     :host:
   148       the pyro nameserver host. Will be detected using broadcast query if
   152       - pyro: nameserver host. Will be detected using broadcast query if unspecified
   149       unspecified.
   153       - zmq: repository host socket address
   150 
   154 
   151     :group:
   155     :group:
   152       the instance's pyro nameserver group. You don't have to specify it unless
   156       the instance's pyro nameserver group. You don't have to specify it unless
   153       tweaked in instance's configuration.
   157       tweaked in instance's configuration.
   154 
   158 
   181         config = cwconfig.CubicWebNoAppConfiguration()
   185         config = cwconfig.CubicWebNoAppConfiguration()
   182         if host:
   186         if host:
   183             config.global_set_option('pyro-ns-host', host)
   187             config.global_set_option('pyro-ns-host', host)
   184         if group:
   188         if group:
   185             config.global_set_option('pyro-ns-group', group)
   189             config.global_set_option('pyro-ns-group', group)
       
   190     elif method == 'zmq':
       
   191         config = cwconfig.CubicWebNoAppConfiguration()
   186     else:
   192     else:
   187         assert database
   193         assert database
   188         config = cwconfig.instance_configuration(database)
   194         config = cwconfig.instance_configuration(database)
   189     repo = get_repository(method, database, config=config)
   195     repo = get_repository(method, database, config=config)
   190     if method == 'inmemory':
   196     if method == 'inmemory':
   191         vreg = repo.vreg
   197         vreg = repo.vreg
   192     elif setvreg:
   198     elif setvreg:
   193         if mulcnx:
   199         if mulcnx:
   194             multiple_connections_fix()
   200             multiple_connections_fix()
   195         vreg = cwvreg.CubicWebVRegistry(config, initlog=initlog)
   201         vreg = cwvreg.CWRegistryStore(config, initlog=initlog)
   196         schema = repo.get_schema()
   202         schema = repo.get_schema()
   197         for oldetype, newetype in ETYPE_NAME_MAP.items():
   203         for oldetype, newetype in ETYPE_NAME_MAP.items():
   198             if oldetype in schema:
   204             if oldetype in schema:
   199                 print 'aliasing', newetype, 'to', oldetype
   205                 print 'aliasing', newetype, 'to', oldetype
   200                 schema._entities[newetype] = schema._entities[oldetype]
   206                 schema._entities[newetype] = schema._entities[oldetype]
   205     cnx.vreg = vreg
   211     cnx.vreg = vreg
   206     return cnx
   212     return cnx
   207 
   213 
   208 def in_memory_repo(config):
   214 def in_memory_repo(config):
   209     """Return and in_memory Repository object from a config (or vreg)"""
   215     """Return and in_memory Repository object from a config (or vreg)"""
   210     if isinstance(config, cwvreg.CubicWebVRegistry):
   216     if isinstance(config, cwvreg.CWRegistryStore):
   211         vreg = config
   217         vreg = config
   212         config = None
   218         config = None
   213     else:
   219     else:
   214         vreg = None
   220         vreg = None
   215     # get local access to the repository
   221     # get local access to the repository
   278 
   284 
   279 class DBAPIRequest(RequestSessionBase):
   285 class DBAPIRequest(RequestSessionBase):
   280 
   286 
   281     def __init__(self, vreg, session=None):
   287     def __init__(self, vreg, session=None):
   282         super(DBAPIRequest, self).__init__(vreg)
   288         super(DBAPIRequest, self).__init__(vreg)
       
   289         #: 'language' => translation_function() mapping
   283         try:
   290         try:
   284             # no vreg or config which doesn't handle translations
   291             # no vreg or config which doesn't handle translations
   285             self.translations = vreg.config.translations
   292             self.translations = vreg.config.translations
   286         except AttributeError:
   293         except AttributeError:
   287             self.translations = {}
   294             self.translations = {}
       
   295         #: Request language identifier eg: 'en'
       
   296         self.lang = None
   288         self.set_default_language(vreg)
   297         self.set_default_language(vreg)
   289         # cache entities built during the request
   298         #: cache entities built during the request
   290         self._eid_cache = {}
   299         self._eid_cache = {}
   291         if session is not None:
   300         if session is not None:
   292             self.set_session(session)
   301             self.set_session(session)
   293         else:
   302         else:
   294             # these args are initialized after a connection is
   303             # these args are initialized after a connection is
   333             # this occurs usually during test execution
   342             # this occurs usually during test execution
   334             self._ = self.__ = unicode
   343             self._ = self.__ = unicode
   335             self.pgettext = lambda x, y: unicode(y)
   344             self.pgettext = lambda x, y: unicode(y)
   336         self.debug('request default language: %s', self.lang)
   345         self.debug('request default language: %s', self.lang)
   337 
   346 
       
   347     # server-side service call #################################################
       
   348 
       
   349     def call_service(self, regid, async=False, **kwargs):
       
   350         return self.cnx.call_service(regid, async, **kwargs)
       
   351 
   338     # entities cache management ###############################################
   352     # entities cache management ###############################################
   339 
   353 
   340     def entity_cache(self, eid):
   354     def entity_cache(self, eid):
   341         return self._eid_cache[eid]
   355         return self._eid_cache[eid]
   342 
   356 
   553         if self._closed is None and self._close_on_del:
   567         if self._closed is None and self._close_on_del:
   554             try:
   568             try:
   555                 self.close()
   569                 self.close()
   556             except Exception:
   570             except Exception:
   557                 pass
   571                 pass
       
   572 
       
   573     # server-side service call #################################################
       
   574 
       
   575     @check_not_closed
       
   576     def call_service(self, regid, async=False, **kwargs):
       
   577         return self._repo.call_service(self.sessionid, regid, async, **kwargs)
   558 
   578 
   559     # connection initialization methods ########################################
   579     # connection initialization methods ########################################
   560 
   580 
   561     def load_appobjects(self, cubes=_MARKER, subpath=None, expand=True):
   581     def load_appobjects(self, cubes=_MARKER, subpath=None, expand=True):
   562         config = self.vreg.config
   582         config = self.vreg.config