35 |
35 |
36 from logilab.common.logging_ext import set_log_methods |
36 from logilab.common.logging_ext import set_log_methods |
37 from logilab.common.decorators import monkeypatch, cachedproperty |
37 from logilab.common.decorators import monkeypatch, cachedproperty |
38 from logilab.common.deprecation import deprecated |
38 from logilab.common.deprecation import deprecated |
39 |
39 |
40 from cubicweb import ETYPE_NAME_MAP, ConnectionError, AuthenticationError,\ |
40 from cubicweb import ETYPE_NAME_MAP, AuthenticationError,\ |
41 cwvreg, cwconfig |
41 cwvreg, cwconfig |
|
42 from cubicweb.repoapi import get_repository |
42 from cubicweb.req import RequestSessionBase |
43 from cubicweb.req import RequestSessionBase |
43 from cubicweb.utils import parse_repo_uri |
|
44 |
44 |
45 |
45 |
46 _MARKER = object() |
46 _MARKER = object() |
47 |
47 |
48 def _fake_property_value(self, name): |
48 def _fake_property_value(self, name): |
89 self.cnxtype = cnxtype |
89 self.cnxtype = cnxtype |
90 self.log_queries = log |
90 self.log_queries = log |
91 self.close_on_del = close |
91 self.close_on_del = close |
92 |
92 |
93 |
93 |
94 def _get_inmemory_repo(config, vreg=None): |
94 |
95 from cubicweb.server.repository import Repository |
|
96 from cubicweb.server.utils import TasksManager |
|
97 return Repository(config, TasksManager(), vreg=vreg) |
|
98 |
|
99 def get_repository(uri=None, config=None, vreg=None): |
|
100 """get a repository for the given URI or config/vregistry (in case we're |
|
101 loading the repository for a client, eg web server, configuration). |
|
102 |
|
103 The returned repository may be an in-memory repository or a proxy object |
|
104 using a specific RPC method, depending on the given URI (pyro or zmq). |
|
105 """ |
|
106 if uri is None: |
|
107 return _get_inmemory_repo(config, vreg) |
|
108 |
|
109 protocol, hostport, appid = parse_repo_uri(uri) |
|
110 |
|
111 if protocol == 'inmemory': |
|
112 # me may have been called with a dummy 'inmemory://' uri ... |
|
113 return _get_inmemory_repo(config, vreg) |
|
114 |
|
115 if protocol == 'pyroloc': # direct connection to the instance |
|
116 from logilab.common.pyro_ext import get_proxy |
|
117 uri = uri.replace('pyroloc', 'PYRO') |
|
118 return get_proxy(uri) |
|
119 |
|
120 if protocol == 'pyro': # connection mediated through the pyro ns |
|
121 from logilab.common.pyro_ext import ns_get_proxy |
|
122 path = appid.strip('/') |
|
123 if not path: |
|
124 raise ConnectionError( |
|
125 "can't find instance name in %s (expected to be the path component)" |
|
126 % uri) |
|
127 if '.' in path: |
|
128 nsgroup, nsid = path.rsplit('.', 1) |
|
129 else: |
|
130 nsgroup = 'cubicweb' |
|
131 nsid = path |
|
132 return ns_get_proxy(nsid, defaultnsgroup=nsgroup, nshost=hostport) |
|
133 |
|
134 if protocol.startswith('zmqpickle-'): |
|
135 from cubicweb.zmqclient import ZMQRepositoryClient |
|
136 return ZMQRepositoryClient(uri) |
|
137 else: |
|
138 raise ConnectionError('unknown protocol: `%s`' % protocol) |
|
139 |
95 |
140 |
96 |
141 def _repo_connect(repo, login, **kwargs): |
97 def _repo_connect(repo, login, **kwargs): |
142 """Constructor to create a new connection to the given CubicWeb repository. |
98 """Constructor to create a new connection to the given CubicWeb repository. |
143 |
99 |