repoapi.py
author Julien Cristau <julien.cristau@logilab.fr>
Mon, 18 May 2015 16:36:26 +0200
changeset 10438 f27a489994e8
parent 10356 a009a31fb1ea
child 10568 3914388b2d0f
permissions -rw-r--r--
[devtools] make test db name for postgresql process-specific In case we run two tests concurrently on the same cluster, better not use the same db name for both.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10235
684215aca046 Remove remote repository-access-through-pyro support
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10087
diff changeset
     1
# copyright 2013-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
9045
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
"""
10354
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10236
diff changeset
    20
from logilab.common.deprecation import class_deprecated
9125
9aaf8472bad0 [repoapi] deprecate dbapi compat method
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9116
diff changeset
    21
9045
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    22
from cubicweb.utils import parse_repo_uri
10354
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10236
diff changeset
    23
from cubicweb import ConnectionError, AuthenticationError
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10236
diff changeset
    24
from cubicweb.server.session import Connection
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10236
diff changeset
    25
9045
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    26
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    27
### 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
    28
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    29
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
    30
    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
    31
    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
    32
    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
    33
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    34
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    35
### public API ######################################################
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
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
    38
    """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
    39
    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
    40
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    41
    The returned repository may be an in-memory repository or a proxy object
10235
684215aca046 Remove remote repository-access-through-pyro support
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10087
diff changeset
    42
    using a specific RPC method, depending on the given URI.
9045
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    43
    """
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    44
    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
    45
        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
    46
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    47
    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
    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 == 'inmemory':
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    50
        # 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
    51
        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
    52
10236
ef3059a692cb Remove the remote repository-access-through-zmq support
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10235
diff changeset
    53
    raise ConnectionError('unknown protocol: `%s`' % protocol)
9045
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    54
9061
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9060
diff changeset
    55
def connect(repo, login, **kwargs):
10356
a009a31fb1ea [connection] eliminate ClientConnection leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
    56
    """Take credential and return associated Connection.
9061
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9060
diff changeset
    57
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9060
diff changeset
    58
    raise AuthenticationError if the credential are invalid."""
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9060
diff changeset
    59
    sessionid = repo.connect(login, **kwargs)
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9060
diff changeset
    60
    session = repo._get_session(sessionid)
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9060
diff changeset
    61
    # XXX the autoclose_session should probably be handle on the session directly
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9060
diff changeset
    62
    # this is something to consider once we have proper server side Connection.
10354
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10236
diff changeset
    63
    return Connection(session)
9061
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9060
diff changeset
    64
9062
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    65
def anonymous_cnx(repo):
10356
a009a31fb1ea [connection] eliminate ClientConnection leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
    66
    """return a Connection for Anonymous user.
9062
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    67
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    68
    raises an AuthenticationError if anonymous usage is not allowed
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    69
    """
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    70
    anoninfo = getattr(repo.config, 'anonymous_user', lambda: None)()
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    71
    if anoninfo is None: # no anonymous user
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    72
        raise AuthenticationError('anonymous access is not authorized')
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    73
    anon_login, anon_password = anoninfo
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    74
    # use vreg's repository cache
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    75
    return connect(repo, anon_login, password=anon_password)
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    76
9052
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    77
10354
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10236
diff changeset
    78
class ClientConnection(Connection):
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10236
diff changeset
    79
    __metaclass__ = class_deprecated
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10236
diff changeset
    80
    __deprecation_warning__ = '[3.20] %(cls)s is deprecated, use Connection instead'