author | Julien Jehannet <julien.jehannet@logilab.fr> |
Wed, 30 Sep 2009 18:53:18 +0200 | |
branch | stable |
changeset 3537 | 73b5bec579ba |
parent 3275 | 5247789df541 |
child 3293 | 69c0ba095536 |
child 3553 | 76b4a177a018 |
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 |
||
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
18 |
from cubicweb import RequestSessionMixIn, Binary, UnknownEid |
0 | 19 |
from cubicweb.dbapi import ConnectionProperties |
940
15dcdc863965
fix imports : common.utils -> utils
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
746
diff
changeset
|
20 |
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
|
21 |
from cubicweb.rqlrewrite import RQLRewriter |
0 | 22 |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
23 |
ETYPE_PYOBJ_MAP[Binary] = 'Bytes' |
0 | 24 |
|
25 |
def is_final(rqlst, variable, args): |
|
26 |
# try to find if this is a final var or not |
|
27 |
for select in rqlst.children: |
|
28 |
for sol in select.solutions: |
|
29 |
etype = variable.get_type(sol, args) |
|
30 |
if etype is None: |
|
31 |
continue |
|
32 |
if etype in BASE_TYPES: |
|
33 |
return True |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
34 |
return False |
0 | 35 |
|
36 |
def _make_description(selected, args, solution): |
|
37 |
"""return a description for a result set""" |
|
38 |
description = [] |
|
39 |
for term in selected: |
|
40 |
description.append(term.get_type(solution, args)) |
|
41 |
return description |
|
42 |
||
43 |
||
44 |
class Session(RequestSessionMixIn): |
|
45 |
"""tie session id, user, connections pool and other session data all |
|
46 |
together |
|
47 |
""" |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
48 |
|
0 | 49 |
def __init__(self, user, repo, cnxprops=None, _id=None): |
50 |
super(Session, self).__init__(repo.vreg) |
|
51 |
self.id = _id or make_uid(user.login.encode('UTF8')) |
|
52 |
cnxprops = cnxprops or ConnectionProperties('inmemory') |
|
53 |
self.user = user |
|
54 |
self.repo = repo |
|
55 |
self.cnxtype = cnxprops.cnxtype |
|
56 |
self.creation = time() |
|
57 |
self.timestamp = self.creation |
|
58 |
self.is_internal_session = False |
|
59 |
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
|
60 |
self.default_mode = 'read' |
0 | 61 |
# short cut to querier .execute method |
62 |
self._execute = repo.querier.execute |
|
63 |
# shared data, used to communicate extra information between the client |
|
64 |
# and the rql server |
|
65 |
self.data = {} |
|
66 |
# i18n initialization |
|
67 |
self.set_language(cnxprops.lang) |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
68 |
# internals |
0 | 69 |
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
|
70 |
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
|
71 |
self._closed = False |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
72 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
73 |
def __str__(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
74 |
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
|
75 |
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
|
76 |
|
2630
b3f997bc8c26
R [session] property for quick access to repo.schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2623
diff
changeset
|
77 |
@property |
b3f997bc8c26
R [session] property for quick access to repo.schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2623
diff
changeset
|
78 |
def schema(self): |
b3f997bc8c26
R [session] property for quick access to repo.schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2623
diff
changeset
|
79 |
return self.repo.schema |
b3f997bc8c26
R [session] property for quick access to repo.schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2623
diff
changeset
|
80 |
|
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
|
81 |
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
|
82 |
"""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
|
83 |
session = Session(user, self.repo) |
3161
005f745315b0
[notification] should get actual session here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3112
diff
changeset
|
84 |
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
|
85 |
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
|
86 |
|
3197
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
87 |
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
|
88 |
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
|
89 |
__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
|
90 |
return |
6b55a2a81fd8
[R repo session] add_relation method use in hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
91 |
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
|
92 |
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
|
93 |
__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
|
94 |
finally: |
6b55a2a81fd8
[R repo session] add_relation method use in hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
95 |
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
|
96 |
|
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
|
97 |
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
|
98 |
"""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
|
99 |
|
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 |
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
|
101 |
|
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 |
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
|
103 |
|
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 |
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
|
105 |
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
|
106 |
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
|
107 |
""" |
3197
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
108 |
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
|
109 |
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
|
110 |
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
|
111 |
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
|
112 |
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
|
113 |
else: |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
114 |
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
|
115 |
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
|
116 |
|
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
|
117 |
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
|
118 |
"""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
|
119 |
|
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 |
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
|
121 |
|
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 |
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
|
123 |
|
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 |
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
|
125 |
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
|
126 |
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
|
127 |
""" |
3197
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
128 |
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
|
129 |
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
|
130 |
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
|
131 |
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
|
132 |
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
|
133 |
else: |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
134 |
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
|
135 |
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
|
136 |
|
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
|
137 |
# 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
|
138 |
|
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
|
139 |
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
|
140 |
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
|
141 |
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
|
142 |
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
|
143 |
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
|
144 |
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
|
145 |
|
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 |
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
|
147 |
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
|
148 |
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
|
149 |
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
|
150 |
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
|
151 |
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
|
152 |
|
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 |
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
|
154 |
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
|
155 |
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
|
156 |
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
|
157 |
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
|
158 |
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
|
159 |
|
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 |
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
|
161 |
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
|
162 |
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
|
163 |
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
|
164 |
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
|
165 |
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
|
166 |
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
|
167 |
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
|
168 |
targetentity = self.entity_from_eid(targeteid) |
77936fa67ae6
[repo] set entity.rset & co when needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3161
diff
changeset
|
169 |
if targetentity.rset is None: |
77936fa67ae6
[repo] set entity.rset & co when needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3161
diff
changeset
|
170 |
targetentity.rset = rset |
77936fa67ae6
[repo] set entity.rset & co when needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3161
diff
changeset
|
171 |
targetentity.row = rset.rowcount |
77936fa67ae6
[repo] set entity.rset & co when needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3161
diff
changeset
|
172 |
targetentity.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
|
173 |
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
|
174 |
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
|
175 |
|
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 |
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
|
177 |
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
|
178 |
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
|
179 |
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
|
180 |
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
|
181 |
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
|
182 |
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
|
183 |
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
|
184 |
# 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
|
185 |
# 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
|
186 |
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
|
187 |
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
|
188 |
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
|
189 |
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
|
190 |
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
|
191 |
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
|
192 |
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
|
193 |
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
|
194 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
195 |
# resource accessors ###################################################### |
0 | 196 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
197 |
def actual_session(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
198 |
"""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
|
199 |
return self |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
200 |
|
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
|
201 |
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
|
202 |
"""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
|
203 |
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
|
204 |
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
|
205 |
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
|
206 |
rollback=rollback_on_failure) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
207 |
|
0 | 208 |
def set_language(self, language): |
209 |
"""i18n configuration for translation""" |
|
210 |
vreg = self.vreg |
|
211 |
language = language or self.user.property_value('ui.language') |
|
212 |
try: |
|
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
213 |
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
|
214 |
self._ = self.__ = gettext |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
215 |
self.pgettext = pgettext |
0 | 216 |
except KeyError: |
217 |
language = vreg.property_value('ui.language') |
|
218 |
try: |
|
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
219 |
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
|
220 |
self._ = self.__ = gettext |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
221 |
self.pgettext = pgettext |
0 | 222 |
except KeyError: |
223 |
self._ = self.__ = unicode |
|
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
224 |
self.pgettext = lambda x,y: y |
0 | 225 |
self.lang = language |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
226 |
|
0 | 227 |
def change_property(self, prop, value): |
228 |
assert prop == 'lang' # this is the only one changeable property for now |
|
229 |
self.set_language(value) |
|
230 |
||
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
231 |
# connection management ################################################### |
0 | 232 |
|
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
|
233 |
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
|
234 |
"""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
|
235 |
|
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
|
236 |
* 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
|
237 |
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
|
238 |
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
|
239 |
|
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
|
240 |
* 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
|
241 |
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
|
242 |
|
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 |
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
|
244 |
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
|
245 |
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
|
246 |
""" |
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 |
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
|
248 |
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
|
249 |
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
|
250 |
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
|
251 |
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
|
252 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
253 |
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
|
254 |
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
|
255 |
def set_mode(self, value): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
256 |
self._threaddata.mode = value |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
257 |
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
|
258 |
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
|
259 |
' default_mode on commit / rollback') |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
260 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
261 |
def get_commit_state(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
262 |
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
|
263 |
def set_commit_state(self, value): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
264 |
self._threaddata.commit_state = value |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
265 |
commit_state = property(get_commit_state, set_commit_state) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
266 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
267 |
@property |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
268 |
def pool(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
269 |
"""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
|
270 |
return getattr(self._threaddata, 'pool', None) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
271 |
|
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
|
272 |
def set_pool(self, checkclosed=True): |
0 | 273 |
"""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
|
274 |
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
|
275 |
raise Exception('try to set pool on a closed session') |
0 | 276 |
if self.pool is None: |
2054
277e8d3b1154
fix potential race-condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
277 |
# 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
|
278 |
self._threaddata.pool = pool = self.repo._get_pool() |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
279 |
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
|
280 |
pool.pool_set() |
0 | 281 |
except: |
2054
277e8d3b1154
fix potential race-condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
282 |
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
|
283 |
self.repo._free_pool(pool) |
0 | 284 |
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
|
285 |
self._threads_in_transaction.add(threading.currentThread()) |
0 | 286 |
return self._threaddata.pool |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
287 |
|
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
|
288 |
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
|
289 |
"""the session is no longer using its pool, at least for some time""" |
0 | 290 |
# pool may be none if no operation has been done since last commit |
291 |
# 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
|
292 |
if self.pool is not None and (ignoremode or self.mode == 'read'): |
0 | 293 |
# even in read mode, we must release the current transaction |
2055 | 294 |
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
|
295 |
try: |
654decb099e3
typo, no error if thread isn't in running set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2306
diff
changeset
|
296 |
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
|
297 |
except KeyError: |
654decb099e3
typo, no error if thread isn't in running set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2306
diff
changeset
|
298 |
pass |
2063
fe4278b50388
fix [re]set_pool prototype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2055
diff
changeset
|
299 |
pool.pool_reset() |
0 | 300 |
self._threaddata.pool = None |
2054
277e8d3b1154
fix potential race-condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
301 |
# free pool once everything is done to avoid race-condition |
2055 | 302 |
self.repo._free_pool(pool) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
303 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
304 |
def _touch(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
305 |
"""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
|
306 |
self.timestamp = time() |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
307 |
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
|
308 |
self._threaddata.mode = self.default_mode |
0 | 309 |
|
310 |
# shared data handling ################################################### |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
311 |
|
0 | 312 |
def get_shared_data(self, key, default=None, pop=False): |
313 |
"""return value associated to `key` in session data""" |
|
314 |
if pop: |
|
315 |
return self.data.pop(key, default) |
|
316 |
else: |
|
317 |
return self.data.get(key, default) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
318 |
|
0 | 319 |
def set_shared_data(self, key, value, querydata=False): |
320 |
"""set value associated to `key` in session data""" |
|
321 |
if querydata: |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
322 |
self.transaction_data[key] = value |
0 | 323 |
else: |
324 |
self.data[key] = value |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
325 |
|
0 | 326 |
# request interface ####################################################### |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
327 |
|
0 | 328 |
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
|
329 |
# 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
|
330 |
# 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
|
331 |
# 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
|
332 |
# 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
|
333 |
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
|
334 |
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
|
335 |
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
|
336 |
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
|
337 |
ecache[entity.eid] = entity |
0 | 338 |
|
339 |
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
|
340 |
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
|
341 |
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
|
342 |
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
|
343 |
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
|
344 |
|
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 |
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
|
346 |
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
|
347 |
|
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 |
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
|
349 |
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
|
350 |
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
|
351 |
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
|
352 |
del self.transaction_data['ecache'][eid] |
0 | 353 |
|
354 |
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
|
355 |
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
|
356 |
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
|
357 |
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
|
358 |
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
|
359 |
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
|
360 |
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
|
361 |
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
|
362 |
return url |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
363 |
|
0 | 364 |
def from_controller(self): |
365 |
"""return the id (string) of the controller issuing the request (no |
|
366 |
sense here, always return 'view') |
|
367 |
""" |
|
368 |
return 'view' |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
369 |
|
0 | 370 |
def source_defs(self): |
371 |
return self.repo.source_defs() |
|
372 |
||
373 |
def describe(self, eid): |
|
374 |
"""return a tuple (type, sourceuri, extid) for the entity with id <eid>""" |
|
375 |
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
|
376 |
|
0 | 377 |
# db-api like interface ################################################### |
378 |
||
379 |
def source_from_eid(self, eid): |
|
380 |
"""return the source where the entity with id <eid> is located""" |
|
381 |
return self.repo.source_from_eid(eid, self) |
|
382 |
||
383 |
def decorate_rset(self, rset, propagate=False): |
|
384 |
rset.vreg = self.vreg |
|
385 |
rset.req = propagate and self or self.actual_session() |
|
386 |
return rset |
|
387 |
||
388 |
@property |
|
389 |
def super_session(self): |
|
390 |
try: |
|
391 |
csession = self._threaddata.childsession |
|
392 |
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
|
393 |
if isinstance(self, (ChildSession, InternalSession)): |
0 | 394 |
csession = self |
395 |
else: |
|
396 |
csession = ChildSession(self) |
|
397 |
self._threaddata.childsession = csession |
|
398 |
# 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
|
399 |
self.set_pool(checkclosed=False) |
0 | 400 |
return csession |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
401 |
|
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
|
402 |
def unsafe_execute(self, rql, kwargs=None, eid_key=None, build_descr=True, |
0 | 403 |
propagate=False): |
404 |
"""like .execute but with security checking disabled (this method is |
|
405 |
internal to the server, it's not part of the db-api) |
|
406 |
||
407 |
if `propagate` is true, the super_session will be attached to the result |
|
408 |
set instead of the parent session, hence further query done through |
|
409 |
entities fetched from this result set will bypass security as well |
|
410 |
""" |
|
411 |
return self.super_session.execute(rql, kwargs, eid_key, build_descr, |
|
412 |
propagate) |
|
413 |
||
414 |
@property |
|
415 |
def cursor(self): |
|
416 |
"""return a rql cursor""" |
|
417 |
return self |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
418 |
|
0 | 419 |
def execute(self, rql, kwargs=None, eid_key=None, build_descr=True, |
420 |
propagate=False): |
|
421 |
"""db-api like method directly linked to the querier execute method |
|
422 |
||
423 |
Becare that unlike actual cursor.execute, `build_descr` default to |
|
424 |
false |
|
425 |
""" |
|
426 |
rset = self._execute(self, rql, kwargs, eid_key, build_descr) |
|
427 |
return self.decorate_rset(rset, propagate) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
428 |
|
0 | 429 |
def commit(self, reset_pool=True): |
430 |
"""commit the current session's transaction""" |
|
431 |
if self.pool is None: |
|
432 |
assert not self.pending_operations |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
433 |
self.transaction_data.clear() |
0 | 434 |
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
|
435 |
self.debug('commit session %s done (no db activity)', self.id) |
0 | 436 |
return |
437 |
if self.commit_state: |
|
438 |
return |
|
439 |
# on rollback, an operation should have the following state |
|
440 |
# information: |
|
441 |
# - processed by the precommit/commit event or not |
|
442 |
# - if processed, is it the failed operation |
|
443 |
try: |
|
444 |
for trstate in ('precommit', 'commit'): |
|
445 |
processed = [] |
|
446 |
self.commit_state = trstate |
|
447 |
try: |
|
448 |
while self.pending_operations: |
|
449 |
operation = self.pending_operations.pop(0) |
|
450 |
operation.processed = trstate |
|
451 |
processed.append(operation) |
|
452 |
operation.handle_event('%s_event' % trstate) |
|
453 |
self.pending_operations[:] = processed |
|
454 |
self.debug('%s session %s done', trstate, self.id) |
|
455 |
except: |
|
456 |
self.exception('error while %sing', trstate) |
|
457 |
operation.failed = True |
|
458 |
for operation in processed: |
|
459 |
operation.handle_event('revert%s_event' % trstate) |
|
460 |
self.rollback(reset_pool) |
|
461 |
raise |
|
462 |
self.pool.commit() |
|
463 |
finally: |
|
464 |
self._touch() |
|
465 |
self.commit_state = None |
|
466 |
self.pending_operations[:] = [] |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
467 |
self.transaction_data.clear() |
0 | 468 |
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
|
469 |
self.reset_pool(ignoremode=True) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
470 |
|
0 | 471 |
def rollback(self, reset_pool=True): |
472 |
"""rollback the current session's transaction""" |
|
473 |
if self.pool is None: |
|
474 |
assert not self.pending_operations |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
475 |
self.transaction_data.clear() |
0 | 476 |
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
|
477 |
self.debug('rollback session %s done (no db activity)', self.id) |
0 | 478 |
return |
479 |
try: |
|
480 |
while self.pending_operations: |
|
481 |
try: |
|
482 |
operation = self.pending_operations.pop(0) |
|
483 |
operation.handle_event('rollback_event') |
|
484 |
except: |
|
485 |
self.critical('rollback error', exc_info=sys.exc_info()) |
|
486 |
continue |
|
487 |
self.pool.rollback() |
|
2188
3a57c8173290
* deprecate session.entity method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
488 |
self.debug('rollback for session %s done', self.id) |
0 | 489 |
finally: |
490 |
self._touch() |
|
491 |
self.pending_operations[:] = [] |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
492 |
self.transaction_data.clear() |
0 | 493 |
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
|
494 |
self.reset_pool(ignoremode=True) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
495 |
|
0 | 496 |
def close(self): |
497 |
"""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
|
498 |
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
|
499 |
# 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
|
500 |
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
|
501 |
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
|
502 |
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
|
503 |
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
|
504 |
# 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
|
505 |
# 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
|
506 |
# 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
|
507 |
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
|
508 |
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
|
509 |
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
|
510 |
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
|
511 |
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
|
512 |
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
|
513 |
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
|
514 |
'session anyway', thread) |
0 | 515 |
self.rollback() |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
516 |
|
0 | 517 |
# transaction data/operations management ################################## |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
518 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
519 |
@property |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
520 |
def transaction_data(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
521 |
try: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
522 |
return self._threaddata.transaction_data |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
523 |
except AttributeError: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
524 |
self._threaddata.transaction_data = {} |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
525 |
return self._threaddata.transaction_data |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
526 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
527 |
@property |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
528 |
def pending_operations(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
529 |
try: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
530 |
return self._threaddata.pending_operations |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
531 |
except AttributeError: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
532 |
self._threaddata.pending_operations = [] |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
533 |
return self._threaddata.pending_operations |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
534 |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
535 |
|
0 | 536 |
def add_operation(self, operation, index=None): |
537 |
"""add an observer""" |
|
538 |
assert self.commit_state != 'commit' |
|
539 |
if index is not None: |
|
540 |
self.pending_operations.insert(index, operation) |
|
541 |
else: |
|
542 |
self.pending_operations.append(operation) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
543 |
|
0 | 544 |
# querier helpers ######################################################### |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
545 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
546 |
@property |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
547 |
def rql_rewriter(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
548 |
try: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
549 |
return self._threaddata._rewriter |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
550 |
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
|
551 |
self._threaddata._rewriter = RQLRewriter(self) |
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
552 |
return self._threaddata._rewriter |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
553 |
|
0 | 554 |
def build_description(self, rqlst, args, result): |
555 |
"""build a description for a given result""" |
|
556 |
if len(rqlst.children) == 1 and len(rqlst.children[0].solutions) == 1: |
|
557 |
# easy, all lines are identical |
|
558 |
selected = rqlst.children[0].selection |
|
559 |
solution = rqlst.children[0].solutions[0] |
|
560 |
description = _make_description(selected, args, solution) |
|
561 |
return [tuple(description)] * len(result) |
|
562 |
# hard, delegate the work :o) |
|
563 |
return self.manual_build_descr(rqlst, args, result) |
|
564 |
||
565 |
def manual_build_descr(self, rqlst, args, result): |
|
566 |
"""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
|
567 |
|
0 | 568 |
XXX could probably be done more efficiently during execution of query |
569 |
""" |
|
570 |
# not so easy, looks for variable which changes from one solution |
|
571 |
# to another |
|
572 |
unstables = rqlst.get_variable_variables() |
|
573 |
basedescription = [] |
|
574 |
todetermine = [] |
|
575 |
selected = rqlst.children[0].selection # sample selection |
|
576 |
for i, term in enumerate(selected): |
|
577 |
if isinstance(term, Function) and term.descr().rtype is not None: |
|
578 |
basedescription.append(term.get_type(term.descr().rtype, args)) |
|
579 |
continue |
|
580 |
for vref in term.get_nodes(VariableRef): |
|
581 |
if vref.name in unstables: |
|
582 |
basedescription.append(None) |
|
583 |
todetermine.append( (i, is_final(rqlst, vref.variable, args)) ) |
|
584 |
break |
|
585 |
else: |
|
586 |
# sample etype |
|
587 |
etype = rqlst.children[0].solutions[0] |
|
588 |
basedescription.append(term.get_type(etype, args)) |
|
589 |
if not todetermine: |
|
590 |
return [tuple(basedescription)] * len(result) |
|
591 |
return self._build_descr(result, basedescription, todetermine) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
592 |
|
0 | 593 |
def _build_descr(self, result, basedescription, todetermine): |
594 |
description = [] |
|
595 |
etype_from_eid = self.describe |
|
596 |
for row in result: |
|
597 |
row_descr = basedescription |
|
598 |
for index, isfinal in todetermine: |
|
599 |
value = row[index] |
|
600 |
if value is None: |
|
601 |
# None value inserted by an outer join, no type |
|
602 |
row_descr[index] = None |
|
603 |
continue |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
604 |
if isfinal: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
605 |
row_descr[index] = etype_from_pyobj(value) |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
606 |
else: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
607 |
try: |
1169
52058e8a3af9
somewhat handle corrupted database when manually building a database
sylvain.thenault@logilab.fr
parents:
974
diff
changeset
|
608 |
row_descr[index] = etype_from_eid(value)[0] |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
609 |
except UnknownEid: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
610 |
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
|
611 |
row_descr[index] = row[index] = None |
0 | 612 |
description.append(tuple(row_descr)) |
613 |
return description |
|
614 |
||
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
|
615 |
@deprecated("use 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
|
616 |
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
|
617 |
"""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
|
618 |
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
|
619 |
|
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2466
diff
changeset
|
620 |
@deprecated('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
|
621 |
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
|
622 |
if setdefault: |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
623 |
assert not pop |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
624 |
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
|
625 |
if pop: |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
626 |
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
|
627 |
else: |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
628 |
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
|
629 |
|
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2466
diff
changeset
|
630 |
@deprecated('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
|
631 |
def entity(self, eid): |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
632 |
"""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
|
633 |
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
|
634 |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
635 |
|
0 | 636 |
class ChildSession(Session): |
637 |
"""child (or internal) session are used to hijack the security system |
|
638 |
""" |
|
639 |
cnxtype = 'inmemory' |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
640 |
|
0 | 641 |
def __init__(self, parent_session): |
642 |
self.id = None |
|
643 |
self.is_internal_session = False |
|
644 |
self.is_super_session = True |
|
645 |
# session which has created this one |
|
646 |
self.parent_session = parent_session |
|
647 |
self.user = InternalManager() |
|
648 |
self.repo = parent_session.repo |
|
649 |
self.vreg = parent_session.vreg |
|
650 |
self.data = parent_session.data |
|
651 |
self.encoding = parent_session.encoding |
|
652 |
self.lang = parent_session.lang |
|
653 |
self._ = self.__ = parent_session._ |
|
654 |
# short cut to querier .execute method |
|
655 |
self._execute = self.repo.querier.execute |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
656 |
|
0 | 657 |
@property |
658 |
def super_session(self): |
|
659 |
return self |
|
660 |
||
661 |
def get_mode(self): |
|
662 |
return self.parent_session.mode |
|
663 |
def set_mode(self, value): |
|
664 |
self.parent_session.set_mode(value) |
|
665 |
mode = property(get_mode, set_mode) |
|
666 |
||
667 |
def get_commit_state(self): |
|
668 |
return self.parent_session.commit_state |
|
669 |
def set_commit_state(self, value): |
|
670 |
self.parent_session.set_commit_state(value) |
|
671 |
commit_state = property(get_commit_state, set_commit_state) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
672 |
|
0 | 673 |
@property |
674 |
def pool(self): |
|
675 |
return self.parent_session.pool |
|
676 |
@property |
|
677 |
def pending_operations(self): |
|
678 |
return self.parent_session.pending_operations |
|
679 |
@property |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
680 |
def transaction_data(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
681 |
return self.parent_session.transaction_data |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
682 |
|
0 | 683 |
def set_pool(self): |
684 |
"""the session need a pool to execute some queries""" |
|
685 |
self.parent_session.set_pool() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
686 |
|
0 | 687 |
def reset_pool(self): |
688 |
"""the session has no longer using its pool, at least for some time |
|
689 |
""" |
|
690 |
self.parent_session.reset_pool() |
|
691 |
||
692 |
def actual_session(self): |
|
693 |
"""return the original parent session if any, else self""" |
|
694 |
return self.parent_session |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
695 |
|
0 | 696 |
def commit(self, reset_pool=True): |
697 |
"""commit the current session's transaction""" |
|
698 |
self.parent_session.commit(reset_pool) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
699 |
|
0 | 700 |
def rollback(self, reset_pool=True): |
701 |
"""rollback the current session's transaction""" |
|
702 |
self.parent_session.rollback(reset_pool) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
703 |
|
0 | 704 |
def close(self): |
705 |
"""do not close pool on session close, since they are shared now""" |
|
706 |
self.rollback() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
707 |
|
0 | 708 |
def user_data(self): |
709 |
"""returns a dictionnary with this user's information""" |
|
710 |
return self.parent_session.user_data() |
|
711 |
||
712 |
||
713 |
class InternalSession(Session): |
|
714 |
"""special session created internaly by the repository""" |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
715 |
|
0 | 716 |
def __init__(self, repo, cnxprops=None): |
717 |
super(InternalSession, self).__init__(_IMANAGER, repo, cnxprops, |
|
718 |
_id='internal') |
|
719 |
self.cnxtype = 'inmemory' |
|
720 |
self.is_internal_session = True |
|
721 |
self.is_super_session = True |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
722 |
|
0 | 723 |
@property |
724 |
def super_session(self): |
|
725 |
return self |
|
726 |
||
727 |
||
728 |
class InternalManager(object): |
|
729 |
"""a manager user with all access rights used internally for task such as |
|
730 |
bootstrapping the repository or creating regular users according to |
|
731 |
repository content |
|
732 |
""" |
|
733 |
def __init__(self): |
|
734 |
self.eid = -1 |
|
735 |
self.login = u'__internal_manager__' |
|
736 |
self.properties = {} |
|
737 |
||
738 |
def matching_groups(self, groups): |
|
739 |
return 1 |
|
740 |
||
741 |
def is_in_group(self, group): |
|
742 |
return True |
|
743 |
||
744 |
def owns(self, eid): |
|
745 |
return True |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
746 |
|
0 | 747 |
def has_permission(self, pname, contexteid=None): |
748 |
return True |
|
749 |
||
750 |
def property_value(self, key): |
|
751 |
if key == 'ui.language': |
|
752 |
return 'en' |
|
753 |
return None |
|
754 |
||
1132 | 755 |
_IMANAGER = InternalManager() |
0 | 756 |
|
757 |
from logging import getLogger |
|
758 |
from cubicweb import set_log_methods |
|
759 |
set_log_methods(Session, getLogger('cubicweb.session')) |