author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 27 Jan 2010 09:53:48 +0100 | |
changeset 4387 | 4aacd6492ef4 |
parent 4252 | 6c4f109c2b03 |
child 4592 | ddbab12acae0 |
permissions | -rw-r--r-- |
0 | 1 |
"""DB-API 2.0 compliant module |
2 |
||
3 |
Take a look at http://www.python.org/peps/pep-0249.html |
|
4 |
||
5 |
(most parts of this document are reported here in docstrings) |
|
6 |
||
7 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3833
diff
changeset
|
8 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 9 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1923
diff
changeset
|
10 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 11 |
""" |
12 |
__docformat__ = "restructuredtext en" |
|
13 |
||
1132 | 14 |
from logging import getLogger |
0 | 15 |
from time import time, clock |
2496
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
16 |
from itertools import count |
0 | 17 |
|
1923
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
18 |
from logilab.common.logging_ext import set_log_methods |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
19 |
from logilab.common.decorators import monkeypatch |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
20 |
from logilab.common.deprecation import deprecated |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
21 |
|
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
22 |
from cubicweb import ETYPE_NAME_MAP, ConnectionError, cwvreg, cwconfig |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
23 |
from cubicweb.req import RequestSessionBase |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
24 |
|
1524 | 25 |
|
0 | 26 |
_MARKER = object() |
27 |
||
2496
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
28 |
def _fake_property_value(self, name): |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
29 |
try: |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
30 |
return super(dbapi.DBAPIRequest, self).property_value(name) |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
31 |
except KeyError: |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
32 |
return '' |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
33 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
34 |
def _fix_cls_attrs(reg, appobject): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
35 |
appobject.vreg = reg.vreg |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
36 |
appobject.schema = reg.schema |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
37 |
appobject.config = reg.config |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
38 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
39 |
def multiple_connections_fix(): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
40 |
"""some monkey patching necessary when an application has to deal with |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
41 |
several connections to different repositories. It tries to hide buggy class |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
42 |
attributes since classes are not designed to be shared among multiple |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
43 |
registries. |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
44 |
""" |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
45 |
defaultcls = cwvreg.VRegistry.REGISTRY_FACTORY[None] |
2770
356e9d7c356d
R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2665
diff
changeset
|
46 |
orig_select_best = defaultcls.orig_select_best = defaultcls._select_best |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
47 |
@monkeypatch(defaultcls) |
2770
356e9d7c356d
R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2665
diff
changeset
|
48 |
def _select_best(self, appobjects, *args, **kwargs): |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
49 |
"""return an instance of the most specific object according |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
50 |
to parameters |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
51 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
52 |
raise NoSelectableObject if no object apply |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
53 |
""" |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
54 |
for appobjectcls in appobjects: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
55 |
_fix_cls_attrs(self, appobjectcls) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
56 |
selected = orig_select_best(self, appobjects, *args, **kwargs) |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
57 |
# redo the same thing on the instance so it won't use equivalent class |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
58 |
# attributes (which may change) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
59 |
_fix_cls_attrs(self, selected) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
60 |
return selected |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
61 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
62 |
etypescls = cwvreg.VRegistry.REGISTRY_FACTORY['etypes'] |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
63 |
orig_etype_class = etypescls.orig_etype_class = etypescls.etype_class |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
64 |
@monkeypatch(defaultcls) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
65 |
def etype_class(self, etype): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
66 |
"""return an entity class for the given entity type. |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
67 |
Try to find out a specific class for this kind of entity or |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
68 |
default to a dump of the class registered for 'Any' |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
69 |
""" |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
70 |
usercls = orig_etype_class(self, etype) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
71 |
if etype == 'Any': |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
72 |
return usercls |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
73 |
usercls.e_schema = self.schema.eschema(etype) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
74 |
return usercls |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
75 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
76 |
def multiple_connections_unfix(): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
77 |
defaultcls = cwvreg.VRegistry.REGISTRY_FACTORY[None] |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
78 |
defaultcls.select_best = defaultcls.orig_select_best |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
79 |
etypescls = cwvreg.VRegistry.REGISTRY_FACTORY['etypes'] |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
80 |
etypescls.etype_class = etypescls.orig_etype_class |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
81 |
|
0 | 82 |
class ConnectionProperties(object): |
83 |
def __init__(self, cnxtype=None, lang=None, close=True, log=False): |
|
84 |
self.cnxtype = cnxtype or 'pyro' |
|
85 |
self.lang = lang |
|
86 |
self.log_queries = log |
|
87 |
self.close_on_del = close |
|
88 |
||
89 |
||
90 |
def get_repository(method, database=None, config=None, vreg=None): |
|
91 |
"""get a proxy object to the CubicWeb repository, using a specific RPC method. |
|
1524 | 92 |
|
0 | 93 |
Only 'in-memory' and 'pyro' are supported for now. Either vreg or config |
94 |
argument should be given |
|
95 |
""" |
|
96 |
assert method in ('pyro', 'inmemory') |
|
97 |
assert vreg or config |
|
98 |
if vreg and not config: |
|
99 |
config = vreg.config |
|
100 |
if method == 'inmemory': |
|
101 |
# get local access to the repository |
|
102 |
from cubicweb.server.repository import Repository |
|
103 |
return Repository(config, vreg=vreg) |
|
104 |
else: # method == 'pyro' |
|
105 |
# resolve the Pyro object |
|
2665
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
106 |
from logilab.common.pyro_ext import ns_get_proxy |
0 | 107 |
try: |
2665
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
108 |
return ns_get_proxy(database, |
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
109 |
defaultnsgroup=config['pyro-ns-group'], |
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
110 |
nshost=config['pyro-ns-host']) |
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
111 |
except Exception, ex: |
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
112 |
raise ConnectionError(str(ex)) |
1524 | 113 |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
114 |
def repo_connect(repo, login, **kwargs): |
0 | 115 |
"""Constructor to create a new connection to the CubicWeb repository. |
1524 | 116 |
|
0 | 117 |
Returns a Connection instance. |
118 |
""" |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
119 |
if not 'cnxprops' in kwargs: |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
120 |
kwargs['cnxprops'] = ConnectionProperties('inmemory') |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
121 |
cnxid = repo.connect(unicode(login), **kwargs) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
122 |
cnx = Connection(repo, cnxid, kwargs['cnxprops']) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
123 |
if kwargs['cnxprops'].cnxtype == 'inmemory': |
0 | 124 |
cnx.vreg = repo.vreg |
125 |
return cnx |
|
1524 | 126 |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
127 |
def connect(database=None, login=None, host=None, group=None, |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
128 |
cnxprops=None, setvreg=True, mulcnx=True, initlog=True, **kwargs): |
0 | 129 |
"""Constructor for creating a connection to the CubicWeb repository. |
130 |
Returns a Connection object. |
|
131 |
||
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
132 |
When method is 'pyro', setvreg is True, try to deal with connections to |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
133 |
differents instances in the same process unless specified otherwise by |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
134 |
setting the mulcnx to False. |
0 | 135 |
""" |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
136 |
config = cwconfig.CubicWebNoAppConfiguration() |
0 | 137 |
if host: |
138 |
config.global_set_option('pyro-ns-host', host) |
|
139 |
if group: |
|
140 |
config.global_set_option('pyro-ns-group', group) |
|
141 |
cnxprops = cnxprops or ConnectionProperties() |
|
142 |
method = cnxprops.cnxtype |
|
143 |
repo = get_repository(method, database, config=config) |
|
144 |
if method == 'inmemory': |
|
145 |
vreg = repo.vreg |
|
146 |
elif setvreg: |
|
147 |
if mulcnx: |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
148 |
multiple_connections_fix() |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
149 |
vreg = cwvreg.CubicWebVRegistry(config, initlog=initlog) |
1923
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
150 |
schema = repo.get_schema() |
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
151 |
for oldetype, newetype in ETYPE_NAME_MAP.items(): |
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
152 |
if oldetype in schema: |
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
153 |
print 'aliasing', newetype, 'to', oldetype |
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
154 |
schema._entities[newetype] = schema._entities[oldetype] |
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
155 |
vreg.set_schema(schema) |
0 | 156 |
else: |
157 |
vreg = None |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
158 |
cnx = repo_connect(repo, login, cnxprops=cnxprops, **kwargs) |
0 | 159 |
cnx.vreg = vreg |
160 |
return cnx |
|
161 |
||
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
162 |
def in_memory_cnx(config, login, **kwargs): |
0 | 163 |
"""usefull method for testing and scripting to get a dbapi.Connection |
1524 | 164 |
object connected to an in-memory repository instance |
0 | 165 |
""" |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
166 |
if isinstance(config, cwvreg.CubicWebVRegistry): |
0 | 167 |
vreg = config |
168 |
config = None |
|
169 |
else: |
|
170 |
vreg = None |
|
171 |
# get local access to the repository |
|
172 |
repo = get_repository('inmemory', config=config, vreg=vreg) |
|
173 |
# connection to the CubicWeb repository |
|
174 |
cnxprops = ConnectionProperties('inmemory') |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
175 |
cnx = repo_connect(repo, login, cnxprops=cnxprops, **kwargs) |
0 | 176 |
return repo, cnx |
177 |
||
178 |
||
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
179 |
class DBAPIRequest(RequestSessionBase): |
1524 | 180 |
|
0 | 181 |
def __init__(self, vreg, cnx=None): |
182 |
super(DBAPIRequest, self).__init__(vreg) |
|
183 |
try: |
|
184 |
# no vreg or config which doesn't handle translations |
|
185 |
self.translations = vreg.config.translations |
|
186 |
except AttributeError: |
|
187 |
self.translations = {} |
|
188 |
self.set_default_language(vreg) |
|
189 |
# cache entities built during the request |
|
190 |
self._eid_cache = {} |
|
191 |
# these args are initialized after a connection is |
|
192 |
# established |
|
193 |
self.cnx = None # connection associated to the request |
|
194 |
self._user = None # request's user, set at authentication |
|
195 |
if cnx is not None: |
|
196 |
self.set_connection(cnx) |
|
197 |
||
198 |
def base_url(self): |
|
199 |
return self.vreg.config['base-url'] |
|
1524 | 200 |
|
0 | 201 |
def from_controller(self): |
202 |
return 'view' |
|
1524 | 203 |
|
0 | 204 |
def set_connection(self, cnx, user=None): |
205 |
"""method called by the session handler when the user is authenticated |
|
206 |
or an anonymous connection is open |
|
207 |
""" |
|
208 |
self.cnx = cnx |
|
209 |
self.cursor = cnx.cursor(self) |
|
210 |
self.set_user(user) |
|
1524 | 211 |
|
0 | 212 |
def set_default_language(self, vreg): |
213 |
try: |
|
214 |
self.lang = vreg.property_value('ui.language') |
|
215 |
except: # property may not be registered |
|
216 |
self.lang = 'en' |
|
217 |
# use req.__ to translate a message without registering it to the catalog |
|
218 |
try: |
|
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3258
diff
changeset
|
219 |
gettext, pgettext = self.translations[self.lang] |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3258
diff
changeset
|
220 |
self._ = self.__ = gettext |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3258
diff
changeset
|
221 |
self.pgettext = pgettext |
0 | 222 |
except KeyError: |
223 |
# this occurs usually during test execution |
|
224 |
self._ = self.__ = unicode |
|
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3258
diff
changeset
|
225 |
self.pgettext = lambda x,y: y |
2111
5e142c7a4531
different message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
226 |
self.debug('request default language: %s', self.lang) |
0 | 227 |
|
228 |
def decorate_rset(self, rset): |
|
229 |
rset.vreg = self.vreg |
|
230 |
rset.req = self |
|
231 |
return rset |
|
1524 | 232 |
|
0 | 233 |
def describe(self, eid): |
234 |
"""return a tuple (type, sourceuri, extid) for the entity with id <eid>""" |
|
235 |
return self.cnx.describe(eid) |
|
1524 | 236 |
|
0 | 237 |
def source_defs(self): |
238 |
"""return the definition of sources used by the repository.""" |
|
239 |
return self.cnx.source_defs() |
|
1524 | 240 |
|
0 | 241 |
# entities cache management ############################################### |
1524 | 242 |
|
0 | 243 |
def entity_cache(self, eid): |
244 |
return self._eid_cache[eid] |
|
1524 | 245 |
|
0 | 246 |
def set_entity_cache(self, entity): |
247 |
self._eid_cache[entity.eid] = entity |
|
248 |
||
249 |
def cached_entities(self): |
|
250 |
return self._eid_cache.values() |
|
1524 | 251 |
|
0 | 252 |
def drop_entity_cache(self, eid=None): |
253 |
if eid is None: |
|
254 |
self._eid_cache = {} |
|
255 |
else: |
|
256 |
del self._eid_cache[eid] |
|
257 |
||
258 |
# low level session data management ####################################### |
|
259 |
||
260 |
def session_data(self): |
|
261 |
"""return a dictionnary containing session data""" |
|
262 |
return self.cnx.session_data() |
|
263 |
||
264 |
def get_session_data(self, key, default=None, pop=False): |
|
265 |
"""return value associated to `key` in session data""" |
|
266 |
return self.cnx.get_session_data(key, default, pop) |
|
1524 | 267 |
|
0 | 268 |
def set_session_data(self, key, value): |
269 |
"""set value associated to `key` in session data""" |
|
270 |
return self.cnx.set_session_data(key, value) |
|
1524 | 271 |
|
0 | 272 |
def del_session_data(self, key): |
273 |
"""remove value associated to `key` in session data""" |
|
274 |
return self.cnx.del_session_data(key) |
|
275 |
||
276 |
def get_shared_data(self, key, default=None, pop=False): |
|
277 |
"""return value associated to `key` in shared data""" |
|
278 |
return self.cnx.get_shared_data(key, default, pop) |
|
1524 | 279 |
|
0 | 280 |
def set_shared_data(self, key, value, querydata=False): |
281 |
"""set value associated to `key` in shared data |
|
282 |
||
283 |
if `querydata` is true, the value will be added to the repository |
|
284 |
session's query data which are cleared on commit/rollback of the current |
|
285 |
transaction, and won't be available through the connexion, only on the |
|
286 |
repository side. |
|
287 |
""" |
|
288 |
return self.cnx.set_shared_data(key, value, querydata) |
|
289 |
||
290 |
# server session compat layer ############################################# |
|
291 |
||
3110
757d36162235
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2665
diff
changeset
|
292 |
def hijack_user(self, user): |
757d36162235
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2665
diff
changeset
|
293 |
"""return a fake request/session using specified user""" |
757d36162235
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2665
diff
changeset
|
294 |
req = DBAPIRequest(self.vreg) |
757d36162235
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2665
diff
changeset
|
295 |
req.set_connection(self.cnx, user) |
757d36162235
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2665
diff
changeset
|
296 |
return req |
757d36162235
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2665
diff
changeset
|
297 |
|
0 | 298 |
@property |
299 |
def user(self): |
|
300 |
if self._user is None and self.cnx: |
|
2245
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2111
diff
changeset
|
301 |
self.set_user(self.cnx.user(self, {'lang': self.lang})) |
0 | 302 |
return self._user |
303 |
||
304 |
def set_user(self, user): |
|
305 |
self._user = user |
|
306 |
if user: |
|
307 |
self.set_entity_cache(user) |
|
1524 | 308 |
|
0 | 309 |
def execute(self, *args, **kwargs): |
310 |
"""Session interface compatibility""" |
|
311 |
return self.cursor.execute(*args, **kwargs) |
|
312 |
||
313 |
set_log_methods(DBAPIRequest, getLogger('cubicweb.dbapi')) |
|
1524 | 314 |
|
315 |
||
0 | 316 |
# exceptions ################################################################## |
317 |
||
318 |
class ProgrammingError(Exception): #DatabaseError): |
|
319 |
"""Exception raised for errors that are related to the database's operation |
|
320 |
and not necessarily under the control of the programmer, e.g. an unexpected |
|
321 |
disconnect occurs, the data source name is not found, a transaction could |
|
322 |
not be processed, a memory allocation error occurred during processing, |
|
323 |
etc. |
|
324 |
""" |
|
325 |
||
326 |
# module level objects ######################################################## |
|
327 |
||
328 |
||
329 |
apilevel = '2.0' |
|
330 |
||
331 |
"""Integer constant stating the level of thread safety the interface supports. |
|
332 |
Possible values are: |
|
333 |
||
334 |
0 Threads may not share the module. |
|
335 |
1 Threads may share the module, but not connections. |
|
336 |
2 Threads may share the module and connections. |
|
337 |
3 Threads may share the module, connections and |
|
338 |
cursors. |
|
339 |
||
340 |
Sharing in the above context means that two threads may use a resource without |
|
341 |
wrapping it using a mutex semaphore to implement resource locking. Note that |
|
342 |
you cannot always make external resources thread safe by managing access using |
|
343 |
a mutex: the resource may rely on global variables or other external sources |
|
344 |
that are beyond your control. |
|
345 |
""" |
|
346 |
threadsafety = 1 |
|
347 |
||
348 |
"""String constant stating the type of parameter marker formatting expected by |
|
349 |
the interface. Possible values are : |
|
350 |
||
1524 | 351 |
'qmark' Question mark style, |
0 | 352 |
e.g. '...WHERE name=?' |
1524 | 353 |
'numeric' Numeric, positional style, |
0 | 354 |
e.g. '...WHERE name=:1' |
1524 | 355 |
'named' Named style, |
0 | 356 |
e.g. '...WHERE name=:name' |
1524 | 357 |
'format' ANSI C printf format codes, |
0 | 358 |
e.g. '...WHERE name=%s' |
1524 | 359 |
'pyformat' Python extended format codes, |
0 | 360 |
e.g. '...WHERE name=%(name)s' |
361 |
""" |
|
362 |
paramstyle = 'pyformat' |
|
363 |
||
364 |
||
365 |
# connection object ########################################################### |
|
366 |
||
367 |
class Connection(object): |
|
3258
6536ee4f37f7
update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3110
diff
changeset
|
368 |
"""DB-API 2.0 compatible Connection object for CubicWeb |
0 | 369 |
""" |
370 |
# make exceptions available through the connection object |
|
371 |
ProgrammingError = ProgrammingError |
|
372 |
||
373 |
def __init__(self, repo, cnxid, cnxprops=None): |
|
374 |
self._repo = repo |
|
375 |
self.sessionid = cnxid |
|
376 |
self._close_on_del = getattr(cnxprops, 'close_on_del', True) |
|
377 |
self._cnxtype = getattr(cnxprops, 'cnxtype', 'pyro') |
|
378 |
self._closed = None |
|
379 |
if cnxprops and cnxprops.log_queries: |
|
380 |
self.executed_queries = [] |
|
381 |
self.cursor_class = LogCursor |
|
382 |
else: |
|
383 |
self.cursor_class = Cursor |
|
384 |
self.anonymous_connection = False |
|
385 |
self.vreg = None |
|
386 |
# session's data |
|
387 |
self.data = {} |
|
388 |
||
389 |
def __repr__(self): |
|
390 |
if self.anonymous_connection: |
|
391 |
return '<Connection %s (anonymous)>' % self.sessionid |
|
392 |
return '<Connection %s>' % self.sessionid |
|
393 |
||
394 |
def request(self): |
|
395 |
return DBAPIRequest(self.vreg, self) |
|
1524 | 396 |
|
0 | 397 |
def session_data(self): |
398 |
"""return a dictionnary containing session data""" |
|
399 |
return self.data |
|
1524 | 400 |
|
0 | 401 |
def get_session_data(self, key, default=None, pop=False): |
402 |
"""return value associated to `key` in session data""" |
|
403 |
if pop: |
|
404 |
return self.data.pop(key, default) |
|
405 |
else: |
|
406 |
return self.data.get(key, default) |
|
1524 | 407 |
|
0 | 408 |
def set_session_data(self, key, value): |
409 |
"""set value associated to `key` in session data""" |
|
410 |
self.data[key] = value |
|
1524 | 411 |
|
0 | 412 |
def del_session_data(self, key): |
413 |
"""remove value associated to `key` in session data""" |
|
414 |
try: |
|
415 |
del self.data[key] |
|
416 |
except KeyError: |
|
1524 | 417 |
pass |
0 | 418 |
|
419 |
def check(self): |
|
420 |
"""raise `BadSessionId` if the connection is no more valid""" |
|
1132 | 421 |
self._repo.check_session(self.sessionid) |
0 | 422 |
|
2245
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2111
diff
changeset
|
423 |
def set_session_props(self, **props): |
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2111
diff
changeset
|
424 |
"""raise `BadSessionId` if the connection is no more valid""" |
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2111
diff
changeset
|
425 |
self._repo.set_session_props(self.sessionid, props) |
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2111
diff
changeset
|
426 |
|
0 | 427 |
def get_shared_data(self, key, default=None, pop=False): |
428 |
"""return value associated to `key` in shared data""" |
|
429 |
return self._repo.get_shared_data(self.sessionid, key, default, pop) |
|
1524 | 430 |
|
0 | 431 |
def set_shared_data(self, key, value, querydata=False): |
432 |
"""set value associated to `key` in shared data |
|
433 |
||
434 |
if `querydata` is true, the value will be added to the repository |
|
435 |
session's query data which are cleared on commit/rollback of the current |
|
436 |
transaction, and won't be available through the connexion, only on the |
|
437 |
repository side. |
|
438 |
""" |
|
439 |
return self._repo.set_shared_data(self.sessionid, key, value, querydata) |
|
1524 | 440 |
|
0 | 441 |
def get_schema(self): |
442 |
"""Return the schema currently used by the repository. |
|
1524 | 443 |
|
0 | 444 |
This is NOT part of the DB-API. |
445 |
""" |
|
446 |
if self._closed is not None: |
|
447 |
raise ProgrammingError('Closed connection') |
|
448 |
return self._repo.get_schema() |
|
449 |
||
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
450 |
def load_appobjects(self, cubes=_MARKER, subpath=None, expand=True, |
3707
78596919ede3
[c-c] fixes for shell w/ pyro instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
451 |
force_reload=None): |
0 | 452 |
config = self.vreg.config |
453 |
if cubes is _MARKER: |
|
454 |
cubes = self._repo.get_cubes() |
|
455 |
elif cubes is None: |
|
456 |
cubes = () |
|
457 |
else: |
|
458 |
if not isinstance(cubes, (list, tuple)): |
|
459 |
cubes = (cubes,) |
|
460 |
if expand: |
|
461 |
cubes = config.expand_cubes(cubes) |
|
462 |
if subpath is None: |
|
463 |
subpath = esubpath = ('entities', 'views') |
|
464 |
else: |
|
465 |
esubpath = subpath |
|
466 |
if 'views' in subpath: |
|
467 |
esubpath = list(subpath) |
|
468 |
esubpath.remove('views') |
|
469 |
esubpath.append('web/views') |
|
470 |
cubes = reversed([config.cube_dir(p) for p in cubes]) |
|
471 |
vpath = config.build_vregistry_path(cubes, evobjpath=esubpath, |
|
472 |
tvobjpath=subpath) |
|
473 |
self.vreg.register_objects(vpath, force_reload) |
|
474 |
if self._cnxtype == 'inmemory': |
|
475 |
# should reinit hooks manager as well |
|
476 |
hm, config = self._repo.hm, self._repo.config |
|
477 |
hm.set_schema(hm.schema) # reset structure |
|
478 |
hm.register_system_hooks(config) |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2266
diff
changeset
|
479 |
# instance specific hooks |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2266
diff
changeset
|
480 |
if self._repo.config.instance_hooks: |
0 | 481 |
hm.register_hooks(config.load_hooks(self.vreg)) |
1524 | 482 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
483 |
load_vobjects = deprecated()(load_appobjects) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
484 |
|
2496
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
485 |
def use_web_compatible_requests(self, baseurl, sitetitle=None): |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
486 |
"""monkey patch DBAPIRequest to fake a cw.web.request, so you should |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
487 |
able to call html views using rset from a simple dbapi connection. |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
488 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
489 |
You should call `load_appobjects` at some point to register those views. |
2496
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
490 |
""" |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
491 |
from cubicweb.web.request import CubicWebRequestBase as cwrb |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
492 |
DBAPIRequest.build_ajax_replace_url = cwrb.build_ajax_replace_url.im_func |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
493 |
DBAPIRequest.list_form_param = cwrb.list_form_param.im_func |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
494 |
DBAPIRequest.property_value = _fake_property_value |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
495 |
DBAPIRequest.next_tabindex = count().next |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
496 |
DBAPIRequest.form = {} |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
497 |
DBAPIRequest.data = {} |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
498 |
fake = lambda *args, **kwargs: None |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
499 |
DBAPIRequest.relative_path = fake |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
500 |
DBAPIRequest.url = fake |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
501 |
DBAPIRequest.next_tabindex = fake |
3832
ce8dd861f442
#473188: missing get_page_data when patching bare db-api request for web compatibility
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3707
diff
changeset
|
502 |
DBAPIRequest.get_page_data = fake |
3833
5507aa75f601
add set_page_data as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3832
diff
changeset
|
503 |
DBAPIRequest.set_page_data = fake |
2496
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
504 |
DBAPIRequest.add_js = fake #cwrb.add_js.im_func |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
505 |
DBAPIRequest.add_css = fake #cwrb.add_css.im_func |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
506 |
# XXX could ask the repo for it's base-url configuration |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
507 |
self.vreg.config.set_option('base-url', baseurl) |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
508 |
# XXX why is this needed? if really needed, could be fetched by a query |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
509 |
if sitetitle is not None: |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
510 |
self.vreg['propertydefs']['ui.site-title'] = {'default': sitetitle} |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
511 |
|
0 | 512 |
def source_defs(self): |
513 |
"""Return the definition of sources used by the repository. |
|
1524 | 514 |
|
0 | 515 |
This is NOT part of the DB-API. |
516 |
""" |
|
517 |
if self._closed is not None: |
|
518 |
raise ProgrammingError('Closed connection') |
|
519 |
return self._repo.source_defs() |
|
520 |
||
322
0d9aca19b3d0
make req argument optional
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
169
diff
changeset
|
521 |
def user(self, req=None, props=None): |
0 | 522 |
"""return the User object associated to this connection""" |
523 |
# cnx validity is checked by the call to .user_info |
|
2245
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2111
diff
changeset
|
524 |
eid, login, groups, properties = self._repo.user_info(self.sessionid, |
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2111
diff
changeset
|
525 |
props) |
0 | 526 |
if req is None: |
527 |
req = self.request() |
|
1923
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
528 |
rset = req.eid_rset(eid, 'CWUser') |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
529 |
user = self.vreg['etypes'].etype_class('CWUser')(req, rset, row=0, |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
530 |
groups=groups, |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
531 |
properties=properties) |
0 | 532 |
user['login'] = login # cache login |
533 |
return user |
|
534 |
||
535 |
def __del__(self): |
|
536 |
"""close the remote connection if necessary""" |
|
537 |
if self._closed is None and self._close_on_del: |
|
538 |
try: |
|
539 |
self.close() |
|
540 |
except: |
|
541 |
pass |
|
1524 | 542 |
|
0 | 543 |
def describe(self, eid): |
544 |
return self._repo.describe(self.sessionid, eid) |
|
1524 | 545 |
|
0 | 546 |
def close(self): |
547 |
"""Close the connection now (rather than whenever __del__ is called). |
|
1524 | 548 |
|
0 | 549 |
The connection will be unusable from this point forward; an Error (or |
550 |
subclass) exception will be raised if any operation is attempted with |
|
551 |
the connection. The same applies to all cursor objects trying to use the |
|
552 |
connection. Note that closing a connection without committing the |
|
553 |
changes first will cause an implicit rollback to be performed. |
|
554 |
""" |
|
555 |
if self._closed: |
|
556 |
raise ProgrammingError('Connection is already closed') |
|
557 |
self._repo.close(self.sessionid) |
|
558 |
self._closed = 1 |
|
559 |
||
560 |
def commit(self): |
|
561 |
"""Commit any pending transaction to the database. Note that if the |
|
562 |
database supports an auto-commit feature, this must be initially off. An |
|
563 |
interface method may be provided to turn it back on. |
|
1524 | 564 |
|
0 | 565 |
Database modules that do not support transactions should implement this |
566 |
method with void functionality. |
|
567 |
""" |
|
568 |
if not self._closed is None: |
|
569 |
raise ProgrammingError('Connection is already closed') |
|
570 |
self._repo.commit(self.sessionid) |
|
571 |
||
572 |
def rollback(self): |
|
573 |
"""This method is optional since not all databases provide transaction |
|
574 |
support. |
|
1524 | 575 |
|
0 | 576 |
In case a database does provide transactions this method causes the the |
577 |
database to roll back to the start of any pending transaction. Closing |
|
578 |
a connection without committing the changes first will cause an implicit |
|
579 |
rollback to be performed. |
|
580 |
""" |
|
581 |
if not self._closed is None: |
|
582 |
raise ProgrammingError('Connection is already closed') |
|
583 |
self._repo.rollback(self.sessionid) |
|
584 |
||
585 |
def cursor(self, req=None): |
|
586 |
"""Return a new Cursor Object using the connection. If the database |
|
587 |
does not provide a direct cursor concept, the module will have to |
|
588 |
emulate cursors using other means to the extent needed by this |
|
589 |
specification. |
|
590 |
""" |
|
591 |
if self._closed is not None: |
|
592 |
raise ProgrammingError('Can\'t get cursor on closed connection') |
|
593 |
if req is None: |
|
594 |
req = self.request() |
|
595 |
return self.cursor_class(self, self._repo, req=req) |
|
596 |
||
597 |
||
598 |
# cursor object ############################################################### |
|
599 |
||
600 |
class Cursor(object): |
|
601 |
"""These objects represent a database cursor, which is used to manage the |
|
602 |
context of a fetch operation. Cursors created from the same connection are |
|
603 |
not isolated, i.e., any changes done to the database by a cursor are |
|
604 |
immediately visible by the other cursors. Cursors created from different |
|
605 |
connections can or can not be isolated, depending on how the transaction |
|
606 |
support is implemented (see also the connection's rollback() and commit() |
|
607 |
methods.) |
|
608 |
""" |
|
1524 | 609 |
|
0 | 610 |
def __init__(self, connection, repo, req=None): |
611 |
"""This read-only attribute return a reference to the Connection |
|
612 |
object on which the cursor was created. |
|
613 |
""" |
|
614 |
self.connection = connection |
|
615 |
"""optionnal issuing request instance""" |
|
616 |
self.req = req |
|
617 |
||
618 |
"""This read/write attribute specifies the number of rows to fetch at a |
|
619 |
time with fetchmany(). It defaults to 1 meaning to fetch a single row |
|
620 |
at a time. |
|
1524 | 621 |
|
0 | 622 |
Implementations must observe this value with respect to the fetchmany() |
623 |
method, but are free to interact with the database a single row at a |
|
624 |
time. It may also be used in the implementation of executemany(). |
|
625 |
""" |
|
626 |
self.arraysize = 1 |
|
627 |
||
628 |
self._repo = repo |
|
629 |
self._sessid = connection.sessionid |
|
630 |
self._res = None |
|
631 |
self._closed = None |
|
632 |
self._index = 0 |
|
633 |
||
1524 | 634 |
|
0 | 635 |
def close(self): |
636 |
"""Close the cursor now (rather than whenever __del__ is called). The |
|
637 |
cursor will be unusable from this point forward; an Error (or subclass) |
|
638 |
exception will be raised if any operation is attempted with the cursor. |
|
639 |
""" |
|
640 |
self._closed = True |
|
641 |
||
1524 | 642 |
|
0 | 643 |
def execute(self, operation, parameters=None, eid_key=None, build_descr=True): |
644 |
"""Prepare and execute a database operation (query or command). |
|
645 |
Parameters may be provided as sequence or mapping and will be bound to |
|
646 |
variables in the operation. Variables are specified in a |
|
647 |
database-specific notation (see the module's paramstyle attribute for |
|
648 |
details). |
|
1524 | 649 |
|
0 | 650 |
A reference to the operation will be retained by the cursor. If the |
651 |
same operation object is passed in again, then the cursor can optimize |
|
652 |
its behavior. This is most effective for algorithms where the same |
|
653 |
operation is used, but different parameters are bound to it (many |
|
654 |
times). |
|
1524 | 655 |
|
0 | 656 |
For maximum efficiency when reusing an operation, it is best to use the |
657 |
setinputsizes() method to specify the parameter types and sizes ahead |
|
658 |
of time. It is legal for a parameter to not match the predefined |
|
659 |
information; the implementation should compensate, possibly with a loss |
|
660 |
of efficiency. |
|
1524 | 661 |
|
0 | 662 |
The parameters may also be specified as list of tuples to e.g. insert |
663 |
multiple rows in a single operation, but this kind of usage is |
|
664 |
depreciated: executemany() should be used instead. |
|
1524 | 665 |
|
0 | 666 |
Return values are not defined by the DB-API, but this here it returns a |
667 |
ResultSet object. |
|
668 |
""" |
|
669 |
self._res = res = self._repo.execute(self._sessid, operation, |
|
670 |
parameters, eid_key, build_descr) |
|
671 |
self.req.decorate_rset(res) |
|
672 |
self._index = 0 |
|
673 |
return res |
|
1524 | 674 |
|
0 | 675 |
|
676 |
def executemany(self, operation, seq_of_parameters): |
|
677 |
"""Prepare a database operation (query or command) and then execute it |
|
678 |
against all parameter sequences or mappings found in the sequence |
|
679 |
seq_of_parameters. |
|
1524 | 680 |
|
0 | 681 |
Modules are free to implement this method using multiple calls to the |
682 |
execute() method or by using array operations to have the database |
|
683 |
process the sequence as a whole in one call. |
|
1524 | 684 |
|
0 | 685 |
Use of this method for an operation which produces one or more result |
686 |
sets constitutes undefined behavior, and the implementation is |
|
687 |
permitted (but not required) to raise an exception when it detects that |
|
688 |
a result set has been created by an invocation of the operation. |
|
1524 | 689 |
|
0 | 690 |
The same comments as for execute() also apply accordingly to this |
691 |
method. |
|
1524 | 692 |
|
0 | 693 |
Return values are not defined. |
694 |
""" |
|
695 |
for parameters in seq_of_parameters: |
|
696 |
self.execute(operation, parameters) |
|
697 |
if self._res.rows is not None: |
|
698 |
self._res = None |
|
699 |
raise ProgrammingError('Operation returned a result set') |
|
700 |
||
701 |
||
702 |
def fetchone(self): |
|
703 |
"""Fetch the next row of a query result set, returning a single |
|
704 |
sequence, or None when no more data is available. |
|
1524 | 705 |
|
0 | 706 |
An Error (or subclass) exception is raised if the previous call to |
707 |
execute*() did not produce any result set or no call was issued yet. |
|
708 |
""" |
|
709 |
if self._res is None: |
|
710 |
raise ProgrammingError('No result set') |
|
711 |
row = self._res.rows[self._index] |
|
712 |
self._index += 1 |
|
713 |
return row |
|
714 |
||
1524 | 715 |
|
0 | 716 |
def fetchmany(self, size=None): |
717 |
"""Fetch the next set of rows of a query result, returning a sequence |
|
718 |
of sequences (e.g. a list of tuples). An empty sequence is returned |
|
719 |
when no more rows are available. |
|
1524 | 720 |
|
0 | 721 |
The number of rows to fetch per call is specified by the parameter. If |
722 |
it is not given, the cursor's arraysize determines the number of rows |
|
723 |
to be fetched. The method should try to fetch as many rows as indicated |
|
724 |
by the size parameter. If this is not possible due to the specified |
|
725 |
number of rows not being available, fewer rows may be returned. |
|
1524 | 726 |
|
0 | 727 |
An Error (or subclass) exception is raised if the previous call to |
728 |
execute*() did not produce any result set or no call was issued yet. |
|
1524 | 729 |
|
0 | 730 |
Note there are performance considerations involved with the size |
731 |
parameter. For optimal performance, it is usually best to use the |
|
732 |
arraysize attribute. If the size parameter is used, then it is best |
|
733 |
for it to retain the same value from one fetchmany() call to the next. |
|
734 |
""" |
|
735 |
if self._res is None: |
|
736 |
raise ProgrammingError('No result set') |
|
737 |
if size is None: |
|
738 |
size = self.arraysize |
|
739 |
rows = self._res.rows[self._index:self._index + size] |
|
740 |
self._index += size |
|
741 |
return rows |
|
742 |
||
1524 | 743 |
|
0 | 744 |
def fetchall(self): |
745 |
"""Fetch all (remaining) rows of a query result, returning them as a |
|
746 |
sequence of sequences (e.g. a list of tuples). Note that the cursor's |
|
747 |
arraysize attribute can affect the performance of this operation. |
|
1524 | 748 |
|
0 | 749 |
An Error (or subclass) exception is raised if the previous call to |
750 |
execute*() did not produce any result set or no call was issued yet. |
|
751 |
""" |
|
752 |
if self._res is None: |
|
753 |
raise ProgrammingError('No result set') |
|
754 |
if not self._res.rows: |
|
755 |
return [] |
|
756 |
rows = self._res.rows[self._index:] |
|
757 |
self._index = len(self._res) |
|
758 |
return rows |
|
759 |
||
760 |
||
761 |
def setinputsizes(self, sizes): |
|
762 |
"""This can be used before a call to execute*() to predefine memory |
|
763 |
areas for the operation's parameters. |
|
1524 | 764 |
|
0 | 765 |
sizes is specified as a sequence -- one item for each input parameter. |
766 |
The item should be a Type Object that corresponds to the input that |
|
767 |
will be used, or it should be an integer specifying the maximum length |
|
768 |
of a string parameter. If the item is None, then no predefined memory |
|
769 |
area will be reserved for that column (this is useful to avoid |
|
770 |
predefined areas for large inputs). |
|
1524 | 771 |
|
0 | 772 |
This method would be used before the execute*() method is invoked. |
1524 | 773 |
|
0 | 774 |
Implementations are free to have this method do nothing and users are |
775 |
free to not use it. |
|
776 |
""" |
|
777 |
pass |
|
778 |
||
1524 | 779 |
|
0 | 780 |
def setoutputsize(self, size, column=None): |
781 |
"""Set a column buffer size for fetches of large columns (e.g. LONGs, |
|
782 |
BLOBs, etc.). The column is specified as an index into the result |
|
783 |
sequence. Not specifying the column will set the default size for all |
|
784 |
large columns in the cursor. |
|
1524 | 785 |
|
0 | 786 |
This method would be used before the execute*() method is invoked. |
1524 | 787 |
|
0 | 788 |
Implementations are free to have this method do nothing and users are |
789 |
free to not use it. |
|
1524 | 790 |
""" |
0 | 791 |
pass |
792 |
||
1524 | 793 |
|
0 | 794 |
class LogCursor(Cursor): |
795 |
"""override the standard cursor to log executed queries""" |
|
1524 | 796 |
|
0 | 797 |
def execute(self, operation, parameters=None, eid_key=None, build_descr=True): |
798 |
"""override the standard cursor to log executed queries""" |
|
799 |
tstart, cstart = time(), clock() |
|
800 |
rset = Cursor.execute(self, operation, parameters, eid_key, build_descr) |
|
801 |
self.connection.executed_queries.append((operation, parameters, |
|
802 |
time() - tstart, clock() - cstart)) |
|
803 |
return rset |
|
804 |