author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 27 Nov 2009 09:08:54 +0100 | |
branch | stable |
changeset 3919 | f91bd15f427c |
parent 3833 | 5507aa75f601 |
child 3890 | d7a270f50f54 |
child 4212 | ab6573088b4a |
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, |
3707
78596919ede3
[c-c] fixes for shell w/ pyro instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
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 |
3832
ce8dd861f442
#473188: missing get_page_data when patching bare db-api request for web compatibility
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3707
diff
changeset
|
500 |
DBAPIRequest.get_page_data = fake |
3833
5507aa75f601
add set_page_data as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3832
diff
changeset
|
501 |
DBAPIRequest.set_page_data = fake |
2496
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
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 |