author | Pierre-Yves David <pierre-yves.david@logilab.fr> |
Wed, 27 Mar 2013 11:42:13 +0100 | |
changeset 8811 | 1b7b4709c0cf |
parent 8670 | f02139297beb |
child 10235 | 684215aca046 |
permissions | -rw-r--r-- |
8352
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
1 |
# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
3 |
# |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
4 |
# This file is part of CubicWeb. |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
5 |
# |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
9 |
# any later version. |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
10 |
# |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
14 |
# details. |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
15 |
# |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
18 |
"""Source to query another RQL repository using pyro""" |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
19 |
|
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
20 |
__docformat__ = "restructuredtext en" |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
21 |
_ = unicode |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
22 |
|
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
23 |
from functools import partial |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
24 |
import zmq |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
25 |
|
8670
f02139297beb
prefix "tcp://" zmq uris with "zmqpickle" (closes #2574114)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8572
diff
changeset
|
26 |
from cubicweb.server.cwzmq import cwproto_to_zmqaddr |
8352
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
27 |
|
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
28 |
# XXX hack to overpass old zmq limitation that force to have |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
29 |
# only one context per python process |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
30 |
try: |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
31 |
from cubicweb.server.cwzmq import ctx |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
32 |
except ImportError: |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
33 |
ctx = zmq.Context() |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
34 |
|
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
35 |
class ZMQRepositoryClient(object): |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
36 |
""" |
8670
f02139297beb
prefix "tcp://" zmq uris with "zmqpickle" (closes #2574114)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8572
diff
changeset
|
37 |
This class delegates the overall repository stuff to a remote source. |
8352
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
38 |
|
8670
f02139297beb
prefix "tcp://" zmq uris with "zmqpickle" (closes #2574114)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8572
diff
changeset
|
39 |
So calling a method of this repository will result on calling the |
8352
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
40 |
corresponding method of the remote source repository. |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
41 |
|
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
42 |
Any raised exception on the remote source is propagated locally. |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
43 |
|
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
44 |
ZMQ is used as the transport layer and cPickle is used to serialize data. |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
45 |
""" |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
46 |
|
8572
e54b3bc39011
fix no more running zmq repository test (closes #2500153)
Florent Cayré <florent.cayre@logilab.fr>
parents:
8352
diff
changeset
|
47 |
def __init__(self, zmq_address): |
8670
f02139297beb
prefix "tcp://" zmq uris with "zmqpickle" (closes #2574114)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8572
diff
changeset
|
48 |
"""A zmq address provided here will be like |
f02139297beb
prefix "tcp://" zmq uris with "zmqpickle" (closes #2574114)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8572
diff
changeset
|
49 |
`zmqpickle-tcp://127.0.0.1:42000`. W |
f02139297beb
prefix "tcp://" zmq uris with "zmqpickle" (closes #2574114)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8572
diff
changeset
|
50 |
|
f02139297beb
prefix "tcp://" zmq uris with "zmqpickle" (closes #2574114)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8572
diff
changeset
|
51 |
We chop the prefix to get a real zmq address. |
f02139297beb
prefix "tcp://" zmq uris with "zmqpickle" (closes #2574114)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8572
diff
changeset
|
52 |
""" |
8352
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
53 |
self.socket = ctx.socket(zmq.REQ) |
8670
f02139297beb
prefix "tcp://" zmq uris with "zmqpickle" (closes #2574114)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8572
diff
changeset
|
54 |
self.socket.connect(cwproto_to_zmqaddr(zmq_address)) |
8352
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
55 |
|
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
56 |
def __zmqcall__(self, name, *args, **kwargs): |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
57 |
self.socket.send_pyobj([name, args, kwargs]) |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
58 |
result = self.socket.recv_pyobj() |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
59 |
if isinstance(result, BaseException): |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
60 |
raise result |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
61 |
return result |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
62 |
|
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
63 |
def __getattr__(self, name): |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
64 |
return partial(self.__zmqcall__, name) |