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