author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 25 Sep 2009 11:50:22 +0200 | |
changeset 3477 | 8a9e26b93bb7 |
parent 3379 | 9192ba07890d |
child 3589 | a5432f99f2d9 |
permissions | -rw-r--r-- |
0 | 1 |
"""Repository users' and internal' sessions. |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1880
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
: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:
1880
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
import sys |
|
11 |
import threading |
|
12 |
from time import time |
|
13 |
||
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2466
diff
changeset
|
14 |
from logilab.common.deprecation import deprecated |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
15 |
from rql.nodes import VariableRef, Function, ETYPE_PYOBJ_MAP, etype_from_pyobj |
0 | 16 |
from yams import BASE_TYPES |
17 |
||
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
18 |
from cubicweb import Binary, UnknownEid |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
19 |
from cubicweb.req import RequestSessionBase |
0 | 20 |
from cubicweb.dbapi import ConnectionProperties |
940
15dcdc863965
fix imports : common.utils -> utils
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
746
diff
changeset
|
21 |
from cubicweb.utils import make_uid |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3198
diff
changeset
|
22 |
from cubicweb.rqlrewrite import RQLRewriter |
0 | 23 |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
24 |
ETYPE_PYOBJ_MAP[Binary] = 'Bytes' |
0 | 25 |
|
26 |
def is_final(rqlst, variable, args): |
|
27 |
# try to find if this is a final var or not |
|
28 |
for select in rqlst.children: |
|
29 |
for sol in select.solutions: |
|
30 |
etype = variable.get_type(sol, args) |
|
31 |
if etype is None: |
|
32 |
continue |
|
33 |
if etype in BASE_TYPES: |
|
34 |
return True |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
35 |
return False |
0 | 36 |
|
37 |
def _make_description(selected, args, solution): |
|
38 |
"""return a description for a result set""" |
|
39 |
description = [] |
|
40 |
for term in selected: |
|
41 |
description.append(term.get_type(solution, args)) |
|
42 |
return description |
|
43 |
||
44 |
||
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
45 |
class Session(RequestSessionBase): |
0 | 46 |
"""tie session id, user, connections pool and other session data all |
47 |
together |
|
48 |
""" |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
49 |
|
0 | 50 |
def __init__(self, user, repo, cnxprops=None, _id=None): |
51 |
super(Session, self).__init__(repo.vreg) |
|
52 |
self.id = _id or make_uid(user.login.encode('UTF8')) |
|
53 |
cnxprops = cnxprops or ConnectionProperties('inmemory') |
|
54 |
self.user = user |
|
55 |
self.repo = repo |
|
56 |
self.cnxtype = cnxprops.cnxtype |
|
57 |
self.creation = time() |
|
58 |
self.timestamp = self.creation |
|
59 |
self.is_internal_session = False |
|
60 |
self.is_super_session = False |
|
2570
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
61 |
self.default_mode = 'read' |
0 | 62 |
# short cut to querier .execute method |
63 |
self._execute = repo.querier.execute |
|
64 |
# shared data, used to communicate extra information between the client |
|
65 |
# and the rql server |
|
66 |
self.data = {} |
|
67 |
# i18n initialization |
|
68 |
self.set_language(cnxprops.lang) |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
69 |
# internals |
0 | 70 |
self._threaddata = threading.local() |
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
71 |
self._threads_in_transaction = set() |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
72 |
self._closed = False |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
73 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
74 |
def __str__(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
75 |
return '<%ssession %s (%s 0x%x)>' % (self.cnxtype, self.user.login, |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
76 |
self.id, id(self)) |
2604
6b55a2a81fd8
[R repo session] add_relation method use in hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
77 |
|
3112
873202e181bb
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:
3074
diff
changeset
|
78 |
def hijack_user(self, user): |
873202e181bb
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:
3074
diff
changeset
|
79 |
"""return a fake request/session using specified user""" |
873202e181bb
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:
3074
diff
changeset
|
80 |
session = Session(user, self.repo) |
3161
005f745315b0
[notification] should get actual session here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3112
diff
changeset
|
81 |
session._threaddata = self.actual_session()._threaddata |
3112
873202e181bb
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:
3074
diff
changeset
|
82 |
return session |
873202e181bb
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:
3074
diff
changeset
|
83 |
|
3197
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
84 |
def _super_call(self, __cb, *args, **kwargs): |
2604
6b55a2a81fd8
[R repo session] add_relation method use in hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
85 |
if self.is_super_session: |
3197
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
86 |
__cb(self, *args, **kwargs) |
2604
6b55a2a81fd8
[R repo session] add_relation method use in hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
87 |
return |
6b55a2a81fd8
[R repo session] add_relation method use in hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
88 |
self.is_super_session = True |
6b55a2a81fd8
[R repo session] add_relation method use in hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
89 |
try: |
3197
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
90 |
__cb(self, *args, **kwargs) |
2604
6b55a2a81fd8
[R repo session] add_relation method use in hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
91 |
finally: |
6b55a2a81fd8
[R repo session] add_relation method use in hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
92 |
self.is_super_session = False |
6b55a2a81fd8
[R repo session] add_relation method use in hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
93 |
|
3112
873202e181bb
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:
3074
diff
changeset
|
94 |
def add_relation(self, fromeid, rtype, toeid): |
873202e181bb
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:
3074
diff
changeset
|
95 |
"""provide direct access to the repository method to add a relation. |
873202e181bb
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:
3074
diff
changeset
|
96 |
|
873202e181bb
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:
3074
diff
changeset
|
97 |
This is equivalent to the following rql query: |
873202e181bb
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:
3074
diff
changeset
|
98 |
|
873202e181bb
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:
3074
diff
changeset
|
99 |
SET X rtype Y WHERE X eid fromeid, T eid toeid |
873202e181bb
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:
3074
diff
changeset
|
100 |
|
873202e181bb
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:
3074
diff
changeset
|
101 |
without read security check but also all the burden of rql execution. |
873202e181bb
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:
3074
diff
changeset
|
102 |
You may use this in hooks when you know both eids of the relation you |
873202e181bb
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:
3074
diff
changeset
|
103 |
want to add. |
873202e181bb
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:
3074
diff
changeset
|
104 |
""" |
3197
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
105 |
if self.vreg.schema[rtype].inlined: |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
106 |
entity = self.entity_from_eid(fromeid) |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
107 |
entity[rtype] = toeid |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
108 |
self._super_call(self.repo.glob_update_entity, |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
109 |
entity, set((rtype,))) |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
110 |
else: |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
111 |
self._super_call(self.repo.glob_add_relation, |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
112 |
fromeid, rtype, toeid) |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
113 |
|
3112
873202e181bb
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:
3074
diff
changeset
|
114 |
def delete_relation(self, fromeid, rtype, toeid): |
873202e181bb
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:
3074
diff
changeset
|
115 |
"""provide direct access to the repository method to delete a relation. |
873202e181bb
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:
3074
diff
changeset
|
116 |
|
873202e181bb
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:
3074
diff
changeset
|
117 |
This is equivalent to the following rql query: |
873202e181bb
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:
3074
diff
changeset
|
118 |
|
873202e181bb
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:
3074
diff
changeset
|
119 |
DELETE X rtype Y WHERE X eid fromeid, T eid toeid |
873202e181bb
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:
3074
diff
changeset
|
120 |
|
873202e181bb
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:
3074
diff
changeset
|
121 |
without read security check but also all the burden of rql execution. |
873202e181bb
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:
3074
diff
changeset
|
122 |
You may use this in hooks when you know both eids of the relation you |
873202e181bb
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:
3074
diff
changeset
|
123 |
want to delete. |
873202e181bb
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:
3074
diff
changeset
|
124 |
""" |
3197
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
125 |
if self.vreg.schema[rtype].inlined: |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
126 |
entity = self.entity_from_eid(fromeid) |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
127 |
entity[rtype] = None |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
128 |
self._super_call(self.repo.glob_update_entity, |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
129 |
entity, set((rtype,))) |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
130 |
else: |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
131 |
self._super_call(self.repo.glob_delete_relation, |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
132 |
fromeid, rtype, toeid) |
3112
873202e181bb
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:
3074
diff
changeset
|
133 |
|
873202e181bb
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:
3074
diff
changeset
|
134 |
# relations cache handling ################################################# |
873202e181bb
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:
3074
diff
changeset
|
135 |
|
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
136 |
def update_rel_cache_add(self, subject, rtype, object, symetric=False): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
137 |
self._update_entity_rel_cache_add(subject, rtype, 'subject', object) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
138 |
if symetric: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
139 |
self._update_entity_rel_cache_add(object, rtype, 'subject', subject) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
140 |
else: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
141 |
self._update_entity_rel_cache_add(object, rtype, 'object', subject) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
142 |
|
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
143 |
def update_rel_cache_del(self, subject, rtype, object, symetric=False): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
144 |
self._update_entity_rel_cache_del(subject, rtype, 'subject', object) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
145 |
if symetric: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
146 |
self._update_entity_rel_cache_del(object, rtype, 'object', object) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
147 |
else: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
148 |
self._update_entity_rel_cache_del(object, rtype, 'object', subject) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
149 |
|
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
150 |
def _rel_cache(self, eid, rtype, role): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
151 |
try: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
152 |
entity = self.entity_cache(eid) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
153 |
except KeyError: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
154 |
return |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
155 |
return entity.relation_cached(rtype, role) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
156 |
|
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
157 |
def _update_entity_rel_cache_add(self, eid, rtype, role, targeteid): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
158 |
rcache = self._rel_cache(eid, rtype, role) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
159 |
if rcache is not None: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
160 |
rset, entities = rcache |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
161 |
rset.rows.append([targeteid]) |
2781
4e1ad9d6a3e7
[session cache] always append to description, turn it into a list if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2764
diff
changeset
|
162 |
if not isinstance(rset.description, list): # else description not set |
4e1ad9d6a3e7
[session cache] always append to description, turn it into a list if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2764
diff
changeset
|
163 |
rset.description = list(rset.description) |
4e1ad9d6a3e7
[session cache] always append to description, turn it into a list if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2764
diff
changeset
|
164 |
rset.description.append([self.describe(targeteid)[0]]) |
3196
77936fa67ae6
[repo] set entity.rset & co when needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3161
diff
changeset
|
165 |
targetentity = self.entity_from_eid(targeteid) |
3379
9192ba07890d
use .cw_rset instead of rset on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3373
diff
changeset
|
166 |
if targetentity.cw_rset is None: |
3373
3cd644bfab12
use 3.6 attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
167 |
targetentity.cw_rset = rset |
3cd644bfab12
use 3.6 attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
168 |
targetentity.cw_row = rset.rowcount |
3cd644bfab12
use 3.6 attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
169 |
targetentity.cw_col = 0 |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
170 |
rset.rowcount += 1 |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
171 |
entities.append(targetentity) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
172 |
|
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
173 |
def _update_entity_rel_cache_del(self, eid, rtype, role, targeteid): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
174 |
rcache = self._rel_cache(eid, rtype, role) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
175 |
if rcache is not None: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
176 |
rset, entities = rcache |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
177 |
for idx, row in enumerate(rset.rows): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
178 |
if row[0] == targeteid: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
179 |
break |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
180 |
else: |
3074
141cfaac6b97
don't fail when trying to update relation cache for relation deletion, this may occurs regularly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2874
diff
changeset
|
181 |
# this may occurs if the cache has been filed by a hook |
141cfaac6b97
don't fail when trying to update relation cache for relation deletion, this may occurs regularly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2874
diff
changeset
|
182 |
# after the database update |
141cfaac6b97
don't fail when trying to update relation cache for relation deletion, this may occurs regularly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2874
diff
changeset
|
183 |
self.debug('cache inconsistency for %s %s %s %s', eid, rtype, |
141cfaac6b97
don't fail when trying to update relation cache for relation deletion, this may occurs regularly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2874
diff
changeset
|
184 |
role, targeteid) |
141cfaac6b97
don't fail when trying to update relation cache for relation deletion, this may occurs regularly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2874
diff
changeset
|
185 |
return |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
186 |
del rset.rows[idx] |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
187 |
if isinstance(rset.description, list): # else description not set |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
188 |
del rset.description[idx] |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
189 |
del entities[idx] |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
190 |
rset.rowcount -= 1 |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
191 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
192 |
# resource accessors ###################################################### |
0 | 193 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
194 |
def actual_session(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
195 |
"""return the original parent session if any, else self""" |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
196 |
return self |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
197 |
|
2623
e1f34b77290b
oops, should have been in a earlier commit (give rollback_on_failure to doexec)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2616
diff
changeset
|
198 |
def system_sql(self, sql, args=None, rollback_on_failure=True): |
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
199 |
"""return a sql cursor on the system database""" |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
200 |
if not sql.split(None, 1)[0].upper() == 'SELECT': |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
201 |
self.mode = 'write' |
2623
e1f34b77290b
oops, should have been in a earlier commit (give rollback_on_failure to doexec)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2616
diff
changeset
|
202 |
return self.pool.source('system').doexec(self, sql, args, |
e1f34b77290b
oops, should have been in a earlier commit (give rollback_on_failure to doexec)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2616
diff
changeset
|
203 |
rollback=rollback_on_failure) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
204 |
|
0 | 205 |
def set_language(self, language): |
206 |
"""i18n configuration for translation""" |
|
207 |
vreg = self.vreg |
|
208 |
language = language or self.user.property_value('ui.language') |
|
209 |
try: |
|
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
210 |
gettext, pgettext = vreg.config.translations[language] |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
211 |
self._ = self.__ = gettext |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
212 |
self.pgettext = pgettext |
0 | 213 |
except KeyError: |
214 |
language = vreg.property_value('ui.language') |
|
215 |
try: |
|
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
216 |
gettext, pgettext = vreg.config.translations[language] |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
217 |
self._ = self.__ = gettext |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
218 |
self.pgettext = pgettext |
0 | 219 |
except KeyError: |
220 |
self._ = self.__ = unicode |
|
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
221 |
self.pgettext = lambda x,y: y |
0 | 222 |
self.lang = language |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
223 |
|
0 | 224 |
def change_property(self, prop, value): |
225 |
assert prop == 'lang' # this is the only one changeable property for now |
|
226 |
self.set_language(value) |
|
227 |
||
2840
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
228 |
def deleted_in_transaction(self, eid): |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
229 |
return eid in self.transaction_data.get('pendingeids', ()) |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
230 |
|
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
231 |
def added_in_transaction(self, eid): |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
232 |
return eid in self.transaction_data.get('neweids', ()) |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
233 |
|
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
234 |
def schema_rproperty(self, rtype, eidfrom, eidto, rprop): |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
235 |
rschema = self.repo.schema[rtype] |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
236 |
subjtype = self.describe(eidfrom)[0] |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
237 |
objtype = self.describe(eidto)[0] |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
238 |
return rschema.rproperty(subjtype, objtype, rprop) |
06daf13195d4
[hooks] deprecates hookhelper module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
239 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
240 |
# connection management ################################################### |
0 | 241 |
|
2570
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
242 |
def keep_pool_mode(self, mode): |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
243 |
"""set pool_mode, e.g. how the session will keep its pool: |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
244 |
|
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
245 |
* if mode == 'write', the pool is freed after each ready query, but kept |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
246 |
until the transaction's end (eg commit or rollback) when a write query |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
247 |
is detected (eg INSERT/SET/DELETE queries) |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
248 |
|
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
249 |
* if mode == 'transaction', the pool is only freed after the |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
250 |
transaction's end |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
251 |
|
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
252 |
notice that a repository has a limited set of pools, and a session has to |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
253 |
wait for a free pool to run any rql query (unless it already has a pool |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
254 |
set). |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
255 |
""" |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
256 |
assert mode in ('transaction', 'write') |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
257 |
if mode == 'transaction': |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
258 |
self.default_mode = 'transaction' |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
259 |
else: # mode == 'write' |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
260 |
self.default_mode = 'read' |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
261 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
262 |
def get_mode(self): |
2570
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
263 |
return getattr(self._threaddata, 'mode', self.default_mode) |
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
264 |
def set_mode(self, value): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
265 |
self._threaddata.mode = value |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
266 |
mode = property(get_mode, set_mode, |
2570
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
267 |
doc='transaction mode (read/write/transaction), resetted to' |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
268 |
' default_mode on commit / rollback') |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
269 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
270 |
def get_commit_state(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
271 |
return getattr(self._threaddata, 'commit_state', None) |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
272 |
def set_commit_state(self, value): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
273 |
self._threaddata.commit_state = value |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
274 |
commit_state = property(get_commit_state, set_commit_state) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
275 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
276 |
@property |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
277 |
def pool(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
278 |
"""connections pool, set according to transaction mode for each query""" |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
279 |
return getattr(self._threaddata, 'pool', None) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
280 |
|
2764
55579275a091
[session pool] dont check closed session when calling set_pool from super_session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
281 |
def set_pool(self, checkclosed=True): |
0 | 282 |
"""the session need a pool to execute some queries""" |
2764
55579275a091
[session pool] dont check closed session when calling set_pool from super_session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
283 |
if checkclosed and self._closed: |
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
284 |
raise Exception('try to set pool on a closed session') |
0 | 285 |
if self.pool is None: |
2054
277e8d3b1154
fix potential race-condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
286 |
# get pool first to avoid race-condition |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2200
diff
changeset
|
287 |
self._threaddata.pool = pool = self.repo._get_pool() |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
288 |
try: |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2200
diff
changeset
|
289 |
pool.pool_set() |
0 | 290 |
except: |
2054
277e8d3b1154
fix potential race-condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
291 |
self._threaddata.pool = None |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2200
diff
changeset
|
292 |
self.repo._free_pool(pool) |
0 | 293 |
raise |
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
294 |
self._threads_in_transaction.add(threading.currentThread()) |
0 | 295 |
return self._threaddata.pool |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
296 |
|
2570
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
297 |
def reset_pool(self, ignoremode=False): |
2319
654decb099e3
typo, no error if thread isn't in running set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2306
diff
changeset
|
298 |
"""the session is no longer using its pool, at least for some time""" |
0 | 299 |
# pool may be none if no operation has been done since last commit |
300 |
# or rollback |
|
2570
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
301 |
if self.pool is not None and (ignoremode or self.mode == 'read'): |
0 | 302 |
# even in read mode, we must release the current transaction |
2055 | 303 |
pool = self.pool |
2319
654decb099e3
typo, no error if thread isn't in running set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2306
diff
changeset
|
304 |
try: |
654decb099e3
typo, no error if thread isn't in running set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2306
diff
changeset
|
305 |
self._threads_in_transaction.remove(threading.currentThread()) |
654decb099e3
typo, no error if thread isn't in running set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2306
diff
changeset
|
306 |
except KeyError: |
654decb099e3
typo, no error if thread isn't in running set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2306
diff
changeset
|
307 |
pass |
2063
fe4278b50388
fix [re]set_pool prototype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2055
diff
changeset
|
308 |
pool.pool_reset() |
0 | 309 |
self._threaddata.pool = None |
2054
277e8d3b1154
fix potential race-condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
310 |
# free pool once everything is done to avoid race-condition |
2055 | 311 |
self.repo._free_pool(pool) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
312 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
313 |
def _touch(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
314 |
"""update latest session usage timestamp and reset mode to read""" |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
315 |
self.timestamp = time() |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
316 |
self.local_perm_cache.clear() |
2570
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
317 |
self._threaddata.mode = self.default_mode |
0 | 318 |
|
319 |
# shared data handling ################################################### |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
320 |
|
0 | 321 |
def get_shared_data(self, key, default=None, pop=False): |
322 |
"""return value associated to `key` in session data""" |
|
323 |
if pop: |
|
324 |
return self.data.pop(key, default) |
|
325 |
else: |
|
326 |
return self.data.get(key, default) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
327 |
|
0 | 328 |
def set_shared_data(self, key, value, querydata=False): |
329 |
"""set value associated to `key` in session data""" |
|
330 |
if querydata: |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
331 |
self.transaction_data[key] = value |
0 | 332 |
else: |
333 |
self.data[key] = value |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
334 |
|
0 | 335 |
# request interface ####################################################### |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
336 |
|
2855
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
337 |
@property |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
338 |
def cursor(self): |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
339 |
"""return a rql cursor""" |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
340 |
return self |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
341 |
|
0 | 342 |
def set_entity_cache(self, entity): |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
343 |
# XXX session level caching may be a pb with multiple repository |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
344 |
# instances, but 1. this is probably not the only one :$ and 2. it |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
345 |
# may be an acceptable risk. Anyway we could activate it or not |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
346 |
# according to a configuration option |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
347 |
try: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
348 |
self.transaction_data['ecache'].setdefault(entity.eid, entity) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
349 |
except KeyError: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
350 |
self.transaction_data['ecache'] = ecache = {} |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
351 |
ecache[entity.eid] = entity |
0 | 352 |
|
353 |
def entity_cache(self, eid): |
|
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
354 |
try: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
355 |
return self.transaction_data['ecache'][eid] |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
356 |
except: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
357 |
raise |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
358 |
|
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
359 |
def cached_entities(self): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
360 |
return self.transaction_data.get('ecache', {}).values() |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
361 |
|
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
362 |
def drop_entity_cache(self, eid=None): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
363 |
if eid is None: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
364 |
self.transaction_data.pop('ecache', None) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
365 |
else: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
366 |
del self.transaction_data['ecache'][eid] |
0 | 367 |
|
368 |
def base_url(self): |
|
2466
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
369 |
url = self.repo.config['base-url'] |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
370 |
if not url: |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
371 |
try: |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
372 |
url = self.repo.config.default_base_url() |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
373 |
except AttributeError: # default_base_url() might not be available |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
374 |
self.warning('missing base-url definition in server config') |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
375 |
url = u'' |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
376 |
return url |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
377 |
|
0 | 378 |
def from_controller(self): |
379 |
"""return the id (string) of the controller issuing the request (no |
|
380 |
sense here, always return 'view') |
|
381 |
""" |
|
382 |
return 'view' |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
383 |
|
0 | 384 |
def source_defs(self): |
385 |
return self.repo.source_defs() |
|
386 |
||
387 |
def describe(self, eid): |
|
388 |
"""return a tuple (type, sourceuri, extid) for the entity with id <eid>""" |
|
389 |
return self.repo.type_and_source_from_eid(eid, self) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
390 |
|
0 | 391 |
# db-api like interface ################################################### |
392 |
||
393 |
def source_from_eid(self, eid): |
|
394 |
"""return the source where the entity with id <eid> is located""" |
|
395 |
return self.repo.source_from_eid(eid, self) |
|
396 |
||
397 |
def decorate_rset(self, rset, propagate=False): |
|
398 |
rset.vreg = self.vreg |
|
399 |
rset.req = propagate and self or self.actual_session() |
|
400 |
return rset |
|
401 |
||
402 |
@property |
|
403 |
def super_session(self): |
|
404 |
try: |
|
405 |
csession = self._threaddata.childsession |
|
406 |
except AttributeError: |
|
2874
acdd8d8c2cff
[session] use isinstance instead of testing value of is_super_session since we hack it in the add_relation method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2781
diff
changeset
|
407 |
if isinstance(self, (ChildSession, InternalSession)): |
0 | 408 |
csession = self |
409 |
else: |
|
410 |
csession = ChildSession(self) |
|
411 |
self._threaddata.childsession = csession |
|
412 |
# need shared pool set |
|
2764
55579275a091
[session pool] dont check closed session when calling set_pool from super_session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
413 |
self.set_pool(checkclosed=False) |
0 | 414 |
return csession |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
415 |
|
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
416 |
def unsafe_execute(self, rql, kwargs=None, eid_key=None, build_descr=True, |
0 | 417 |
propagate=False): |
418 |
"""like .execute but with security checking disabled (this method is |
|
419 |
internal to the server, it's not part of the db-api) |
|
420 |
||
421 |
if `propagate` is true, the super_session will be attached to the result |
|
422 |
set instead of the parent session, hence further query done through |
|
423 |
entities fetched from this result set will bypass security as well |
|
424 |
""" |
|
425 |
return self.super_session.execute(rql, kwargs, eid_key, build_descr, |
|
426 |
propagate) |
|
427 |
||
428 |
def execute(self, rql, kwargs=None, eid_key=None, build_descr=True, |
|
429 |
propagate=False): |
|
430 |
"""db-api like method directly linked to the querier execute method |
|
431 |
||
432 |
Becare that unlike actual cursor.execute, `build_descr` default to |
|
433 |
false |
|
434 |
""" |
|
435 |
rset = self._execute(self, rql, kwargs, eid_key, build_descr) |
|
436 |
return self.decorate_rset(rset, propagate) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
437 |
|
0 | 438 |
def commit(self, reset_pool=True): |
439 |
"""commit the current session's transaction""" |
|
440 |
if self.pool is None: |
|
441 |
assert not self.pending_operations |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
442 |
self.transaction_data.clear() |
0 | 443 |
self._touch() |
2200
25bb65dc4559
test fixes, all server tests ok, except unittest_migractions (due to inter-tests-side-effects...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2190
diff
changeset
|
444 |
self.debug('commit session %s done (no db activity)', self.id) |
0 | 445 |
return |
446 |
if self.commit_state: |
|
447 |
return |
|
448 |
# on rollback, an operation should have the following state |
|
449 |
# information: |
|
450 |
# - processed by the precommit/commit event or not |
|
451 |
# - if processed, is it the failed operation |
|
452 |
try: |
|
453 |
for trstate in ('precommit', 'commit'): |
|
454 |
processed = [] |
|
455 |
self.commit_state = trstate |
|
456 |
try: |
|
457 |
while self.pending_operations: |
|
458 |
operation = self.pending_operations.pop(0) |
|
459 |
operation.processed = trstate |
|
460 |
processed.append(operation) |
|
461 |
operation.handle_event('%s_event' % trstate) |
|
462 |
self.pending_operations[:] = processed |
|
463 |
self.debug('%s session %s done', trstate, self.id) |
|
464 |
except: |
|
465 |
self.exception('error while %sing', trstate) |
|
466 |
operation.failed = True |
|
467 |
for operation in processed: |
|
468 |
operation.handle_event('revert%s_event' % trstate) |
|
469 |
self.rollback(reset_pool) |
|
470 |
raise |
|
471 |
self.pool.commit() |
|
472 |
finally: |
|
473 |
self._touch() |
|
474 |
self.commit_state = None |
|
475 |
self.pending_operations[:] = [] |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
476 |
self.transaction_data.clear() |
0 | 477 |
if reset_pool: |
2570
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
478 |
self.reset_pool(ignoremode=True) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
479 |
|
0 | 480 |
def rollback(self, reset_pool=True): |
481 |
"""rollback the current session's transaction""" |
|
482 |
if self.pool is None: |
|
483 |
assert not self.pending_operations |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
484 |
self.transaction_data.clear() |
0 | 485 |
self._touch() |
2200
25bb65dc4559
test fixes, all server tests ok, except unittest_migractions (due to inter-tests-side-effects...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2190
diff
changeset
|
486 |
self.debug('rollback session %s done (no db activity)', self.id) |
0 | 487 |
return |
488 |
try: |
|
489 |
while self.pending_operations: |
|
490 |
try: |
|
491 |
operation = self.pending_operations.pop(0) |
|
492 |
operation.handle_event('rollback_event') |
|
493 |
except: |
|
494 |
self.critical('rollback error', exc_info=sys.exc_info()) |
|
495 |
continue |
|
496 |
self.pool.rollback() |
|
2188
3a57c8173290
* deprecate session.entity method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
497 |
self.debug('rollback for session %s done', self.id) |
0 | 498 |
finally: |
499 |
self._touch() |
|
500 |
self.pending_operations[:] = [] |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
501 |
self.transaction_data.clear() |
0 | 502 |
if reset_pool: |
2570
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
503 |
self.reset_pool(ignoremode=True) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
504 |
|
0 | 505 |
def close(self): |
506 |
"""do not close pool on session close, since they are shared now""" |
|
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
507 |
self._closed = True |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
508 |
# copy since _threads_in_transaction maybe modified while waiting |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
509 |
for thread in self._threads_in_transaction.copy(): |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
510 |
if thread is threading.currentThread(): |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
511 |
continue |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
512 |
self.info('waiting for thread %s', thread) |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
513 |
# do this loop/break instead of a simple join(10) in case thread is |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
514 |
# the main thread (in which case it will be removed from |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
515 |
# self._threads_in_transaction but still be alive...) |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
516 |
for i in xrange(10): |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
517 |
thread.join(1) |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
518 |
if not (thread.isAlive() and |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
519 |
thread in self._threads_in_transaction): |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
520 |
break |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
521 |
else: |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
522 |
self.error('thread %s still alive after 10 seconds, will close ' |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
523 |
'session anyway', thread) |
0 | 524 |
self.rollback() |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
525 |
|
0 | 526 |
# transaction data/operations management ################################## |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
527 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
528 |
@property |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
529 |
def transaction_data(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
530 |
try: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
531 |
return self._threaddata.transaction_data |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
532 |
except AttributeError: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
533 |
self._threaddata.transaction_data = {} |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
534 |
return self._threaddata.transaction_data |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
535 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
536 |
@property |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
537 |
def pending_operations(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
538 |
try: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
539 |
return self._threaddata.pending_operations |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
540 |
except AttributeError: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
541 |
self._threaddata.pending_operations = [] |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
542 |
return self._threaddata.pending_operations |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
543 |
|
0 | 544 |
def add_operation(self, operation, index=None): |
545 |
"""add an observer""" |
|
546 |
assert self.commit_state != 'commit' |
|
547 |
if index is not None: |
|
548 |
self.pending_operations.insert(index, operation) |
|
549 |
else: |
|
550 |
self.pending_operations.append(operation) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
551 |
|
0 | 552 |
# querier helpers ######################################################### |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
553 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
554 |
@property |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
555 |
def rql_rewriter(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
556 |
try: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
557 |
return self._threaddata._rewriter |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
558 |
except AttributeError: |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3198
diff
changeset
|
559 |
self._threaddata._rewriter = RQLRewriter(self) |
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
560 |
return self._threaddata._rewriter |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
561 |
|
0 | 562 |
def build_description(self, rqlst, args, result): |
563 |
"""build a description for a given result""" |
|
564 |
if len(rqlst.children) == 1 and len(rqlst.children[0].solutions) == 1: |
|
565 |
# easy, all lines are identical |
|
566 |
selected = rqlst.children[0].selection |
|
567 |
solution = rqlst.children[0].solutions[0] |
|
568 |
description = _make_description(selected, args, solution) |
|
569 |
return [tuple(description)] * len(result) |
|
570 |
# hard, delegate the work :o) |
|
571 |
return self.manual_build_descr(rqlst, args, result) |
|
572 |
||
573 |
def manual_build_descr(self, rqlst, args, result): |
|
574 |
"""build a description for a given result by analysing each row |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
575 |
|
0 | 576 |
XXX could probably be done more efficiently during execution of query |
577 |
""" |
|
578 |
# not so easy, looks for variable which changes from one solution |
|
579 |
# to another |
|
580 |
unstables = rqlst.get_variable_variables() |
|
581 |
basedescription = [] |
|
582 |
todetermine = [] |
|
583 |
selected = rqlst.children[0].selection # sample selection |
|
584 |
for i, term in enumerate(selected): |
|
585 |
if isinstance(term, Function) and term.descr().rtype is not None: |
|
586 |
basedescription.append(term.get_type(term.descr().rtype, args)) |
|
587 |
continue |
|
588 |
for vref in term.get_nodes(VariableRef): |
|
589 |
if vref.name in unstables: |
|
590 |
basedescription.append(None) |
|
591 |
todetermine.append( (i, is_final(rqlst, vref.variable, args)) ) |
|
592 |
break |
|
593 |
else: |
|
594 |
# sample etype |
|
595 |
etype = rqlst.children[0].solutions[0] |
|
596 |
basedescription.append(term.get_type(etype, args)) |
|
597 |
if not todetermine: |
|
598 |
return [tuple(basedescription)] * len(result) |
|
599 |
return self._build_descr(result, basedescription, todetermine) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
600 |
|
0 | 601 |
def _build_descr(self, result, basedescription, todetermine): |
602 |
description = [] |
|
603 |
etype_from_eid = self.describe |
|
604 |
for row in result: |
|
605 |
row_descr = basedescription |
|
606 |
for index, isfinal in todetermine: |
|
607 |
value = row[index] |
|
608 |
if value is None: |
|
609 |
# None value inserted by an outer join, no type |
|
610 |
row_descr[index] = None |
|
611 |
continue |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
612 |
if isfinal: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
613 |
row_descr[index] = etype_from_pyobj(value) |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
614 |
else: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
615 |
try: |
1169
52058e8a3af9
somewhat handle corrupted database when manually building a database
sylvain.thenault@logilab.fr
parents:
974
diff
changeset
|
616 |
row_descr[index] = etype_from_eid(value)[0] |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
617 |
except UnknownEid: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
618 |
self.critical('wrong eid %s in repository, should check database' % value) |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
619 |
row_descr[index] = row[index] = None |
0 | 620 |
description.append(tuple(row_descr)) |
621 |
return description |
|
622 |
||
2855
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
623 |
# deprecated ############################################################### |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
624 |
|
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
625 |
@property |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
626 |
@deprecated("[3.6] use session.vreg.schema") |
2855
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
627 |
def schema(self): |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
628 |
return self.repo.schema |
1d9be3dffa94
[repo] more deprecation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2840
diff
changeset
|
629 |
|
2788
8d3dbe577d3a
R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2786
diff
changeset
|
630 |
@deprecated("[3.4] use vreg['etypes'].etype_class(etype)") |
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:
2647
diff
changeset
|
631 |
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:
2647
diff
changeset
|
632 |
"""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:
2647
diff
changeset
|
633 |
return self.vreg['etypes'].etype_class(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:
2647
diff
changeset
|
634 |
|
2788
8d3dbe577d3a
R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2786
diff
changeset
|
635 |
@deprecated('[3.4] use direct access to session.transaction_data') |
2182
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
636 |
def query_data(self, key, default=None, setdefault=False, pop=False): |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
637 |
if setdefault: |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
638 |
assert not pop |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
639 |
return self.transaction_data.setdefault(key, default) |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
640 |
if pop: |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
641 |
return self.transaction_data.pop(key, default) |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
642 |
else: |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
643 |
return self.transaction_data.get(key, default) |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
644 |
|
2788
8d3dbe577d3a
R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2786
diff
changeset
|
645 |
@deprecated('[3.4] use entity_from_eid(eid, etype=None)') |
2182
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
646 |
def entity(self, eid): |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
647 |
"""return a result set for the given eid""" |
2680
66472d85d548
[R] use req.entity_from_eid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
648 |
return self.entity_from_eid(eid) |
2182
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
649 |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
650 |
|
0 | 651 |
class ChildSession(Session): |
652 |
"""child (or internal) session are used to hijack the security system |
|
653 |
""" |
|
654 |
cnxtype = 'inmemory' |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
655 |
|
0 | 656 |
def __init__(self, parent_session): |
657 |
self.id = None |
|
658 |
self.is_internal_session = False |
|
659 |
self.is_super_session = True |
|
660 |
# session which has created this one |
|
661 |
self.parent_session = parent_session |
|
662 |
self.user = InternalManager() |
|
2891
60afb9705035
[session] temporary fix, need .req on internal manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2880
diff
changeset
|
663 |
self.user.req = self # XXX remove when "vreg = user.req.vreg" hack in entity.py is gone |
0 | 664 |
self.repo = parent_session.repo |
665 |
self.vreg = parent_session.vreg |
|
666 |
self.data = parent_session.data |
|
667 |
self.encoding = parent_session.encoding |
|
668 |
self.lang = parent_session.lang |
|
669 |
self._ = self.__ = parent_session._ |
|
670 |
# short cut to querier .execute method |
|
671 |
self._execute = self.repo.querier.execute |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
672 |
|
0 | 673 |
@property |
674 |
def super_session(self): |
|
675 |
return self |
|
676 |
||
677 |
def get_mode(self): |
|
678 |
return self.parent_session.mode |
|
679 |
def set_mode(self, value): |
|
680 |
self.parent_session.set_mode(value) |
|
681 |
mode = property(get_mode, set_mode) |
|
682 |
||
683 |
def get_commit_state(self): |
|
684 |
return self.parent_session.commit_state |
|
685 |
def set_commit_state(self, value): |
|
686 |
self.parent_session.set_commit_state(value) |
|
687 |
commit_state = property(get_commit_state, set_commit_state) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
688 |
|
0 | 689 |
@property |
690 |
def pool(self): |
|
691 |
return self.parent_session.pool |
|
692 |
@property |
|
693 |
def pending_operations(self): |
|
694 |
return self.parent_session.pending_operations |
|
695 |
@property |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
696 |
def transaction_data(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
697 |
return self.parent_session.transaction_data |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
698 |
|
0 | 699 |
def set_pool(self): |
700 |
"""the session need a pool to execute some queries""" |
|
701 |
self.parent_session.set_pool() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
702 |
|
0 | 703 |
def reset_pool(self): |
704 |
"""the session has no longer using its pool, at least for some time |
|
705 |
""" |
|
706 |
self.parent_session.reset_pool() |
|
707 |
||
708 |
def actual_session(self): |
|
709 |
"""return the original parent session if any, else self""" |
|
710 |
return self.parent_session |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
711 |
|
0 | 712 |
def commit(self, reset_pool=True): |
713 |
"""commit the current session's transaction""" |
|
714 |
self.parent_session.commit(reset_pool) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
715 |
|
0 | 716 |
def rollback(self, reset_pool=True): |
717 |
"""rollback the current session's transaction""" |
|
718 |
self.parent_session.rollback(reset_pool) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
719 |
|
0 | 720 |
def close(self): |
721 |
"""do not close pool on session close, since they are shared now""" |
|
722 |
self.rollback() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
723 |
|
0 | 724 |
def user_data(self): |
725 |
"""returns a dictionnary with this user's information""" |
|
726 |
return self.parent_session.user_data() |
|
727 |
||
728 |
||
729 |
class InternalSession(Session): |
|
730 |
"""special session created internaly by the repository""" |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
731 |
|
0 | 732 |
def __init__(self, repo, cnxprops=None): |
2891
60afb9705035
[session] temporary fix, need .req on internal manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2880
diff
changeset
|
733 |
super(InternalSession, self).__init__(InternalManager(), repo, cnxprops, |
0 | 734 |
_id='internal') |
2891
60afb9705035
[session] temporary fix, need .req on internal manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2880
diff
changeset
|
735 |
self.user.req = self # XXX remove when "vreg = user.req.vreg" hack in entity.py is gone |
0 | 736 |
self.cnxtype = 'inmemory' |
737 |
self.is_internal_session = True |
|
738 |
self.is_super_session = True |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
739 |
|
0 | 740 |
@property |
741 |
def super_session(self): |
|
742 |
return self |
|
743 |
||
744 |
||
745 |
class InternalManager(object): |
|
746 |
"""a manager user with all access rights used internally for task such as |
|
747 |
bootstrapping the repository or creating regular users according to |
|
748 |
repository content |
|
749 |
""" |
|
750 |
def __init__(self): |
|
751 |
self.eid = -1 |
|
752 |
self.login = u'__internal_manager__' |
|
753 |
self.properties = {} |
|
754 |
||
755 |
def matching_groups(self, groups): |
|
756 |
return 1 |
|
757 |
||
758 |
def is_in_group(self, group): |
|
759 |
return True |
|
760 |
||
761 |
def owns(self, eid): |
|
762 |
return True |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
763 |
|
0 | 764 |
def has_permission(self, pname, contexteid=None): |
765 |
return True |
|
766 |
||
767 |
def property_value(self, key): |
|
768 |
if key == 'ui.language': |
|
769 |
return 'en' |
|
770 |
return None |
|
771 |
||
772 |
||
773 |
from logging import getLogger |
|
774 |
from cubicweb import set_log_methods |
|
775 |
set_log_methods(Session, getLogger('cubicweb.session')) |