author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 17 Jul 2012 11:08:36 +0200 | |
branch | stable |
changeset 8467 | ad75430a2dc8 |
parent 8352 | 0e3b41118631 |
child 8572 | e54b3bc39011 |
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 |
|
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
26 |
|
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
27 |
# 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
|
28 |
# 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
|
29 |
try: |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
30 |
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
|
31 |
except ImportError: |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
32 |
ctx = zmq.Context() |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
33 |
|
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
34 |
class ZMQRepositoryClient(object): |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
35 |
""" |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
36 |
This class delegate the overall repository stuff to a remote source. |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
37 |
|
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
38 |
So calling a method of this repository will results on calling the |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
39 |
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
|
40 |
|
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
41 |
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
|
42 |
|
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
43 |
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
|
44 |
""" |
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 |
def __init__(self, config, vreg=None): |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
47 |
self.config = config |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
48 |
self.vreg = vreg |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
49 |
self.socket = ctx.socket(zmq.REQ) |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
50 |
self.host = config.get('base-url') |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
51 |
self.socket.connect(self.host) |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
52 |
|
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
53 |
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
|
54 |
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
|
55 |
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
|
56 |
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
|
57 |
raise result |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
58 |
return result |
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
59 |
|
0e3b41118631
[dbapi] add possibility to connect to a remote ZMQRepository (closes #2290126)
Vincent Michel <vincent.michel@logilab.fr>
parents:
diff
changeset
|
60 |
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
|
61 |
return partial(self.__zmqcall__, name) |