15 from time import time, clock |
15 from time import time, clock |
16 from itertools import count |
16 from itertools import count |
17 |
17 |
18 from logilab.common.logging_ext import set_log_methods |
18 from logilab.common.logging_ext import set_log_methods |
19 from logilab.common.decorators import monkeypatch |
19 from logilab.common.decorators import monkeypatch |
|
20 from logilab.common.deprecation import deprecated |
20 |
21 |
21 from cubicweb import ETYPE_NAME_MAP, ConnectionError, RequestSessionMixIn |
22 from cubicweb import ETYPE_NAME_MAP, ConnectionError, RequestSessionMixIn |
22 from cubicweb import cwvreg, cwconfig |
23 from cubicweb import cwvreg, cwconfig |
23 |
24 |
24 _MARKER = object() |
25 _MARKER = object() |
27 try: |
28 try: |
28 return super(dbapi.DBAPIRequest, self).property_value(name) |
29 return super(dbapi.DBAPIRequest, self).property_value(name) |
29 except KeyError: |
30 except KeyError: |
30 return '' |
31 return '' |
31 |
32 |
32 def _fix_cls_attrs(reg, vobject): |
33 def _fix_cls_attrs(reg, appobject): |
33 vobject.vreg = reg.vreg |
34 appobject.vreg = reg.vreg |
34 vobject.schema = reg.schema |
35 appobject.schema = reg.schema |
35 vobject.config = reg.config |
36 appobject.config = reg.config |
36 |
37 |
37 def multiple_connections_fix(): |
38 def multiple_connections_fix(): |
38 """some monkey patching necessary when an application has to deal with |
39 """some monkey patching necessary when an application has to deal with |
39 several connections to different repositories. It tries to hide buggy class |
40 several connections to different repositories. It tries to hide buggy class |
40 attributes since classes are not designed to be shared among multiple |
41 attributes since classes are not designed to be shared among multiple |
41 registries. |
42 registries. |
42 """ |
43 """ |
43 defaultcls = cwvreg.VRegistry.REGISTRY_FACTORY[None] |
44 defaultcls = cwvreg.VRegistry.REGISTRY_FACTORY[None] |
44 orig_select_best = defaultcls.orig_select_best = defaultcls.select_best |
45 orig_select_best = defaultcls.orig_select_best = defaultcls.select_best |
45 @monkeypatch(defaultcls) |
46 @monkeypatch(defaultcls) |
46 def select_best(self, vobjects, *args, **kwargs): |
47 def select_best(self, appobjects, *args, **kwargs): |
47 """return an instance of the most specific object according |
48 """return an instance of the most specific object according |
48 to parameters |
49 to parameters |
49 |
50 |
50 raise NoSelectableObject if no object apply |
51 raise NoSelectableObject if no object apply |
51 """ |
52 """ |
52 for vobjectcls in vobjects: |
53 for appobjectcls in appobjects: |
53 _fix_cls_attrs(self, vobjectcls) |
54 _fix_cls_attrs(self, appobjectcls) |
54 selected = orig_select_best(self, vobjects, *args, **kwargs) |
55 selected = orig_select_best(self, appobjects, *args, **kwargs) |
55 # redo the same thing on the instance so it won't use equivalent class |
56 # redo the same thing on the instance so it won't use equivalent class |
56 # attributes (which may change) |
57 # attributes (which may change) |
57 _fix_cls_attrs(self, selected) |
58 _fix_cls_attrs(self, selected) |
58 return selected |
59 return selected |
59 |
60 |
446 """ |
447 """ |
447 if self._closed is not None: |
448 if self._closed is not None: |
448 raise ProgrammingError('Closed connection') |
449 raise ProgrammingError('Closed connection') |
449 return self._repo.get_schema() |
450 return self._repo.get_schema() |
450 |
451 |
451 def load_vobjects(self, cubes=_MARKER, subpath=None, expand=True, |
452 def load_appobjects(self, cubes=_MARKER, subpath=None, expand=True, |
452 force_reload=None): |
453 force_reload=None): |
453 config = self.vreg.config |
454 config = self.vreg.config |
454 if cubes is _MARKER: |
455 if cubes is _MARKER: |
455 cubes = self._repo.get_cubes() |
456 cubes = self._repo.get_cubes() |
456 elif cubes is None: |
457 elif cubes is None: |
479 hm.register_system_hooks(config) |
480 hm.register_system_hooks(config) |
480 # instance specific hooks |
481 # instance specific hooks |
481 if self._repo.config.instance_hooks: |
482 if self._repo.config.instance_hooks: |
482 hm.register_hooks(config.load_hooks(self.vreg)) |
483 hm.register_hooks(config.load_hooks(self.vreg)) |
483 |
484 |
|
485 load_vobjects = deprecated()(load_appobjects) |
|
486 |
484 def use_web_compatible_requests(self, baseurl, sitetitle=None): |
487 def use_web_compatible_requests(self, baseurl, sitetitle=None): |
485 """monkey patch DBAPIRequest to fake a cw.web.request, so you should |
488 """monkey patch DBAPIRequest to fake a cw.web.request, so you should |
486 able to call html views using rset from a simple dbapi connection. |
489 able to call html views using rset from a simple dbapi connection. |
487 |
490 |
488 You should call `load_vobjects` at some point to register those views. |
491 You should call `load_appobjects` at some point to register those views. |
489 """ |
492 """ |
490 from cubicweb.web.request import CubicWebRequestBase as cwrb |
493 from cubicweb.web.request import CubicWebRequestBase as cwrb |
491 DBAPIRequest.build_ajax_replace_url = cwrb.build_ajax_replace_url.im_func |
494 DBAPIRequest.build_ajax_replace_url = cwrb.build_ajax_replace_url.im_func |
492 DBAPIRequest.list_form_param = cwrb.list_form_param.im_func |
495 DBAPIRequest.list_form_param = cwrb.list_form_param.im_func |
493 DBAPIRequest.property_value = _fake_property_value |
496 DBAPIRequest.property_value = _fake_property_value |