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