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