repoapi.py
author Pierre-Yves David <pierre-yves.david@logilab.fr>
Fri, 21 Jun 2013 12:12:58 +0200
changeset 9054 d7e78645dab6
parent 9053 862040061173
child 9055 2567b7d04e49
permissions -rw-r--r--
[client-connection] add a cursor() method for dbapi compatibility To ease transition from dbapi to repoapi we need the ClientConnection to be as compatible as possible with the dbapi. Adding this method goes in this direction. It'll get deprecated in the deprecation wave that will conclude the repoapi refactoring. 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
9052
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    21
from cubicweb import ConnectionError, ProgrammingError
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    22
from uuid import uuid4
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    23
from contextlib import contextmanager
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    24
from cubicweb.req import RequestSessionBase
9045
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    25
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    26
### 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
    27
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    28
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
    29
    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
    30
    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
    31
    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
    32
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
### public API ######################################################
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    35
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    36
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
    37
    """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
    38
    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
    39
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    40
    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
    41
    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
    42
    """
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    43
    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
    44
        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
    45
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    46
    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
    47
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    48
    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
    49
        # 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
    50
        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
    51
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    52
    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
    53
        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
    54
        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
    55
        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
    56
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    57
    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
    58
        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
    59
        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
    60
        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
    61
            raise ConnectionError(
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    62
                "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
    63
                % uri)
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    64
        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
    65
            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
    66
        else:
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    67
            nsgroup = 'cubicweb'
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    68
            nsid = path
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    69
        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
    70
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    71
    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
    72
        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
    73
        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
    74
    else:
5378a738f333 [repoapi] move get_repository function into a new repoapi module
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    75
        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
    76
