repoapi.py
changeset 9045 5378a738f333
child 9052 4cba5f2cd57b
equal deleted inserted replaced
9044:cfec5cc46008 9045:5378a738f333
       
     1 # copyright 2013-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """Official API to access the content of a repository
       
    19 """
       
    20 from cubicweb.utils import parse_repo_uri
       
    21 from cubicweb import ConnectionError
       
    22 
       
    23 ### private function for specific method ############################
       
    24 
       
    25 def _get_inmemory_repo(config, vreg=None):
       
    26     from cubicweb.server.repository import Repository
       
    27     from cubicweb.server.utils import TasksManager
       
    28     return Repository(config, TasksManager(), vreg=vreg)
       
    29 
       
    30 
       
    31 ### public API ######################################################
       
    32 
       
    33 def get_repository(uri=None, config=None, vreg=None):
       
    34     """get a repository for the given URI or config/vregistry (in case we're
       
    35     loading the repository for a client, eg web server, configuration).
       
    36 
       
    37     The returned repository may be an in-memory repository or a proxy object
       
    38     using a specific RPC method, depending on the given URI (pyro or zmq).
       
    39     """
       
    40     if uri is None:
       
    41         return _get_inmemory_repo(config, vreg)
       
    42 
       
    43     protocol, hostport, appid = parse_repo_uri(uri)
       
    44 
       
    45     if protocol == 'inmemory':
       
    46         # me may have been called with a dummy 'inmemory://' uri ...
       
    47         return _get_inmemory_repo(config, vreg)
       
    48 
       
    49     if protocol == 'pyroloc':  # direct connection to the instance
       
    50         from logilab.common.pyro_ext import get_proxy
       
    51         uri = uri.replace('pyroloc', 'PYRO')
       
    52         return get_proxy(uri)
       
    53 
       
    54     if protocol == 'pyro':  # connection mediated through the pyro ns
       
    55         from logilab.common.pyro_ext import ns_get_proxy
       
    56         path = appid.strip('/')
       
    57         if not path:
       
    58             raise ConnectionError(
       
    59                 "can't find instance name in %s (expected to be the path component)"
       
    60                 % uri)
       
    61         if '.' in path:
       
    62             nsgroup, nsid = path.rsplit('.', 1)
       
    63         else:
       
    64             nsgroup = 'cubicweb'
       
    65             nsid = path
       
    66         return ns_get_proxy(nsid, defaultnsgroup=nsgroup, nshost=hostport)
       
    67 
       
    68     if protocol.startswith('zmqpickle-'):
       
    69         from cubicweb.zmqclient import ZMQRepositoryClient
       
    70         return ZMQRepositoryClient(uri)
       
    71     else:
       
    72         raise ConnectionError('unknown protocol: `%s`' % protocol)
       
    73