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