repoapi.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Wed, 09 Jul 2014 16:32:14 +0200
changeset 10568 3914388b2d0f
parent 10356 a009a31fb1ea
child 10688 fa29f3628a1b
permissions -rw-r--r--
[repoapi, cwconfig] give a convenience method to get a repository from a config This is a very common need. The private repoapi._get_inmemory_repo becomes config.repository(...)
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
"""
10568
3914388b2d0f [repoapi, cwconfig] give a convenience method to get a repository from a config
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10356
diff changeset
    20
from warnings import warn
10354
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10236
diff changeset
    21
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
    22
9045
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    23
from cubicweb.utils import parse_repo_uri
10568
3914388b2d0f [repoapi, cwconfig] give a convenience method to get a repository from a config
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10356
diff changeset
    24
from cubicweb import AuthenticationError
10354
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10236
diff changeset
    25
from cubicweb.server.session import Connection
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10236
diff changeset
    26
9045
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    27
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    28
### public API ######################################################
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
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
    31
    """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
    32
    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
    33
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    34
    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
    35
    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
    36
    """
10568
3914388b2d0f [repoapi, cwconfig] give a convenience method to get a repository from a config
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10356
diff changeset
    37
    if uri is not None:
3914388b2d0f [repoapi, cwconfig] give a convenience method to get a repository from a config
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10356
diff changeset
    38
        warn('[3.22] get_repository only wants a config')
9045
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    39
10568
3914388b2d0f [repoapi, cwconfig] give a convenience method to get a repository from a config
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10356
diff changeset
    40
    assert config is not None, 'get_repository(config=config)'
3914388b2d0f [repoapi, cwconfig] give a convenience method to get a repository from a config
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10356
diff changeset
    41
    return config.repository(vreg)
9045
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    42
9061
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9060
diff changeset
    43
def connect(repo, login, **kwargs):
10356
a009a31fb1ea [connection] eliminate ClientConnection leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
    44
    """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
    45
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9060
diff changeset
    46
    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
    47
    sessionid = repo.connect(login, **kwargs)
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9060
diff changeset
    48
    session = repo._get_session(sessionid)
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9060
diff changeset
    49
    # 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
    50
    # 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
    51
    return Connection(session)
9061
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9060
diff changeset
    52
9062
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    53
def anonymous_cnx(repo):
10356
a009a31fb1ea [connection] eliminate ClientConnection leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
    54
    """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
    55
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    56
    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
    57
    """
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    58
    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
    59
    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
    60
        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
    61
    anon_login, anon_password = anoninfo
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    62
    # use vreg's repository cache
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    63
    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
    64
9052
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    65
10354
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10236
diff changeset
    66
class ClientConnection(Connection):
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10236
diff changeset
    67
    __metaclass__ = class_deprecated
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10236
diff changeset
    68
    __deprecation_warning__ = '[3.20] %(cls)s is deprecated, use Connection instead'