author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 29 Jun 2009 17:02:51 +0200 | |
branch | stable |
changeset 2195 | 58bef4f707ce |
parent 2190 | d9f5fd1879fc |
child 2200 | 25bb65dc4559 |
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 |
||
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
14 |
from logilab.common.deprecation import obsolete |
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 |
0 | 21 |
from cubicweb.server.rqlrewrite import RQLRewriter |
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 |
from rql import stmts |
|
746 | 44 |
assert hasattr(stmts.Union, 'get_variable_variables'), "You need RQL > 0.18.3" |
0 | 45 |
|
46 |
class Session(RequestSessionMixIn): |
|
47 |
"""tie session id, user, connections pool and other session data all |
|
48 |
together |
|
49 |
""" |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
50 |
|
0 | 51 |
def __init__(self, user, repo, cnxprops=None, _id=None): |
52 |
super(Session, self).__init__(repo.vreg) |
|
53 |
self.id = _id or make_uid(user.login.encode('UTF8')) |
|
54 |
cnxprops = cnxprops or ConnectionProperties('inmemory') |
|
55 |
self.user = user |
|
56 |
self.repo = repo |
|
57 |
self.cnxtype = cnxprops.cnxtype |
|
58 |
self.creation = time() |
|
59 |
self.timestamp = self.creation |
|
60 |
self.is_internal_session = False |
|
61 |
self.is_super_session = False |
|
62 |
# short cut to querier .execute method |
|
63 |
self._execute = repo.querier.execute |
|
64 |
# shared data, used to communicate extra information between the client |
|
65 |
# and the rql server |
|
66 |
self.data = {} |
|
67 |
# i18n initialization |
|
68 |
self.set_language(cnxprops.lang) |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
69 |
# internals |
0 | 70 |
self._threaddata = threading.local() |
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
71 |
self._threads_in_transaction = set() |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
72 |
self._closed = False |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
73 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
74 |
def __str__(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
75 |
return '<%ssession %s (%s 0x%x)>' % (self.cnxtype, self.user.login, |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
76 |
self.id, id(self)) |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
77 |
# resource accessors ###################################################### |
0 | 78 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
79 |
def actual_session(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
80 |
"""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
|
81 |
return self |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
82 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
83 |
def etype_class(self, etype): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
84 |
"""return an entity class for the given entity type""" |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
85 |
return self.vreg.etype_class(etype) |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
86 |
|
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
87 |
def system_sql(self, sql, args=None): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
88 |
"""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
|
89 |
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
|
90 |
self.mode = 'write' |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
91 |
cursor = self.pool['system'] |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
92 |
self.pool.source('system').doexec(cursor, sql, args) |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
93 |
return cursor |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
94 |
|
0 | 95 |
def set_language(self, language): |
96 |
"""i18n configuration for translation""" |
|
97 |
vreg = self.vreg |
|
98 |
language = language or self.user.property_value('ui.language') |
|
99 |
try: |
|
100 |
self._ = self.__ = vreg.config.translations[language] |
|
101 |
except KeyError: |
|
102 |
language = vreg.property_value('ui.language') |
|
103 |
try: |
|
104 |
self._ = self.__ = vreg.config.translations[language] |
|
105 |
except KeyError: |
|
106 |
self._ = self.__ = unicode |
|
107 |
self.lang = language |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
108 |
|
0 | 109 |
def change_property(self, prop, value): |
110 |
assert prop == 'lang' # this is the only one changeable property for now |
|
111 |
self.set_language(value) |
|
112 |
||
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
113 |
# connection management ################################################### |
0 | 114 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
115 |
def get_mode(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
116 |
return getattr(self._threaddata, 'mode', 'read') |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
117 |
def set_mode(self, value): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
118 |
self._threaddata.mode = value |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
119 |
mode = property(get_mode, set_mode, |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
120 |
doc='transaction mode (read/write), resetted to read on ' |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
121 |
'commit / rollback') |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
122 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
123 |
def get_commit_state(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
124 |
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
|
125 |
def set_commit_state(self, value): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
126 |
self._threaddata.commit_state = value |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
127 |
commit_state = property(get_commit_state, set_commit_state) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
128 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
129 |
@property |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
130 |
def pool(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
131 |
"""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
|
132 |
return getattr(self._threaddata, 'pool', None) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
133 |
|
0 | 134 |
def set_pool(self): |
135 |
"""the session need a pool to execute some queries""" |
|
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
|
136 |
if self._closed: |
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
|
137 |
raise Exception('try to set pool on a closed session') |
0 | 138 |
if self.pool is None: |
2054
277e8d3b1154
fix potential race-condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
139 |
# get pool first to avoid race-condition |
0 | 140 |
self._threaddata.pool = self.repo._get_pool() |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
141 |
try: |
2063
fe4278b50388
fix [re]set_pool prototype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2055
diff
changeset
|
142 |
self._threaddata.pool.pool_set() |
0 | 143 |
except: |
2054
277e8d3b1154
fix potential race-condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
144 |
self._threaddata.pool = None |
0 | 145 |
self.repo._free_pool(self.pool) |
146 |
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
|
147 |
self._threads_in_transaction.add(threading.currentThread()) |
0 | 148 |
return self._threaddata.pool |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
149 |
|
0 | 150 |
def reset_pool(self): |
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
151 |
"""the session has no longer using its pool, at least for some time""" |
0 | 152 |
# pool may be none if no operation has been done since last commit |
153 |
# or rollback |
|
154 |
if self.pool is not None and self.mode == 'read': |
|
155 |
# even in read mode, we must release the current transaction |
|
2055 | 156 |
pool = self.pool |
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
|
157 |
self._threads_in_transaction.remove(threading.currentThread()) |
2063
fe4278b50388
fix [re]set_pool prototype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2055
diff
changeset
|
158 |
pool.pool_reset() |
0 | 159 |
self._threaddata.pool = None |
2054
277e8d3b1154
fix potential race-condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
160 |
# free pool once everything is done to avoid race-condition |
2055 | 161 |
self.repo._free_pool(pool) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
162 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
163 |
def _touch(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
164 |
"""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
|
165 |
self.timestamp = time() |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
166 |
self.local_perm_cache.clear() |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
167 |
self._threaddata.mode = 'read' |
0 | 168 |
|
169 |
# shared data handling ################################################### |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
170 |
|
0 | 171 |
def get_shared_data(self, key, default=None, pop=False): |
172 |
"""return value associated to `key` in session data""" |
|
173 |
if pop: |
|
174 |
return self.data.pop(key, default) |
|
175 |
else: |
|
176 |
return self.data.get(key, default) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
177 |
|
0 | 178 |
def set_shared_data(self, key, value, querydata=False): |
179 |
"""set value associated to `key` in session data""" |
|
180 |
if querydata: |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
181 |
self.transaction_data[key] = value |
0 | 182 |
else: |
183 |
self.data[key] = value |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
184 |
|
0 | 185 |
# request interface ####################################################### |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
186 |
|
0 | 187 |
def set_entity_cache(self, entity): |
188 |
# no entity cache in the server, too high risk of inconsistency |
|
189 |
# between pre/post hooks |
|
190 |
pass |
|
191 |
||
192 |
def entity_cache(self, eid): |
|
193 |
raise KeyError(eid) |
|
194 |
||
195 |
def base_url(self): |
|
196 |
return self.repo.config['base-url'] or u'' |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
197 |
|
0 | 198 |
def from_controller(self): |
199 |
"""return the id (string) of the controller issuing the request (no |
|
200 |
sense here, always return 'view') |
|
201 |
""" |
|
202 |
return 'view' |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
203 |
|
0 | 204 |
def source_defs(self): |
205 |
return self.repo.source_defs() |
|
206 |
||
207 |
def describe(self, eid): |
|
208 |
"""return a tuple (type, sourceuri, extid) for the entity with id <eid>""" |
|
209 |
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
|
210 |
|
0 | 211 |
# db-api like interface ################################################### |
212 |
||
213 |
def source_from_eid(self, eid): |
|
214 |
"""return the source where the entity with id <eid> is located""" |
|
215 |
return self.repo.source_from_eid(eid, self) |
|
216 |
||
217 |
def decorate_rset(self, rset, propagate=False): |
|
218 |
rset.vreg = self.vreg |
|
219 |
rset.req = propagate and self or self.actual_session() |
|
220 |
return rset |
|
221 |
||
222 |
@property |
|
223 |
def super_session(self): |
|
224 |
try: |
|
225 |
csession = self._threaddata.childsession |
|
226 |
except AttributeError: |
|
227 |
if self.is_super_session: |
|
228 |
csession = self |
|
229 |
else: |
|
230 |
csession = ChildSession(self) |
|
231 |
self._threaddata.childsession = csession |
|
232 |
# need shared pool set |
|
233 |
self.set_pool() |
|
234 |
return csession |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
235 |
|
0 | 236 |
def unsafe_execute(self, rql, kwargs=None, eid_key=None, build_descr=False, |
237 |
propagate=False): |
|
238 |
"""like .execute but with security checking disabled (this method is |
|
239 |
internal to the server, it's not part of the db-api) |
|
240 |
||
241 |
if `propagate` is true, the super_session will be attached to the result |
|
242 |
set instead of the parent session, hence further query done through |
|
243 |
entities fetched from this result set will bypass security as well |
|
244 |
""" |
|
245 |
return self.super_session.execute(rql, kwargs, eid_key, build_descr, |
|
246 |
propagate) |
|
247 |
||
248 |
@property |
|
249 |
def cursor(self): |
|
250 |
"""return a rql cursor""" |
|
251 |
return self |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
252 |
|
0 | 253 |
def execute(self, rql, kwargs=None, eid_key=None, build_descr=True, |
254 |
propagate=False): |
|
255 |
"""db-api like method directly linked to the querier execute method |
|
256 |
||
257 |
Becare that unlike actual cursor.execute, `build_descr` default to |
|
258 |
false |
|
259 |
""" |
|
260 |
rset = self._execute(self, rql, kwargs, eid_key, build_descr) |
|
261 |
return self.decorate_rset(rset, propagate) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
262 |
|
0 | 263 |
def commit(self, reset_pool=True): |
264 |
"""commit the current session's transaction""" |
|
265 |
if self.pool is None: |
|
266 |
assert not self.pending_operations |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
267 |
self.transaction_data.clear() |
0 | 268 |
self._touch() |
269 |
return |
|
270 |
if self.commit_state: |
|
271 |
return |
|
272 |
# on rollback, an operation should have the following state |
|
273 |
# information: |
|
274 |
# - processed by the precommit/commit event or not |
|
275 |
# - if processed, is it the failed operation |
|
276 |
try: |
|
277 |
for trstate in ('precommit', 'commit'): |
|
278 |
processed = [] |
|
279 |
self.commit_state = trstate |
|
280 |
try: |
|
281 |
while self.pending_operations: |
|
282 |
operation = self.pending_operations.pop(0) |
|
283 |
operation.processed = trstate |
|
284 |
processed.append(operation) |
|
285 |
operation.handle_event('%s_event' % trstate) |
|
286 |
self.pending_operations[:] = processed |
|
287 |
self.debug('%s session %s done', trstate, self.id) |
|
288 |
except: |
|
289 |
self.exception('error while %sing', trstate) |
|
290 |
operation.failed = True |
|
291 |
for operation in processed: |
|
292 |
operation.handle_event('revert%s_event' % trstate) |
|
293 |
self.rollback(reset_pool) |
|
294 |
raise |
|
295 |
self.pool.commit() |
|
296 |
finally: |
|
297 |
self._touch() |
|
298 |
self.commit_state = None |
|
299 |
self.pending_operations[:] = [] |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
300 |
self.transaction_data.clear() |
0 | 301 |
if reset_pool: |
302 |
self.reset_pool() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
303 |
|
0 | 304 |
def rollback(self, reset_pool=True): |
305 |
"""rollback the current session's transaction""" |
|
306 |
if self.pool is None: |
|
307 |
assert not self.pending_operations |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
308 |
self.transaction_data.clear() |
0 | 309 |
self._touch() |
310 |
return |
|
311 |
try: |
|
312 |
while self.pending_operations: |
|
313 |
try: |
|
314 |
operation = self.pending_operations.pop(0) |
|
315 |
operation.handle_event('rollback_event') |
|
316 |
except: |
|
317 |
self.critical('rollback error', exc_info=sys.exc_info()) |
|
318 |
continue |
|
319 |
self.pool.rollback() |
|
2188
3a57c8173290
* deprecate session.entity method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
320 |
self.debug('rollback for session %s done', self.id) |
0 | 321 |
finally: |
322 |
self._touch() |
|
323 |
self.pending_operations[:] = [] |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
324 |
self.transaction_data.clear() |
0 | 325 |
if reset_pool: |
326 |
self.reset_pool() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
327 |
|
0 | 328 |
def close(self): |
329 |
"""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
|
330 |
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
|
331 |
# 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
|
332 |
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
|
333 |
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
|
334 |
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
|
335 |
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
|
336 |
# 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
|
337 |
# 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
|
338 |
# 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
|
339 |
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
|
340 |
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
|
341 |
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
|
342 |
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
|
343 |
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
|
344 |
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
|
345 |
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
|
346 |
'session anyway', thread) |
0 | 347 |
self.rollback() |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
348 |
|
0 | 349 |
# transaction data/operations management ################################## |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
350 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
351 |
@property |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
352 |
def transaction_data(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
353 |
try: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
354 |
return self._threaddata.transaction_data |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
355 |
except AttributeError: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
356 |
self._threaddata.transaction_data = {} |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
357 |
return self._threaddata.transaction_data |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
358 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
359 |
@property |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
360 |
def pending_operations(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
361 |
try: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
362 |
return self._threaddata.pending_operations |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
363 |
except AttributeError: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
364 |
self._threaddata.pending_operations = [] |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
365 |
return self._threaddata.pending_operations |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
366 |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
367 |
|
0 | 368 |
def add_operation(self, operation, index=None): |
369 |
"""add an observer""" |
|
370 |
assert self.commit_state != 'commit' |
|
371 |
if index is not None: |
|
372 |
self.pending_operations.insert(index, operation) |
|
373 |
else: |
|
374 |
self.pending_operations.append(operation) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
375 |
|
0 | 376 |
# querier helpers ######################################################### |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
377 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
378 |
@property |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
379 |
def rql_rewriter(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
380 |
try: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
381 |
return self._threaddata._rewriter |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
382 |
except AttributeError: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
383 |
self._threaddata._rewriter = RQLRewriter(self.repo.querier, self) |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
384 |
return self._threaddata._rewriter |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
385 |
|
0 | 386 |
def build_description(self, rqlst, args, result): |
387 |
"""build a description for a given result""" |
|
388 |
if len(rqlst.children) == 1 and len(rqlst.children[0].solutions) == 1: |
|
389 |
# easy, all lines are identical |
|
390 |
selected = rqlst.children[0].selection |
|
391 |
solution = rqlst.children[0].solutions[0] |
|
392 |
description = _make_description(selected, args, solution) |
|
393 |
return [tuple(description)] * len(result) |
|
394 |
# hard, delegate the work :o) |
|
395 |
return self.manual_build_descr(rqlst, args, result) |
|
396 |
||
397 |
def manual_build_descr(self, rqlst, args, result): |
|
398 |
"""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
|
399 |
|
0 | 400 |
XXX could probably be done more efficiently during execution of query |
401 |
""" |
|
402 |
# not so easy, looks for variable which changes from one solution |
|
403 |
# to another |
|
404 |
unstables = rqlst.get_variable_variables() |
|
405 |
basedescription = [] |
|
406 |
todetermine = [] |
|
407 |
selected = rqlst.children[0].selection # sample selection |
|
408 |
for i, term in enumerate(selected): |
|
409 |
if isinstance(term, Function) and term.descr().rtype is not None: |
|
410 |
basedescription.append(term.get_type(term.descr().rtype, args)) |
|
411 |
continue |
|
412 |
for vref in term.get_nodes(VariableRef): |
|
413 |
if vref.name in unstables: |
|
414 |
basedescription.append(None) |
|
415 |
todetermine.append( (i, is_final(rqlst, vref.variable, args)) ) |
|
416 |
break |
|
417 |
else: |
|
418 |
# sample etype |
|
419 |
etype = rqlst.children[0].solutions[0] |
|
420 |
basedescription.append(term.get_type(etype, args)) |
|
421 |
if not todetermine: |
|
422 |
return [tuple(basedescription)] * len(result) |
|
423 |
return self._build_descr(result, basedescription, todetermine) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
424 |
|
0 | 425 |
def _build_descr(self, result, basedescription, todetermine): |
426 |
description = [] |
|
427 |
etype_from_eid = self.describe |
|
428 |
for row in result: |
|
429 |
row_descr = basedescription |
|
430 |
for index, isfinal in todetermine: |
|
431 |
value = row[index] |
|
432 |
if value is None: |
|
433 |
# None value inserted by an outer join, no type |
|
434 |
row_descr[index] = None |
|
435 |
continue |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
436 |
if isfinal: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
437 |
row_descr[index] = etype_from_pyobj(value) |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
438 |
else: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
439 |
try: |
1169
52058e8a3af9
somewhat handle corrupted database when manually building a database
sylvain.thenault@logilab.fr
parents:
974
diff
changeset
|
440 |
row_descr[index] = etype_from_eid(value)[0] |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
441 |
except UnknownEid: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
442 |
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
|
443 |
row_descr[index] = row[index] = None |
0 | 444 |
description.append(tuple(row_descr)) |
445 |
return description |
|
446 |
||
2182
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
447 |
@obsolete('use direct access to session.transaction_data') |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
448 |
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
|
449 |
if setdefault: |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
450 |
assert not pop |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
451 |
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
|
452 |
if pop: |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
453 |
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
|
454 |
else: |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
455 |
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
|
456 |
|
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
457 |
@obsolete('use entity_from_eid(eid, etype=None)') |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
458 |
def entity(self, eid): |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
459 |
"""return a result set for the given eid""" |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
460 |
return self.eid_rset(eid).get_entity(0, 0) |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
461 |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
462 |
|
0 | 463 |
class ChildSession(Session): |
464 |
"""child (or internal) session are used to hijack the security system |
|
465 |
""" |
|
466 |
cnxtype = 'inmemory' |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
467 |
|
0 | 468 |
def __init__(self, parent_session): |
469 |
self.id = None |
|
470 |
self.is_internal_session = False |
|
471 |
self.is_super_session = True |
|
472 |
# session which has created this one |
|
473 |
self.parent_session = parent_session |
|
474 |
self.user = InternalManager() |
|
475 |
self.repo = parent_session.repo |
|
476 |
self.vreg = parent_session.vreg |
|
477 |
self.data = parent_session.data |
|
478 |
self.encoding = parent_session.encoding |
|
479 |
self.lang = parent_session.lang |
|
480 |
self._ = self.__ = parent_session._ |
|
481 |
# short cut to querier .execute method |
|
482 |
self._execute = self.repo.querier.execute |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
483 |
|
0 | 484 |
@property |
485 |
def super_session(self): |
|
486 |
return self |
|
487 |
||
488 |
def get_mode(self): |
|
489 |
return self.parent_session.mode |
|
490 |
def set_mode(self, value): |
|
491 |
self.parent_session.set_mode(value) |
|
492 |
mode = property(get_mode, set_mode) |
|
493 |
||
494 |
def get_commit_state(self): |
|
495 |
return self.parent_session.commit_state |
|
496 |
def set_commit_state(self, value): |
|
497 |
self.parent_session.set_commit_state(value) |
|
498 |
commit_state = property(get_commit_state, set_commit_state) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
499 |
|
0 | 500 |
@property |
501 |
def pool(self): |
|
502 |
return self.parent_session.pool |
|
503 |
@property |
|
504 |
def pending_operations(self): |
|
505 |
return self.parent_session.pending_operations |
|
506 |
@property |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
507 |
def transaction_data(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
508 |
return self.parent_session.transaction_data |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
509 |
|
0 | 510 |
def set_pool(self): |
511 |
"""the session need a pool to execute some queries""" |
|
512 |
self.parent_session.set_pool() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
513 |
|
0 | 514 |
def reset_pool(self): |
515 |
"""the session has no longer using its pool, at least for some time |
|
516 |
""" |
|
517 |
self.parent_session.reset_pool() |
|
518 |
||
519 |
def actual_session(self): |
|
520 |
"""return the original parent session if any, else self""" |
|
521 |
return self.parent_session |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
522 |
|
0 | 523 |
def commit(self, reset_pool=True): |
524 |
"""commit the current session's transaction""" |
|
525 |
self.parent_session.commit(reset_pool) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
526 |
|
0 | 527 |
def rollback(self, reset_pool=True): |
528 |
"""rollback the current session's transaction""" |
|
529 |
self.parent_session.rollback(reset_pool) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
530 |
|
0 | 531 |
def close(self): |
532 |
"""do not close pool on session close, since they are shared now""" |
|
533 |
self.rollback() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
534 |
|
0 | 535 |
def user_data(self): |
536 |
"""returns a dictionnary with this user's information""" |
|
537 |
return self.parent_session.user_data() |
|
538 |
||
539 |
||
540 |
class InternalSession(Session): |
|
541 |
"""special session created internaly by the repository""" |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
542 |
|
0 | 543 |
def __init__(self, repo, cnxprops=None): |
544 |
super(InternalSession, self).__init__(_IMANAGER, repo, cnxprops, |
|
545 |
_id='internal') |
|
546 |
self.cnxtype = 'inmemory' |
|
547 |
self.is_internal_session = True |
|
548 |
self.is_super_session = True |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
549 |
|
0 | 550 |
@property |
551 |
def super_session(self): |
|
552 |
return self |
|
553 |
||
554 |
||
555 |
class InternalManager(object): |
|
556 |
"""a manager user with all access rights used internally for task such as |
|
557 |
bootstrapping the repository or creating regular users according to |
|
558 |
repository content |
|
559 |
""" |
|
560 |
def __init__(self): |
|
561 |
self.eid = -1 |
|
562 |
self.login = u'__internal_manager__' |
|
563 |
self.properties = {} |
|
564 |
||
565 |
def matching_groups(self, groups): |
|
566 |
return 1 |
|
567 |
||
568 |
def is_in_group(self, group): |
|
569 |
return True |
|
570 |
||
571 |
def owns(self, eid): |
|
572 |
return True |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
573 |
|
0 | 574 |
def has_permission(self, pname, contexteid=None): |
575 |
return True |
|
576 |
||
577 |
def property_value(self, key): |
|
578 |
if key == 'ui.language': |
|
579 |
return 'en' |
|
580 |
return None |
|
581 |
||
1132 | 582 |
_IMANAGER = InternalManager() |
0 | 583 |
|
584 |
from logging import getLogger |
|
585 |
from cubicweb import set_log_methods |
|
586 |
set_log_methods(Session, getLogger('cubicweb.session')) |