author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 24 Feb 2010 11:20:13 +0100 | |
branch | stable |
changeset 4686 | c55606a5c4b0 |
parent 4592 | ddbab12acae0 |
child 4705 | 8ae13d059850 |
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""" |
|
4592
ddbab12acae0
ensure cnx is set first (necessary to introduce of a form object to handle the login form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
266 |
if self.cnx is None: |
ddbab12acae0
ensure cnx is set first (necessary to introduce of a form object to handle the login form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
267 |
return None # before the connection has been established |
0 | 268 |
return self.cnx.get_session_data(key, default, pop) |
1524 | 269 |
|
0 | 270 |
def set_session_data(self, key, value): |
271 |
"""set value associated to `key` in session data""" |
|
272 |
return self.cnx.set_session_data(key, value) |
|
1524 | 273 |
|
0 | 274 |
def del_session_data(self, key): |
275 |
"""remove value associated to `key` in session data""" |
|
276 |
return self.cnx.del_session_data(key) |
|
277 |
||
278 |
def get_shared_data(self, key, default=None, pop=False): |
|
279 |
"""return value associated to `key` in shared data""" |
|
280 |
return self.cnx.get_shared_data(key, default, pop) |
|
1524 | 281 |
|
0 | 282 |
def set_shared_data(self, key, value, querydata=False): |
283 |
"""set value associated to `key` in shared data |
|
284 |
||
285 |
if `querydata` is true, the value will be added to the repository |
|
286 |
session's query data which are cleared on commit/rollback of the current |
|
287 |
transaction, and won't be available through the connexion, only on the |
|
288 |
repository side. |
|
289 |
""" |
|
290 |
return self.cnx.set_shared_data(key, value, querydata) |
|
291 |
||
292 |
# server session compat layer ############################################# |
|
293 |
||
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
|
294 |
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
|
295 |
"""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
|
296 |
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
|
297 |
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
|
298 |
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
|
299 |
|
0 | 300 |
@property |
301 |
def user(self): |
|
302 |
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
|
303 |
self.set_user(self.cnx.user(self, {'lang': self.lang})) |
0 | 304 |
return self._user |
305 |
||
306 |
def set_user(self, user): |
|
307 |
self._user = user |
|
308 |
if user: |
|
309 |
self.set_entity_cache(user) |
|
1524 | 310 |
|
0 | 311 |
def execute(self, *args, **kwargs): |
312 |
"""Session interface compatibility""" |
|
313 |
return self.cursor.execute(*args, **kwargs) |
|
314 |
||
315 |
set_log_methods(DBAPIRequest, getLogger('cubicweb.dbapi')) |
|
1524 | 316 |
|
317 |
||
0 | 318 |
# exceptions ################################################################## |
319 |
||
320 |
class ProgrammingError(Exception): #DatabaseError): |
|
321 |
"""Exception raised for errors that are related to the database's operation |
|
322 |
and not necessarily under the control of the programmer, e.g. an unexpected |
|
323 |
disconnect occurs, the data source name is not found, a transaction could |
|
324 |
not be processed, a memory allocation error occurred during processing, |
|
325 |
etc. |
|
326 |
""" |
|
327 |
||
328 |
# module level objects ######################################################## |
|
329 |
||
330 |
||
331 |
apilevel = '2.0' |
|
332 |
||
333 |
"""Integer constant stating the level of thread safety the interface supports. |
|
334 |
Possible values are: |
|
335 |
||
336 |
0 Threads may not share the module. |
|
337 |
1 Threads may share the module, but not connections. |
|
338 |
2 Threads may share the module and connections. |
|
339 |
3 Threads may share the module, connections and |
|
340 |
cursors. |
|
341 |
||
342 |
Sharing in the above context means that two threads may use a resource without |
|
343 |
wrapping it using a mutex semaphore to implement resource locking. Note that |
|
344 |
you cannot always make external resources thread safe by managing access using |
|
345 |
a mutex: the resource may rely on global variables or other external sources |
|
346 |
that are beyond your control. |
|
347 |
""" |
|
348 |
threadsafety = 1 |
|
349 |
||
350 |
"""String constant stating the type of parameter marker formatting expected by |
|
351 |
the interface. Possible values are : |
|
352 |
||
1524 | 353 |
'qmark' Question mark style, |
0 | 354 |
e.g. '...WHERE name=?' |
1524 | 355 |
'numeric' Numeric, positional style, |
0 | 356 |
e.g. '...WHERE name=:1' |
1524 | 357 |
'named' Named style, |
0 | 358 |
e.g. '...WHERE name=:name' |
1524 | 359 |
'format' ANSI C printf format codes, |
0 | 360 |
e.g. '...WHERE name=%s' |
1524 | 361 |
'pyformat' Python extended format codes, |
0 | 362 |
e.g. '...WHERE name=%(name)s' |
363 |
""" |
|
364 |
paramstyle = 'pyformat' |
|
365 |
||
366 |
||
367 |
# connection object ########################################################### |
|
368 |
||
369 |
class Connection(object): |
|
3258
6536ee4f37f7
update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3110
diff
changeset
|
370 |
"""DB-API 2.0 compatible Connection object for CubicWeb |
0 | 371 |
""" |
372 |
# make exceptions available through the connection object |
|
373 |
ProgrammingError = ProgrammingError |
|
374 |
||
375 |
def __init__(self, repo, cnxid, cnxprops=None): |
|
376 |
self._repo = repo |
|
377 |
self.sessionid = cnxid |
|
378 |
self._close_on_del = getattr(cnxprops, 'close_on_del', True) |
|
379 |
self._cnxtype = getattr(cnxprops, 'cnxtype', 'pyro') |
|
380 |
self._closed = None |
|
381 |
if cnxprops and cnxprops.log_queries: |
|
382 |
self.executed_queries = [] |
|
383 |
self.cursor_class = LogCursor |
|
384 |
else: |
|
385 |
self.cursor_class = Cursor |
|
386 |
self.anonymous_connection = False |
|
387 |
self.vreg = None |
|
388 |
# session's data |
|
389 |
self.data = {} |
|
390 |
||
391 |
def __repr__(self): |
|
392 |
if self.anonymous_connection: |
|
393 |
return '<Connection %s (anonymous)>' % self.sessionid |
|
394 |
return '<Connection %s>' % self.sessionid |
|
395 |
||
396 |
def request(self): |
|
397 |
return DBAPIRequest(self.vreg, self) |
|
1524 | 398 |
|
0 | 399 |
def session_data(self): |
400 |
"""return a dictionnary containing session data""" |
|
401 |
return self.data |
|
1524 | 402 |
|
0 | 403 |
def get_session_data(self, key, default=None, pop=False): |
404 |
"""return value associated to `key` in session data""" |
|
405 |
if pop: |
|
406 |
return self.data.pop(key, default) |
|
407 |
else: |
|
408 |
return self.data.get(key, default) |
|
1524 | 409 |
|
0 | 410 |
def set_session_data(self, key, value): |
411 |
"""set value associated to `key` in session data""" |
|
412 |
self.data[key] = value |
|
1524 | 413 |
|
0 | 414 |
def del_session_data(self, key): |
415 |
"""remove value associated to `key` in session data""" |
|
416 |
try: |
|
417 |
del self.data[key] |
|
418 |
except KeyError: |
|
1524 | 419 |
pass |
0 | 420 |
|
421 |
def check(self): |
|
422 |
"""raise `BadSessionId` if the connection is no more valid""" |
|
1132 | 423 |
self._repo.check_session(self.sessionid) |
0 | 424 |
|
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
|
425 |
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
|
426 |
"""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
|
427 |
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
|
428 |
|
0 | 429 |
def get_shared_data(self, key, default=None, pop=False): |
430 |
"""return value associated to `key` in shared data""" |
|
431 |
return self._repo.get_shared_data(self.sessionid, key, default, pop) |
|
1524 | 432 |
|
0 | 433 |
def set_shared_data(self, key, value, querydata=False): |
434 |
"""set value associated to `key` in shared data |
|
435 |
||
436 |
if `querydata` is true, the value will be added to the repository |
|
437 |
session's query data which are cleared on commit/rollback of the current |
|
438 |
transaction, and won't be available through the connexion, only on the |
|
439 |
repository side. |
|
440 |
""" |
|
441 |
return self._repo.set_shared_data(self.sessionid, key, value, querydata) |
|
1524 | 442 |
|
0 | 443 |
def get_schema(self): |
444 |
"""Return the schema currently used by the repository. |
|
1524 | 445 |
|
0 | 446 |
This is NOT part of the DB-API. |
447 |
""" |
|
448 |
if self._closed is not None: |
|
449 |
raise ProgrammingError('Closed connection') |
|
450 |
return self._repo.get_schema() |
|
451 |
||
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
452 |
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
|
453 |
force_reload=None): |
0 | 454 |
config = self.vreg.config |
455 |
if cubes is _MARKER: |
|
456 |
cubes = self._repo.get_cubes() |
|
457 |
elif cubes is None: |
|
458 |
cubes = () |
|
459 |
else: |
|
460 |
if not isinstance(cubes, (list, tuple)): |
|
461 |
cubes = (cubes,) |
|
462 |
if expand: |
|
463 |
cubes = config.expand_cubes(cubes) |
|
464 |
if subpath is None: |
|
465 |
subpath = esubpath = ('entities', 'views') |
|
466 |
else: |
|
467 |
esubpath = subpath |
|
468 |
if 'views' in subpath: |
|
469 |
esubpath = list(subpath) |
|
470 |
esubpath.remove('views') |
|
471 |
esubpath.append('web/views') |
|
472 |
cubes = reversed([config.cube_dir(p) for p in cubes]) |
|
473 |
vpath = config.build_vregistry_path(cubes, evobjpath=esubpath, |
|
474 |
tvobjpath=subpath) |
|
475 |
self.vreg.register_objects(vpath, force_reload) |
|
476 |
if self._cnxtype == 'inmemory': |
|
477 |
# should reinit hooks manager as well |
|
478 |
hm, config = self._repo.hm, self._repo.config |
|
479 |
hm.set_schema(hm.schema) # reset structure |
|
480 |
hm.register_system_hooks(config) |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2266
diff
changeset
|
481 |
# instance specific hooks |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2266
diff
changeset
|
482 |
if self._repo.config.instance_hooks: |
0 | 483 |
hm.register_hooks(config.load_hooks(self.vreg)) |
1524 | 484 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
485 |
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
|
486 |
|
2496
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
487 |
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
|
488 |
"""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
|
489 |
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
|
490 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
491 |
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
|
492 |
""" |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
493 |
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
|
494 |
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
|
495 |
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
|
496 |
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
|
497 |
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
|
498 |
DBAPIRequest.form = {} |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
499 |
DBAPIRequest.data = {} |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
500 |
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
|
501 |
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
|
502 |
DBAPIRequest.url = fake |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
503 |
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
|
504 |
DBAPIRequest.get_page_data = fake |
3833
5507aa75f601
add set_page_data as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3832
diff
changeset
|
505 |
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
|
506 |
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
|
507 |
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
|
508 |
# 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
|
509 |
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
|
510 |
# 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
|
511 |
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
|
512 |
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
|
513 |
|
0 | 514 |
def source_defs(self): |
515 |
"""Return the definition of sources used by the repository. |
|
1524 | 516 |
|
0 | 517 |
This is NOT part of the DB-API. |
518 |
""" |
|
519 |
if self._closed is not None: |
|
520 |
raise ProgrammingError('Closed connection') |
|
521 |
return self._repo.source_defs() |
|
522 |
||
322
0d9aca19b3d0
make req argument optional
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
169
diff
changeset
|
523 |
def user(self, req=None, props=None): |
0 | 524 |
"""return the User object associated to this connection""" |
525 |
# 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
|
526 |
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
|
527 |
props) |
0 | 528 |
if req is None: |
529 |
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
|
530 |
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
|
531 |
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
|
532 |
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
|
533 |
properties=properties) |
0 | 534 |
user['login'] = login # cache login |
535 |
return user |
|
536 |
||
537 |
def __del__(self): |
|
538 |
"""close the remote connection if necessary""" |
|
539 |
if self._closed is None and self._close_on_del: |
|
540 |
try: |
|
541 |
self.close() |
|
542 |
except: |
|
543 |
pass |
|
1524 | 544 |
|
0 | 545 |
def describe(self, eid): |
546 |
return self._repo.describe(self.sessionid, eid) |
|
1524 | 547 |
|
0 | 548 |
def close(self): |
549 |
"""Close the connection now (rather than whenever __del__ is called). |
|
1524 | 550 |
|
0 | 551 |
The connection will be unusable from this point forward; an Error (or |
552 |
subclass) exception will be raised if any operation is attempted with |
|
553 |
the connection. The same applies to all cursor objects trying to use the |
|
554 |
connection. Note that closing a connection without committing the |
|
555 |
changes first will cause an implicit rollback to be performed. |
|
556 |
""" |
|
557 |
if self._closed: |
|
558 |
raise ProgrammingError('Connection is already closed') |
|
559 |
self._repo.close(self.sessionid) |
|
560 |
self._closed = 1 |
|
561 |
||
562 |
def commit(self): |
|
563 |
"""Commit any pending transaction to the database. Note that if the |
|
564 |
database supports an auto-commit feature, this must be initially off. An |
|
565 |
interface method may be provided to turn it back on. |
|
1524 | 566 |
|
0 | 567 |
Database modules that do not support transactions should implement this |
568 |
method with void functionality. |
|
569 |
""" |
|
570 |
if not self._closed is None: |
|
571 |
raise ProgrammingError('Connection is already closed') |
|
572 |
self._repo.commit(self.sessionid) |
|
573 |
||
574 |
def rollback(self): |
|
575 |
"""This method is optional since not all databases provide transaction |
|
576 |
support. |
|
1524 | 577 |
|
0 | 578 |
In case a database does provide transactions this method causes the the |
579 |
database to roll back to the start of any pending transaction. Closing |
|
580 |
a connection without committing the changes first will cause an implicit |
|
581 |
rollback to be performed. |
|
582 |
""" |
|
583 |
if not self._closed is None: |
|
584 |
raise ProgrammingError('Connection is already closed') |
|
585 |
self._repo.rollback(self.sessionid) |
|
586 |
||
587 |
def cursor(self, req=None): |
|
588 |
"""Return a new Cursor Object using the connection. If the database |
|
589 |
does not provide a direct cursor concept, the module will have to |
|
590 |
emulate cursors using other means to the extent needed by this |
|
591 |
specification. |
|
592 |
""" |
|
593 |
if self._closed is not None: |
|
594 |
raise ProgrammingError('Can\'t get cursor on closed connection') |
|
595 |
if req is None: |
|
596 |
req = self.request() |
|
597 |
return self.cursor_class(self, self._repo, req=req) |
|
598 |
||
599 |
||
600 |
# cursor object ############################################################### |
|
601 |
||
602 |
class Cursor(object): |
|
603 |
"""These objects represent a database cursor, which is used to manage the |
|
604 |
context of a fetch operation. Cursors created from the same connection are |
|
605 |
not isolated, i.e., any changes done to the database by a cursor are |
|
606 |
immediately visible by the other cursors. Cursors created from different |
|
607 |
connections can or can not be isolated, depending on how the transaction |
|
608 |
support is implemented (see also the connection's rollback() and commit() |
|
609 |
methods.) |
|
610 |
""" |
|
1524 | 611 |
|
0 | 612 |
def __init__(self, connection, repo, req=None): |
613 |
"""This read-only attribute return a reference to the Connection |
|
614 |
object on which the cursor was created. |
|
615 |
""" |
|
616 |
self.connection = connection |
|
617 |
"""optionnal issuing request instance""" |
|
618 |
self.req = req |
|
619 |
||
620 |
"""This read/write attribute specifies the number of rows to fetch at a |
|
621 |
time with fetchmany(). It defaults to 1 meaning to fetch a single row |
|
622 |
at a time. |
|
1524 | 623 |
|
0 | 624 |
Implementations must observe this value with respect to the fetchmany() |
625 |
method, but are free to interact with the database a single row at a |
|
626 |
time. It may also be used in the implementation of executemany(). |
|
627 |
""" |
|
628 |
self.arraysize = 1 |
|
629 |
||
630 |
self._repo = repo |
|
631 |
self._sessid = connection.sessionid |
|
632 |
self._res = None |
|
633 |
self._closed = None |
|
634 |
self._index = 0 |
|
635 |
||
1524 | 636 |
|
0 | 637 |
def close(self): |
638 |
"""Close the cursor now (rather than whenever __del__ is called). The |
|
639 |
cursor will be unusable from this point forward; an Error (or subclass) |
|
640 |
exception will be raised if any operation is attempted with the cursor. |
|
641 |
""" |
|
642 |
self._closed = True |
|
643 |
||
1524 | 644 |
|
0 | 645 |
def execute(self, operation, parameters=None, eid_key=None, build_descr=True): |
646 |
"""Prepare and execute a database operation (query or command). |
|
647 |
Parameters may be provided as sequence or mapping and will be bound to |
|
648 |
variables in the operation. Variables are specified in a |
|
649 |
database-specific notation (see the module's paramstyle attribute for |
|
650 |
details). |
|
1524 | 651 |
|
0 | 652 |
A reference to the operation will be retained by the cursor. If the |
653 |
same operation object is passed in again, then the cursor can optimize |
|
654 |
its behavior. This is most effective for algorithms where the same |
|
655 |
operation is used, but different parameters are bound to it (many |
|
656 |
times). |
|
1524 | 657 |
|
0 | 658 |
For maximum efficiency when reusing an operation, it is best to use the |
659 |
setinputsizes() method to specify the parameter types and sizes ahead |
|
660 |
of time. It is legal for a parameter to not match the predefined |
|
661 |
information; the implementation should compensate, possibly with a loss |
|
662 |
of efficiency. |
|
1524 | 663 |
|
0 | 664 |
The parameters may also be specified as list of tuples to e.g. insert |
665 |
multiple rows in a single operation, but this kind of usage is |
|
666 |
depreciated: executemany() should be used instead. |
|
1524 | 667 |
|
0 | 668 |
Return values are not defined by the DB-API, but this here it returns a |
669 |
ResultSet object. |
|
670 |
""" |
|
671 |
self._res = res = self._repo.execute(self._sessid, operation, |
|
672 |
parameters, eid_key, build_descr) |
|
673 |
self.req.decorate_rset(res) |
|
674 |
self._index = 0 |
|
675 |
return res |
|
1524 | 676 |
|
0 | 677 |
|
678 |
def executemany(self, operation, seq_of_parameters): |
|
679 |
"""Prepare a database operation (query or command) and then execute it |
|
680 |
against all parameter sequences or mappings found in the sequence |
|
681 |
seq_of_parameters. |
|
1524 | 682 |
|
0 | 683 |
Modules are free to implement this method using multiple calls to the |
684 |
execute() method or by using array operations to have the database |
|
685 |
process the sequence as a whole in one call. |
|
1524 | 686 |
|
0 | 687 |
Use of this method for an operation which produces one or more result |
688 |
sets constitutes undefined behavior, and the implementation is |
|
689 |
permitted (but not required) to raise an exception when it detects that |
|
690 |
a result set has been created by an invocation of the operation. |
|
1524 | 691 |
|
0 | 692 |
The same comments as for execute() also apply accordingly to this |
693 |
method. |
|
1524 | 694 |
|
0 | 695 |
Return values are not defined. |
696 |
""" |
|
697 |
for parameters in seq_of_parameters: |
|
698 |
self.execute(operation, parameters) |
|
699 |
if self._res.rows is not None: |
|
700 |
self._res = None |
|
701 |
raise ProgrammingError('Operation returned a result set') |
|
702 |
||
703 |
||
704 |
def fetchone(self): |
|
705 |
"""Fetch the next row of a query result set, returning a single |
|
706 |
sequence, or None when no more data is available. |
|
1524 | 707 |
|
0 | 708 |
An Error (or subclass) exception is raised if the previous call to |
709 |
execute*() did not produce any result set or no call was issued yet. |
|
710 |
""" |
|
711 |
if self._res is None: |
|
712 |
raise ProgrammingError('No result set') |
|
713 |
row = self._res.rows[self._index] |
|
714 |
self._index += 1 |
|
715 |
return row |
|
716 |
||
1524 | 717 |
|
0 | 718 |
def fetchmany(self, size=None): |
719 |
"""Fetch the next set of rows of a query result, returning a sequence |
|
720 |
of sequences (e.g. a list of tuples). An empty sequence is returned |
|
721 |
when no more rows are available. |
|
1524 | 722 |
|
0 | 723 |
The number of rows to fetch per call is specified by the parameter. If |
724 |
it is not given, the cursor's arraysize determines the number of rows |
|
725 |
to be fetched. The method should try to fetch as many rows as indicated |
|
726 |
by the size parameter. If this is not possible due to the specified |
|
727 |
number of rows not being available, fewer rows may be returned. |
|
1524 | 728 |
|
0 | 729 |
An Error (or subclass) exception is raised if the previous call to |
730 |
execute*() did not produce any result set or no call was issued yet. |
|
1524 | 731 |
|
0 | 732 |
Note there are performance considerations involved with the size |
733 |
parameter. For optimal performance, it is usually best to use the |
|
734 |
arraysize attribute. If the size parameter is used, then it is best |
|
735 |
for it to retain the same value from one fetchmany() call to the next. |
|
736 |
""" |
|
737 |
if self._res is None: |
|
738 |
raise ProgrammingError('No result set') |
|
739 |
if size is None: |
|
740 |
size = self.arraysize |
|
741 |
rows = self._res.rows[self._index:self._index + size] |
|
742 |
self._index += size |
|
743 |
return rows |
|
744 |
||
1524 | 745 |
|
0 | 746 |
def fetchall(self): |
747 |
"""Fetch all (remaining) rows of a query result, returning them as a |
|
748 |
sequence of sequences (e.g. a list of tuples). Note that the cursor's |
|
749 |
arraysize attribute can affect the performance of this operation. |
|
1524 | 750 |
|
0 | 751 |
An Error (or subclass) exception is raised if the previous call to |
752 |
execute*() did not produce any result set or no call was issued yet. |
|
753 |
""" |
|
754 |
if self._res is None: |
|
755 |
raise ProgrammingError('No result set') |
|
756 |
if not self._res.rows: |
|
757 |
return [] |
|
758 |
rows = self._res.rows[self._index:] |
|
759 |
self._index = len(self._res) |
|
760 |
return rows |
|
761 |
||
762 |
||
763 |
def setinputsizes(self, sizes): |
|
764 |
"""This can be used before a call to execute*() to predefine memory |
|
765 |
areas for the operation's parameters. |
|
1524 | 766 |
|
0 | 767 |
sizes is specified as a sequence -- one item for each input parameter. |
768 |
The item should be a Type Object that corresponds to the input that |
|
769 |
will be used, or it should be an integer specifying the maximum length |
|
770 |
of a string parameter. If the item is None, then no predefined memory |
|
771 |
area will be reserved for that column (this is useful to avoid |
|
772 |
predefined areas for large inputs). |
|
1524 | 773 |
|
0 | 774 |
This method would be used before the execute*() method is invoked. |
1524 | 775 |
|
0 | 776 |
Implementations are free to have this method do nothing and users are |
777 |
free to not use it. |
|
778 |
""" |
|
779 |
pass |
|
780 |
||
1524 | 781 |
|
0 | 782 |
def setoutputsize(self, size, column=None): |
783 |
"""Set a column buffer size for fetches of large columns (e.g. LONGs, |
|
784 |
BLOBs, etc.). The column is specified as an index into the result |
|
785 |
sequence. Not specifying the column will set the default size for all |
|
786 |
large columns in the cursor. |
|
1524 | 787 |
|
0 | 788 |
This method would be used before the execute*() method is invoked. |
1524 | 789 |
|
0 | 790 |
Implementations are free to have this method do nothing and users are |
791 |
free to not use it. |
|
1524 | 792 |
""" |
0 | 793 |
pass |
794 |
||
1524 | 795 |
|
0 | 796 |
class LogCursor(Cursor): |
797 |
"""override the standard cursor to log executed queries""" |
|
1524 | 798 |
|
0 | 799 |
def execute(self, operation, parameters=None, eid_key=None, build_descr=True): |
800 |
"""override the standard cursor to log executed queries""" |
|
801 |
tstart, cstart = time(), clock() |
|
802 |
rset = Cursor.execute(self, operation, parameters, eid_key, build_descr) |
|
803 |
self.connection.executed_queries.append((operation, parameters, |
|
804 |
time() - tstart, clock() - cstart)) |
|
805 |
return rset |
|
806 |