repoapi.py
author Rémi Cardona <remi.cardona@logilab.fr>
Fri, 18 Sep 2015 11:54:12 +0200
changeset 10706 b261d90149d0
parent 10688 fa29f3628a1b
permissions -rw-r--r--
[server] Port BFSS to py3k The BFSS API changes in python 3: * 'defaultdir' MUST be a unicode object * 'fsencoding' MUST NOT be set In python 2, fsencoding handles both the encoding of file paths on the file system (utf-8 by default, but the system may actually be using something else) and the encoding of file paths that will be stored in the database. So in python 3, we wipe the slate clean: * rely on sys.getfilesystemencoding() to convert unicode objects to bytes * always encode paths to utf-8 for storage in the database Caveat emptor / here be dragons: * sys.getfilesystemencoding() depends on the current locale, which therefore MUST be set properly * when migrating an existing instance from py2 to py3, one MAY need to reencode file paths stored in the database
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
10688
fa29f3628a1b [py3k] use six.add_metaclass
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10568
diff changeset
    21
fa29f3628a1b [py3k] use six.add_metaclass
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10568
diff changeset
    22
from six import add_metaclass
fa29f3628a1b [py3k] use six.add_metaclass
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10568
diff changeset
    23
10354
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10236
diff changeset
    24
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
    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
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
    27
from cubicweb import AuthenticationError
10354
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10236
diff changeset
    28
from cubicweb.server.session import Connection
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10236
diff changeset
    29
9045
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
10235
684215aca046 Remove remote repository-access-through-pyro support
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10087
diff changeset
    38
    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
    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
    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
    41
        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
    42
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
    43
    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
    44
    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
    45
9061
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9060
diff changeset
    46
def connect(repo, login, **kwargs):
10356
a009a31fb1ea [connection] eliminate ClientConnection leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
    47
    """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
    48
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9060
diff changeset
    49
    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
    50
    sessionid = repo.connect(login, **kwargs)
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9060
diff changeset
    51
    session = repo._get_session(sessionid)
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9060
diff changeset
    52
    # 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
    53
    # 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
    54
    return Connection(session)
9061
e86fdab3d296 [repoapi] add a connect function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9060
diff changeset
    55
9062
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    56
def anonymous_cnx(repo):
10356
a009a31fb1ea [connection] eliminate ClientConnection leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
    57
    """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
    58
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    59
    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
    60
    """
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    61
    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
    62
    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
    63
        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
    64
    anon_login, anon_password = anoninfo
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    65
    # use vreg's repository cache
fa5bc8aef7ed [repoapi] add an anonymous_cnx function
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9061
diff changeset
    66
    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
    67
9052
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    68
10688
fa29f3628a1b [py3k] use six.add_metaclass
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10568
diff changeset
    69
@add_metaclass(class_deprecated)
10354
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10236
diff changeset
    70
class ClientConnection(Connection):
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10236
diff changeset
    71
    __deprecation_warning__ = '[3.20] %(cls)s is deprecated, use Connection instead'