zmqclient.py
branchstable
changeset 8463 a964c40adbe3
parent 8352 0e3b41118631
child 8572 e54b3bc39011
equal deleted inserted replaced
8461:8af7c6d86efb 8463:a964c40adbe3
       
     1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """Source to query another RQL repository using pyro"""
       
    19 
       
    20 __docformat__ = "restructuredtext en"
       
    21 _ = unicode
       
    22 
       
    23 from functools import partial
       
    24 import zmq
       
    25 
       
    26 
       
    27 # XXX hack to overpass old zmq limitation that force to have
       
    28 # only one context per python process
       
    29 try:
       
    30     from cubicweb.server.cwzmq import ctx
       
    31 except ImportError:
       
    32     ctx = zmq.Context()
       
    33 
       
    34 class ZMQRepositoryClient(object):
       
    35     """
       
    36     This class delegate the overall repository stuff to a remote source.
       
    37 
       
    38     So calling a method of this repository will results on calling the
       
    39     corresponding method of the remote source repository.
       
    40 
       
    41     Any raised exception on the remote source is propagated locally.
       
    42 
       
    43     ZMQ is used as the transport layer and cPickle is used to serialize data.
       
    44     """
       
    45 
       
    46     def __init__(self, config, vreg=None):
       
    47         self.config = config
       
    48         self.vreg = vreg
       
    49         self.socket = ctx.socket(zmq.REQ)
       
    50         self.host = config.get('base-url')
       
    51         self.socket.connect(self.host)
       
    52 
       
    53     def __zmqcall__(self, name, *args, **kwargs):
       
    54          self.socket.send_pyobj([name, args, kwargs])
       
    55          result = self.socket.recv_pyobj()
       
    56          if isinstance(result, BaseException):
       
    57              raise result
       
    58          return result
       
    59 
       
    60     def __getattr__(self, name):
       
    61         return partial(self.__zmqcall__, name)