author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 19 Oct 2009 20:01:34 +0200 | |
changeset 3738 | 89871767fa82 |
parent 3720 | 5376aaadd16b |
child 3890 | d7a270f50f54 |
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 |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1923
diff
changeset
|
8 |
:copyright: 2001-2009 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 |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
502 |
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
|
503 |
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
|
504 |
# 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
|
505 |
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
|
506 |
# 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
|
507 |
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
|
508 |
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
|
509 |
|
0 | 510 |
def source_defs(self): |
511 |
"""Return the definition of sources used by the repository. |
|
1524 | 512 |
|
0 | 513 |
This is NOT part of the DB-API. |
514 |
""" |
|
515 |
if self._closed is not None: |
|
516 |
raise ProgrammingError('Closed connection') |
|
517 |
return self._repo.source_defs() |
|
518 |
||
322
0d9aca19b3d0
make req argument optional
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
169
diff
changeset
|
519 |
def user(self, req=None, props=None): |
0 | 520 |
"""return the User object associated to this connection""" |
521 |
# 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
|
522 |
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
|
523 |
props) |
0 | 524 |
if req is None: |
525 |
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
|
526 |
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
|
527 |
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
|
528 |
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
|
529 |
properties=properties) |
0 | 530 |
user['login'] = login # cache login |
531 |
return user |
|
532 |
||
533 |
def __del__(self): |
|
534 |
"""close the remote connection if necessary""" |
|
535 |
if self._closed is None and self._close_on_del: |
|
536 |
try: |
|
537 |
self.close() |
|
538 |
except: |
|
539 |
pass |
|
1524 | 540 |
|
0 | 541 |
def describe(self, eid): |
542 |
return self._repo.describe(self.sessionid, eid) |
|
1524 | 543 |
|
0 | 544 |
def close(self): |
545 |
"""Close the connection now (rather than whenever __del__ is called). |
|
1524 | 546 |
|
0 | 547 |
The connection will be unusable from this point forward; an Error (or |
548 |
subclass) exception will be raised if any operation is attempted with |
|
549 |
the connection. The same applies to all cursor objects trying to use the |
|
550 |
connection. Note that closing a connection without committing the |
|
551 |
changes first will cause an implicit rollback to be performed. |
|
552 |
""" |
|
553 |
if self._closed: |
|
554 |
raise ProgrammingError('Connection is already closed') |
|
555 |
self._repo.close(self.sessionid) |
|
556 |
self._closed = 1 |
|
557 |
||
558 |
def commit(self): |
|
559 |
"""Commit any pending transaction to the database. Note that if the |
|
560 |
database supports an auto-commit feature, this must be initially off. An |
|
561 |
interface method may be provided to turn it back on. |
|
1524 | 562 |
|
0 | 563 |
Database modules that do not support transactions should implement this |
564 |
method with void functionality. |
|
565 |
""" |
|
566 |
if not self._closed is None: |
|
567 |
raise ProgrammingError('Connection is already closed') |
|
568 |
self._repo.commit(self.sessionid) |
|
569 |
||
570 |
def rollback(self): |
|
571 |
"""This method is optional since not all databases provide transaction |
|
572 |
support. |
|
1524 | 573 |
|
0 | 574 |
In case a database does provide transactions this method causes the the |
575 |
database to roll back to the start of any pending transaction. Closing |
|
576 |
a connection without committing the changes first will cause an implicit |
|
577 |
rollback to be performed. |
|
578 |
""" |
|
579 |
if not self._closed is None: |
|
580 |
raise ProgrammingError('Connection is already closed') |
|
581 |
self._repo.rollback(self.sessionid) |
|
582 |
||
583 |
def cursor(self, req=None): |
|
584 |
"""Return a new Cursor Object using the connection. If the database |
|
585 |
does not provide a direct cursor concept, the module will have to |
|
586 |
emulate cursors using other means to the extent needed by this |
|
587 |
specification. |
|
588 |
""" |
|
589 |
if self._closed is not None: |
|
590 |
raise ProgrammingError('Can\'t get cursor on closed connection') |
|
591 |
if req is None: |
|
592 |
req = self.request() |
|
593 |
return self.cursor_class(self, self._repo, req=req) |
|
594 |
||
595 |
||
596 |
# cursor object ############################################################### |
|
597 |
||
598 |
class Cursor(object): |
|
599 |
"""These objects represent a database cursor, which is used to manage the |
|
600 |
context of a fetch operation. Cursors created from the same connection are |
|
601 |
not isolated, i.e., any changes done to the database by a cursor are |
|
602 |
immediately visible by the other cursors. Cursors created from different |
|
603 |
connections can or can not be isolated, depending on how the transaction |
|
604 |
support is implemented (see also the connection's rollback() and commit() |
|
605 |
methods.) |
|
606 |
""" |
|
1524 | 607 |
|
0 | 608 |
def __init__(self, connection, repo, req=None): |
609 |
"""This read-only attribute return a reference to the Connection |
|
610 |
object on which the cursor was created. |
|
611 |
""" |
|
612 |
self.connection = connection |
|
613 |
"""optionnal issuing request instance""" |
|
614 |
self.req = req |
|
615 |
||
616 |
"""This read/write attribute specifies the number of rows to fetch at a |
|
617 |
time with fetchmany(). It defaults to 1 meaning to fetch a single row |
|
618 |
at a time. |
|
1524 | 619 |
|
0 | 620 |
Implementations must observe this value with respect to the fetchmany() |
621 |
method, but are free to interact with the database a single row at a |
|
622 |
time. It may also be used in the implementation of executemany(). |
|
623 |
""" |
|
624 |
self.arraysize = 1 |
|
625 |
||
626 |
self._repo = repo |
|
627 |
self._sessid = connection.sessionid |
|
628 |
self._res = None |
|
629 |
self._closed = None |
|
630 |
self._index = 0 |
|
631 |
||
1524 | 632 |
|
0 | 633 |
def close(self): |
634 |
"""Close the cursor now (rather than whenever __del__ is called). The |
|
635 |
cursor will be unusable from this point forward; an Error (or subclass) |
|
636 |
exception will be raised if any operation is attempted with the cursor. |
|
637 |
""" |
|
638 |
self._closed = True |
|
639 |
||
1524 | 640 |
|
0 | 641 |
def execute(self, operation, parameters=None, eid_key=None, build_descr=True): |
642 |
"""Prepare and execute a database operation (query or command). |
|
643 |
Parameters may be provided as sequence or mapping and will be bound to |
|
644 |
variables in the operation. Variables are specified in a |
|
645 |
database-specific notation (see the module's paramstyle attribute for |
|
646 |
details). |
|
1524 | 647 |
|
0 | 648 |
A reference to the operation will be retained by the cursor. If the |
649 |
same operation object is passed in again, then the cursor can optimize |
|
650 |
its behavior. This is most effective for algorithms where the same |
|
651 |
operation is used, but different parameters are bound to it (many |
|
652 |
times). |
|
1524 | 653 |
|
0 | 654 |
For maximum efficiency when reusing an operation, it is best to use the |
655 |
setinputsizes() method to specify the parameter types and sizes ahead |
|
656 |
of time. It is legal for a parameter to not match the predefined |
|
657 |
information; the implementation should compensate, possibly with a loss |
|
658 |
of efficiency. |
|
1524 | 659 |
|
0 | 660 |
The parameters may also be specified as list of tuples to e.g. insert |
661 |
multiple rows in a single operation, but this kind of usage is |
|
662 |
depreciated: executemany() should be used instead. |
|
1524 | 663 |
|
0 | 664 |
Return values are not defined by the DB-API, but this here it returns a |
665 |
ResultSet object. |
|
666 |
""" |
|
667 |
self._res = res = self._repo.execute(self._sessid, operation, |
|
668 |
parameters, eid_key, build_descr) |
|
669 |
self.req.decorate_rset(res) |
|
670 |
self._index = 0 |
|
671 |
return res |
|
1524 | 672 |
|
0 | 673 |
|
674 |
def executemany(self, operation, seq_of_parameters): |
|
675 |
"""Prepare a database operation (query or command) and then execute it |
|
676 |
against all parameter sequences or mappings found in the sequence |
|
677 |
seq_of_parameters. |
|
1524 | 678 |
|
0 | 679 |
Modules are free to implement this method using multiple calls to the |
680 |
execute() method or by using array operations to have the database |
|
681 |
process the sequence as a whole in one call. |
|
1524 | 682 |
|
0 | 683 |
Use of this method for an operation which produces one or more result |
684 |
sets constitutes undefined behavior, and the implementation is |
|
685 |
permitted (but not required) to raise an exception when it detects that |
|
686 |
a result set has been created by an invocation of the operation. |
|
1524 | 687 |
|
0 | 688 |
The same comments as for execute() also apply accordingly to this |
689 |
method. |
|
1524 | 690 |
|
0 | 691 |
Return values are not defined. |
692 |
""" |
|
693 |
for parameters in seq_of_parameters: |
|
694 |
self.execute(operation, parameters) |
|
695 |
if self._res.rows is not None: |
|
696 |
self._res = None |
|
697 |
raise ProgrammingError('Operation returned a result set') |
|
698 |
||
699 |
||
700 |
def fetchone(self): |
|
701 |
"""Fetch the next row of a query result set, returning a single |
|
702 |
sequence, or None when no more data is available. |
|
1524 | 703 |
|
0 | 704 |
An Error (or subclass) exception is raised if the previous call to |
705 |
execute*() did not produce any result set or no call was issued yet. |
|
706 |
""" |
|
707 |
if self._res is None: |
|
708 |
raise ProgrammingError('No result set') |
|
709 |
row = self._res.rows[self._index] |
|
710 |
self._index += 1 |
|
711 |
return row |
|
712 |
||
1524 | 713 |
|
0 | 714 |
def fetchmany(self, size=None): |
715 |
"""Fetch the next set of rows of a query result, returning a sequence |
|
716 |
of sequences (e.g. a list of tuples). An empty sequence is returned |
|
717 |
when no more rows are available. |
|
1524 | 718 |
|
0 | 719 |
The number of rows to fetch per call is specified by the parameter. If |
720 |
it is not given, the cursor's arraysize determines the number of rows |
|
721 |
to be fetched. The method should try to fetch as many rows as indicated |
|
722 |
by the size parameter. If this is not possible due to the specified |
|
723 |
number of rows not being available, fewer rows may be returned. |
|
1524 | 724 |
|
0 | 725 |
An Error (or subclass) exception is raised if the previous call to |
726 |
execute*() did not produce any result set or no call was issued yet. |
|
1524 | 727 |
|
0 | 728 |
Note there are performance considerations involved with the size |
729 |
parameter. For optimal performance, it is usually best to use the |
|
730 |
arraysize attribute. If the size parameter is used, then it is best |
|
731 |
for it to retain the same value from one fetchmany() call to the next. |
|
732 |
""" |
|
733 |
if self._res is None: |
|
734 |
raise ProgrammingError('No result set') |
|
735 |
if size is None: |
|
736 |
size = self.arraysize |
|
737 |
rows = self._res.rows[self._index:self._index + size] |
|
738 |
self._index += size |
|
739 |
return rows |
|
740 |
||
1524 | 741 |
|
0 | 742 |
def fetchall(self): |
743 |
"""Fetch all (remaining) rows of a query result, returning them as a |
|
744 |
sequence of sequences (e.g. a list of tuples). Note that the cursor's |
|
745 |
arraysize attribute can affect the performance of this operation. |
|
1524 | 746 |
|
0 | 747 |
An Error (or subclass) exception is raised if the previous call to |
748 |
execute*() did not produce any result set or no call was issued yet. |
|
749 |
""" |
|
750 |
if self._res is None: |
|
751 |
raise ProgrammingError('No result set') |
|
752 |
if not self._res.rows: |
|
753 |
return [] |
|
754 |
rows = self._res.rows[self._index:] |
|
755 |
self._index = len(self._res) |
|
756 |
return rows |
|
757 |
||
758 |
||
759 |
def setinputsizes(self, sizes): |
|
760 |
"""This can be used before a call to execute*() to predefine memory |
|
761 |
areas for the operation's parameters. |
|
1524 | 762 |
|
0 | 763 |
sizes is specified as a sequence -- one item for each input parameter. |
764 |
The item should be a Type Object that corresponds to the input that |
|
765 |
will be used, or it should be an integer specifying the maximum length |
|
766 |
of a string parameter. If the item is None, then no predefined memory |
|
767 |
area will be reserved for that column (this is useful to avoid |
|
768 |
predefined areas for large inputs). |
|
1524 | 769 |
|
0 | 770 |
This method would be used before the execute*() method is invoked. |
1524 | 771 |
|
0 | 772 |
Implementations are free to have this method do nothing and users are |
773 |
free to not use it. |
|
774 |
""" |
|
775 |
pass |
|
776 |
||
1524 | 777 |
|
0 | 778 |
def setoutputsize(self, size, column=None): |
779 |
"""Set a column buffer size for fetches of large columns (e.g. LONGs, |
|
780 |
BLOBs, etc.). The column is specified as an index into the result |
|
781 |
sequence. Not specifying the column will set the default size for all |
|
782 |
large columns in the cursor. |
|
1524 | 783 |
|
0 | 784 |
This method would be used before the execute*() method is invoked. |
1524 | 785 |
|
0 | 786 |
Implementations are free to have this method do nothing and users are |
787 |
free to not use it. |
|
1524 | 788 |
""" |
0 | 789 |
pass |
790 |
||
1524 | 791 |
|
0 | 792 |
class LogCursor(Cursor): |
793 |
"""override the standard cursor to log executed queries""" |
|
1524 | 794 |
|
0 | 795 |
def execute(self, operation, parameters=None, eid_key=None, build_descr=True): |
796 |
"""override the standard cursor to log executed queries""" |
|
797 |
tstart, cstart = time(), clock() |
|
798 |
rset = Cursor.execute(self, operation, parameters, eid_key, build_descr) |
|
799 |
self.connection.executed_queries.append((operation, parameters, |
|
800 |
time() - tstart, clock() - cstart)) |
|
801 |
return rset |
|
802 |