9052
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    77
def _srv_cnx_func(name):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    78
    """Decorate ClientConnection method blindly forward to Connection
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    79
    THIS TRANSITIONAL PURPOSE
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    80
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    81
    will be dropped when we have standalone connection"""
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    82
    def proxy(clt_cnx, *args, **kwargs):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    83
        # the ``with`` dance is transitional. We do not have Standalone
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    84
        # Connection yet so we use this trick to unsure the session have the
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    85
        # proper cnx loaded. This can be simplified one we have Standalone
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    86
        # Connection object
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    87
        with clt_cnx._srv_cnx as cnx:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    88
            return getattr(cnx, name)(*args, **kwargs)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    89
    return proxy
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    90
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    91
class ClientConnection(RequestSessionBase):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    92
    """A Connection object to be used Client side.
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    93
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    94
    This object is aimed to be used client side (so potential communication
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    95
    with the repo through RTC) and aims to offer some compatibility with the
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    96
    cubicweb.dbapi.Connection interface.
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    97
    """
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    98
    # make exceptions available through the connection object
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
    99
    ProgrammingError = ProgrammingError
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   100
    # attributes that may be overriden per connection instance
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   101
    anonymous_connection = False # XXX really needed ?
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   102
    is_repo_in_memory = True # BC, always true
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   103
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   104
    def __init__(self, session):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   105
        self._session = session
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   106
        self._cnxid = None
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   107
        self._open = None
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   108
        self._web_request = False
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   109
        self.vreg = session.vreg
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   110
        self._set_user(session.user)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   111
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   112
    def __enter__(self):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   113
        assert self._open is None
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   114
        self._open = True
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   115
        self._cnxid = '%s-%s' % (self._session.id, uuid4().hex)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   116
        self._session.set_cnx(self._cnxid)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   117
        self._session._cnx.ctx_count += 1
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   118
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   119
    def __exit__(self, exc_type, exc_val, exc_tb):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   120
        self._open = False
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   121
        cnxid = self._cnxid
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   122
        self._cnxid = None
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   123
        self._session._cnx.ctx_count -= 1
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   124
        self._session.close_cnx(cnxid)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   125
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   126
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   127
    # begin silly BC
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   128
    @property
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   129
    def _closed(self):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   130
        return not self._open
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   131
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   132
    def close(self):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   133
        if self._open:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   134
            self.__exit__(None, None, None)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   135
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   136
    def __repr__(self):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   137
        if self.anonymous_connection:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   138
            return '<Connection %s (anonymous)>' % self._cnxid
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   139
        return '<Connection %s>' % self._cnxid
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   140
    # end silly BC
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   141
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   142
    @property
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   143
    @contextmanager
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   144
    def _srv_cnx(self):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   145
        """ensure that the session is locked to the right transaction
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   146
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   147
        TRANSITIONAL PURPOSE, This will be dropped once we use standalone
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   148
        session object"""
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   149
        if not self._open:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   150
            raise ProgrammingError('Closed connection %s' % self._cnxid)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   151
        session = self._session
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   152
        old_cnx = session._current_cnx_id
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   153
        try:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   154
            session.set_cnx(self._cnxid)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   155
            session.set_cnxset()
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   156
            try:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   157
                yield session
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   158
            finally:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   159
                session.free_cnxset()
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   160
        finally:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   161
            if old_cnx is not None:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   162
                session.set_cnx(old_cnx)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   163
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   164
    # Main Connection purpose in life #########################################
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   165
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   166
    call_service = _srv_cnx_func('call_service')
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   167
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   168
    def execute(self, *args, **kwargs):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   169
        # the ``with`` dance is transitional. We do not have Standalone
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   170
        # Connection yet so we use this trick to unsure the session have the
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   171
        # proper cnx loaded. This can be simplified one we have Standalone
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   172
        # Connection object
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   173
        with self._srv_cnx as cnx:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   174
            rset = cnx.execute(*args, **kwargs)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   175
        rset.req = self
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   176
        return rset
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   177
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   178
    commit = _srv_cnx_func('commit')
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   179
    rollback = _srv_cnx_func('rollback')
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   180
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   181
    # session data methods #####################################################
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   182
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   183
    get_shared_data = _srv_cnx_func('get_shared_data')
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   184
    set_shared_data = _srv_cnx_func('set_shared_data')
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   185
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   186
    # meta-data accessors ######################################################
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   187
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   188
    def source_defs(self):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   189
        """Return the definition of sources used by the repository."""
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   190
        return self._session.repo.source_defs()
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   191
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   192
    def get_schema(self):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   193
        """Return the schema currently used by the repository."""
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   194
        return self._session.repo.source_defs()
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   195
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   196
    def get_option_value(self, option, foreid=None):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   197
        """Return the value for `option` in the configuration. If `foreid` is
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   198
        specified, the actual repository to which this entity belongs is
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   199
        dereferenced and the option value retrieved from it.
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   200
        """
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   201
        return self._session.repo.get_option_value(option, foreid)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   202
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   203
    describe = _srv_cnx_func('describe')
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   204
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   205
    # undo support ############################################################
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   206
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   207
    def undoable_transactions(self, ueid=None, req=None, **actionfilters):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   208
        """Return a list of undoable transaction objects by the connection's
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   209
        user, ordered by descendant transaction time.
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   210
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   211
        Managers may filter according to user (eid) who has done the transaction
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   212
        using the `ueid` argument. Others will only see their own transactions.
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   213
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   214
        Additional filtering capabilities is provided by using the following
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   215
        named arguments:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   216
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   217
        * `etype` to get only transactions creating/updating/deleting entities
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   218
          of the given type
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   219
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   220
        * `eid` to get only transactions applied to entity of the given eid
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   221
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   222
        * `action` to get only transactions doing the given action (action in
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   223
          'C', 'U', 'D', 'A', 'R'). If `etype`, action can only be 'C', 'U' or
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   224
          'D'.
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   225
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   226
        * `public`: when additional filtering is provided, their are by default
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   227
          only searched in 'public' actions, unless a `public` argument is given
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   228
          and set to false.
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   229
        """
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   230
        # the ``with`` dance is transitional. We do not have Standalone
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   231
        # Connection yet so we use this trick to unsure the session have the
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   232
        # proper cnx loaded. This can be simplified one we have Standalone
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   233
        # Connection object
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   234
        with self._srv_cnx as cnx:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   235
            source = cnx.repo.system_source
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   236
            txinfos = source.undoable_transactions(cnx, ueid, **actionfilters)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   237
        for txinfo in txinfos:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   238
            txinfo.req = req or self  # XXX mostly wrong
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   239
        return txinfos
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   240
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   241
    def transaction_info(self, txuuid, req=None):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   242
        """Return transaction object for the given uid.
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   243
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   244
        raise `NoSuchTransaction` if not found or if session's user is not
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   245
        allowed (eg not in managers group and the transaction doesn't belong to
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   246
        him).
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   247
        """
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   248
        # the ``with`` dance is transitional. We do not have Standalone
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   249
        # Connection yet so we use this trick to unsure the session have the
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   250
        # proper cnx loaded. This can be simplified one we have Standalone
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   251
        # Connection object
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   252
        with self._srv_cnx as cnx:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   253
            txinfo = cnx.repo.system_source.tx_info(cnx, txuuid)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   254
        if req:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   255
            txinfo.req = req
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   256
        else:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   257
            txinfo.cnx = self
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   258
        return txinfo
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   259
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   260
    def transaction_actions(self, txuuid, public=True):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   261
        """Return an ordered list of action effectued during that transaction.
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   262
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   263
        If public is true, return only 'public' actions, eg not ones triggered
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   264
        under the cover by hooks, else return all actions.
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   265
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   266
        raise `NoSuchTransaction` if the transaction is not found or if
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   267
        session's user is not allowed (eg not in managers group and the
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   268
        transaction doesn't belong to him).
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   269
        """
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   270
        # the ``with`` dance is transitional. We do not have Standalone
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   271
        # Connection yet so we use this trick to unsure the session have the
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   272
        # proper cnx loaded. This can be simplified one we have Standalone
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   273
        # Connection object
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   274
        with self._srv_cnx as cnx:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   275
            return cnx.repo.system_source.tx_actions(cnx, txuuid, public)
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   276
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   277
    def undo_transaction(self, txuuid):
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   278
        """Undo the given transaction. Return potential restoration errors.
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   279
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   280
        raise `NoSuchTransaction` if not found or if session's user is not
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   281
        allowed (eg not in managers group and the transaction doesn't belong to
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   282
        him).
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   283
        """
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   284
        # the ``with`` dance is transitional. We do not have Standalone
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   285
        # Connection yet so we use this trick to unsure the session have the
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   286
        # proper cnx loaded. This can be simplified one we have Standalone
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   287
        # Connection object
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   288
        with self._srv_cnx as cnx:
4cba5f2cd57b [repoapi] introduce a basic ClientConnection class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9045
diff changeset
   289
            return cnx.repo.system_source.undo_transaction(cnx, txuuid)
9053
862040061173 [client-connection] add a request() method for dbapi compatibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9052
diff changeset
   290
862040061173 [client-connection] add a request() method for dbapi compatibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9052
diff changeset
   291
    def request(self):
862040061173 [client-connection] add a request() method for dbapi compatibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9052
diff changeset
   292
        # XXX This is DBAPI compatibility method. Deprecate it ASAP.
862040061173 [client-connection] add a request() method for dbapi compatibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9052
diff changeset
   293
        return self
9054
d7e78645dab6 [client-connection] add a cursor() method for dbapi compatibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9053
diff changeset
   294
d7e78645dab6 [client-connection] add a cursor() method for dbapi compatibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9053
diff changeset
   295
    def cursor(self):
d7e78645dab6 [client-connection] add a cursor() method for dbapi compatibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9053
diff changeset
   296
        # XXX This is DBAPI compatibility method. Deprecate it ASAP.
d7e78645dab6 [client-connection] add a cursor() method for dbapi compatibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9053
diff changeset
   297
        return self