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