zmqclient.py
author Pierre-Yves David <pierre-yves.david@logilab.fr>
Thu, 13 Jun 2013 15:12:06 +0200
changeset 9020 cb87e831c183
parent 8670 f02139297beb
child 10235 684215aca046
permissions -rw-r--r--
rename server.session.transaction into server.session.connection The ongoing rework of the API to access the database include the splitting of Session in two objects: The ``Session`` object will only hold credential (user) and global session data. A new object will publicly emerge to handle a database access. This object is named ``Connection`` since that the way database accessors with the same property are named in other system. So we renamed the ``Transaction`` object into ``Connection``. The ``Transaction`` object have already grown in the direction of something directly usable by generic code, but the name ``Transaction`` is ill suited as such object can be used for multiple database transaction in a row. Related to #2503918

# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of CubicWeb.
#
# CubicWeb is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
"""Source to query another RQL repository using pyro"""

__docformat__ = "restructuredtext en"
_ = unicode

from functools import partial
import zmq

from cubicweb.server.cwzmq import cwproto_to_zmqaddr

# XXX hack to overpass old zmq limitation that force to have
# only one context per python process
try:
    from cubicweb.server.cwzmq import ctx
except ImportError:
    ctx = zmq.Context()

class ZMQRepositoryClient(object):
    """
    This class delegates the overall repository stuff to a remote source.

    So calling a method of this repository will result on calling the
    corresponding method of the remote source repository.

    Any raised exception on the remote source is propagated locally.

    ZMQ is used as the transport layer and cPickle is used to serialize data.
    """

    def __init__(self, zmq_address):
        """A zmq address provided here will be like
        `zmqpickle-tcp://127.0.0.1:42000`.  W

        We chop the prefix to get a real zmq address.
        """
        self.socket = ctx.socket(zmq.REQ)
        self.socket.connect(cwproto_to_zmqaddr(zmq_address))

    def __zmqcall__(self, name, *args, **kwargs):
         self.socket.send_pyobj([name, args, kwargs])
         result = self.socket.recv_pyobj()
         if isinstance(result, BaseException):
             raise result
         return result

    def __getattr__(self, name):
        return partial(self.__zmqcall__, name)