author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 24 Jul 2009 18:45:16 +0200 | |
changeset 2499 | 26fb72e0af23 |
parent 2466 | c4ccfd38a542 |
child 2589 | 92f2bc945261 |
child 2613 | 5e19c2bb370e |
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): |
|
2466
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
197 |
url = self.repo.config['base-url'] |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
198 |
if not url: |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
199 |
try: |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
200 |
url = self.repo.config.default_base_url() |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
201 |
except AttributeError: # default_base_url() might not be available |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
202 |
self.warning('missing base-url definition in server config') |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
203 |
url = u'' |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
204 |
return url |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
205 |
|
0 | 206 |
def from_controller(self): |
207 |
"""return the id (string) of the controller issuing the request (no |
|
208 |
sense here, always return 'view') |
|
209 |
""" |
|
210 |
return 'view' |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
211 |
|
0 | 212 |
def source_defs(self): |
213 |
return self.repo.source_defs() |
|
214 |
||
215 |
def describe(self, eid): |
|
216 |
"""return a tuple (type, sourceuri, extid) for the entity with id <eid>""" |
|
217 |
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
|
218 |
|
0 | 219 |
# db-api like interface ################################################### |
220 |
||
221 |
def source_from_eid(self, eid): |
|
222 |
"""return the source where the entity with id <eid> is located""" |
|
223 |
return self.repo.source_from_eid(eid, self) |
|
224 |
||
225 |
def decorate_rset(self, rset, propagate=False): |
|
226 |
rset.vreg = self.vreg |
|
227 |
rset.req = propagate and self or self.actual_session() |
|
228 |
return rset |
|
229 |
||
230 |
@property |
|
231 |
def super_session(self): |
|
232 |
try: |
|
233 |
csession = self._threaddata.childsession |
|
234 |
except AttributeError: |
|
235 |
if self.is_super_session: |
|
236 |
csession = self |
|
237 |
else: |
|
238 |
csession = ChildSession(self) |
|
239 |
self._threaddata.childsession = csession |
|
240 |
# need shared pool set |
|
241 |
self.set_pool() |
|
242 |
return csession |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
243 |
|
0 | 244 |
def unsafe_execute(self, rql, kwargs=None, eid_key=None, build_descr=False, |
245 |
propagate=False): |
|
246 |
"""like .execute but with security checking disabled (this method is |
|
247 |
internal to the server, it's not part of the db-api) |
|
248 |
||
249 |
if `propagate` is true, the super_session will be attached to the result |
|
250 |
set instead of the parent session, hence further query done through |
|
251 |
entities fetched from this result set will bypass security as well |
|
252 |
""" |
|
253 |
return self.super_session.execute(rql, kwargs, eid_key, build_descr, |
|
254 |
propagate) |
|
255 |
||
256 |
@property |
|
257 |
def cursor(self): |
|
258 |
"""return a rql cursor""" |
|
259 |
return self |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
260 |
|
0 | 261 |
def execute(self, rql, kwargs=None, eid_key=None, build_descr=True, |
262 |
propagate=False): |
|
263 |
"""db-api like method directly linked to the querier execute method |
|
264 |
||
265 |
Becare that unlike actual cursor.execute, `build_descr` default to |
|
266 |
false |
|
267 |
""" |
|
268 |
rset = self._execute(self, rql, kwargs, eid_key, build_descr) |
|
269 |
return self.decorate_rset(rset, propagate) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
270 |
|
0 | 271 |
def commit(self, reset_pool=True): |
272 |
"""commit the current session's transaction""" |
|
273 |
if self.pool is None: |
|
274 |
assert not self.pending_operations |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
275 |
self.transaction_data.clear() |
0 | 276 |
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
|
277 |
self.debug('commit session %s done (no db activity)', self.id) |
0 | 278 |
return |
279 |
if self.commit_state: |
|
280 |
return |
|
281 |
# on rollback, an operation should have the following state |
|
282 |
# information: |
|
283 |
# - processed by the precommit/commit event or not |
|
284 |
# - if processed, is it the failed operation |
|
285 |
try: |
|
286 |
for trstate in ('precommit', 'commit'): |
|
287 |
processed = [] |
|
288 |
self.commit_state = trstate |
|
289 |
try: |
|
290 |
while self.pending_operations: |
|
291 |
operation = self.pending_operations.pop(0) |
|
292 |
operation.processed = trstate |
|
293 |
processed.append(operation) |
|
294 |
operation.handle_event('%s_event' % trstate) |
|
295 |
self.pending_operations[:] = processed |
|
296 |
self.debug('%s session %s done', trstate, self.id) |
|
297 |
except: |
|
298 |
self.exception('error while %sing', trstate) |
|
299 |
operation.failed = True |
|
300 |
for operation in processed: |
|
301 |
operation.handle_event('revert%s_event' % trstate) |
|
302 |
self.rollback(reset_pool) |
|
303 |
raise |
|
304 |
self.pool.commit() |
|
305 |
finally: |
|
306 |
self._touch() |
|
307 |
self.commit_state = None |
|
308 |
self.pending_operations[:] = [] |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
309 |
self.transaction_data.clear() |
0 | 310 |
if reset_pool: |
311 |
self.reset_pool() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
312 |
|
0 | 313 |
def rollback(self, reset_pool=True): |
314 |
"""rollback the current session's transaction""" |
|
315 |
if self.pool is None: |
|
316 |
assert not self.pending_operations |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
317 |
self.transaction_data.clear() |
0 | 318 |
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
|
319 |
self.debug('rollback session %s done (no db activity)', self.id) |
0 | 320 |
return |
321 |
try: |
|
322 |
while self.pending_operations: |
|
323 |
try: |
|
324 |
operation = self.pending_operations.pop(0) |
|
325 |
operation.handle_event('rollback_event') |
|
326 |
except: |
|
327 |
self.critical('rollback error', exc_info=sys.exc_info()) |
|
328 |
continue |
|
329 |
self.pool.rollback() |
|
2188
3a57c8173290
* deprecate session.entity method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
330 |
self.debug('rollback for session %s done', self.id) |
0 | 331 |
finally: |
332 |
self._touch() |
|
333 |
self.pending_operations[:] = [] |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
334 |
self.transaction_data.clear() |
0 | 335 |
if reset_pool: |
336 |
self.reset_pool() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
337 |
|
0 | 338 |
def close(self): |
339 |
"""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
|
340 |
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
|
341 |
# 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
|
342 |
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
|
343 |
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
|
344 |
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
|
345 |
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
|
346 |
# 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
|
347 |
# 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
|
348 |
# 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
|
349 |
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
|
350 |
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
|
351 |
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
|
352 |
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
|
353 |
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
|
354 |
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
|
355 |
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
|
356 |
'session anyway', thread) |
0 | 357 |
self.rollback() |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
358 |
|
0 | 359 |
# transaction data/operations management ################################## |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
360 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
361 |
@property |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
362 |
def transaction_data(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
363 |
try: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
364 |
return self._threaddata.transaction_data |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
365 |
except AttributeError: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
366 |
self._threaddata.transaction_data = {} |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
367 |
return self._threaddata.transaction_data |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
368 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
369 |
@property |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
370 |
def pending_operations(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
371 |
try: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
372 |
return self._threaddata.pending_operations |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
373 |
except AttributeError: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
374 |
self._threaddata.pending_operations = [] |
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 |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
377 |
|
0 | 378 |
def add_operation(self, operation, index=None): |
379 |
"""add an observer""" |
|
380 |
assert self.commit_state != 'commit' |
|
381 |
if index is not None: |
|
382 |
self.pending_operations.insert(index, operation) |
|
383 |
else: |
|
384 |
self.pending_operations.append(operation) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
385 |
|
0 | 386 |
# querier helpers ######################################################### |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
387 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
388 |
@property |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
389 |
def rql_rewriter(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
390 |
try: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
391 |
return self._threaddata._rewriter |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
392 |
except AttributeError: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
393 |
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
|
394 |
return self._threaddata._rewriter |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
395 |
|
0 | 396 |
def build_description(self, rqlst, args, result): |
397 |
"""build a description for a given result""" |
|
398 |
if len(rqlst.children) == 1 and len(rqlst.children[0].solutions) == 1: |
|
399 |
# easy, all lines are identical |
|
400 |
selected = rqlst.children[0].selection |
|
401 |
solution = rqlst.children[0].solutions[0] |
|
402 |
description = _make_description(selected, args, solution) |
|
403 |
return [tuple(description)] * len(result) |
|
404 |
# hard, delegate the work :o) |
|
405 |
return self.manual_build_descr(rqlst, args, result) |
|
406 |
||
407 |
def manual_build_descr(self, rqlst, args, result): |
|
408 |
"""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
|
409 |
|
0 | 410 |
XXX could probably be done more efficiently during execution of query |
411 |
""" |
|
412 |
# not so easy, looks for variable which changes from one solution |
|
413 |
# to another |
|
414 |
unstables = rqlst.get_variable_variables() |
|
415 |
basedescription = [] |
|
416 |
todetermine = [] |
|
417 |
selected = rqlst.children[0].selection # sample selection |
|
418 |
for i, term in enumerate(selected): |
|
419 |
if isinstance(term, Function) and term.descr().rtype is not None: |
|
420 |
basedescription.append(term.get_type(term.descr().rtype, args)) |
|
421 |
continue |
|
422 |
for vref in term.get_nodes(VariableRef): |
|
423 |
if vref.name in unstables: |
|
424 |
basedescription.append(None) |
|
425 |
todetermine.append( (i, is_final(rqlst, vref.variable, args)) ) |
|
426 |
break |
|
427 |
else: |
|
428 |
# sample etype |
|
429 |
etype = rqlst.children[0].solutions[0] |
|
430 |
basedescription.append(term.get_type(etype, args)) |
|
431 |
if not todetermine: |
|
432 |
return [tuple(basedescription)] * len(result) |
|
433 |
return self._build_descr(result, basedescription, todetermine) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
434 |
|
0 | 435 |
def _build_descr(self, result, basedescription, todetermine): |
436 |
description = [] |
|
437 |
etype_from_eid = self.describe |
|
438 |
for row in result: |
|
439 |
row_descr = basedescription |
|
440 |
for index, isfinal in todetermine: |
|
441 |
value = row[index] |
|
442 |
if value is None: |
|
443 |
# None value inserted by an outer join, no type |
|
444 |
row_descr[index] = None |
|
445 |
continue |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
446 |
if isfinal: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
447 |
row_descr[index] = etype_from_pyobj(value) |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
448 |
else: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
449 |
try: |
1169
52058e8a3af9
somewhat handle corrupted database when manually building a database
sylvain.thenault@logilab.fr
parents:
974
diff
changeset
|
450 |
row_descr[index] = etype_from_eid(value)[0] |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
451 |
except UnknownEid: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
452 |
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
|
453 |
row_descr[index] = row[index] = None |
0 | 454 |
description.append(tuple(row_descr)) |
455 |
return description |
|
456 |
||
2182
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
457 |
@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
|
458 |
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
|
459 |
if setdefault: |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
460 |
assert not pop |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
461 |
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
|
462 |
if pop: |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
463 |
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
|
464 |
else: |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
465 |
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
|
466 |
|
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
467 |
@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
|
468 |
def entity(self, eid): |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
469 |
"""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
|
470 |
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
|
471 |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
472 |
|
0 | 473 |
class ChildSession(Session): |
474 |
"""child (or internal) session are used to hijack the security system |
|
475 |
""" |
|
476 |
cnxtype = 'inmemory' |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
477 |
|
0 | 478 |
def __init__(self, parent_session): |
479 |
self.id = None |
|
480 |
self.is_internal_session = False |
|
481 |
self.is_super_session = True |
|
482 |
# session which has created this one |
|
483 |
self.parent_session = parent_session |
|
484 |
self.user = InternalManager() |
|
485 |
self.repo = parent_session.repo |
|
486 |
self.vreg = parent_session.vreg |
|
487 |
self.data = parent_session.data |
|
488 |
self.encoding = parent_session.encoding |
|
489 |
self.lang = parent_session.lang |
|
490 |
self._ = self.__ = parent_session._ |
|
491 |
# short cut to querier .execute method |
|
492 |
self._execute = self.repo.querier.execute |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
493 |
|
0 | 494 |
@property |
495 |
def super_session(self): |
|
496 |
return self |
|
497 |
||
498 |
def get_mode(self): |
|
499 |
return self.parent_session.mode |
|
500 |
def set_mode(self, value): |
|
501 |
self.parent_session.set_mode(value) |
|
502 |
mode = property(get_mode, set_mode) |
|
503 |
||
504 |
def get_commit_state(self): |
|
505 |
return self.parent_session.commit_state |
|
506 |
def set_commit_state(self, value): |
|
507 |
self.parent_session.set_commit_state(value) |
|
508 |
commit_state = property(get_commit_state, set_commit_state) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
509 |
|
0 | 510 |
@property |
511 |
def pool(self): |
|
512 |
return self.parent_session.pool |
|
513 |
@property |
|
514 |
def pending_operations(self): |
|
515 |
return self.parent_session.pending_operations |
|
516 |
@property |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
517 |
def transaction_data(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
518 |
return self.parent_session.transaction_data |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
519 |
|
0 | 520 |
def set_pool(self): |
521 |
"""the session need a pool to execute some queries""" |
|
522 |
self.parent_session.set_pool() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
523 |
|
0 | 524 |
def reset_pool(self): |
525 |
"""the session has no longer using its pool, at least for some time |
|
526 |
""" |
|
527 |
self.parent_session.reset_pool() |
|
528 |
||
529 |
def actual_session(self): |
|
530 |
"""return the original parent session if any, else self""" |
|
531 |
return self.parent_session |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
532 |
|
0 | 533 |
def commit(self, reset_pool=True): |
534 |
"""commit the current session's transaction""" |
|
535 |
self.parent_session.commit(reset_pool) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
536 |
|
0 | 537 |
def rollback(self, reset_pool=True): |
538 |
"""rollback the current session's transaction""" |
|
539 |
self.parent_session.rollback(reset_pool) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
540 |
|
0 | 541 |
def close(self): |
542 |
"""do not close pool on session close, since they are shared now""" |
|
543 |
self.rollback() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
544 |
|
0 | 545 |
def user_data(self): |
546 |
"""returns a dictionnary with this user's information""" |
|
547 |
return self.parent_session.user_data() |
|
548 |
||
549 |
||
550 |
class InternalSession(Session): |
|
551 |
"""special session created internaly by the repository""" |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
552 |
|
0 | 553 |
def __init__(self, repo, cnxprops=None): |
554 |
super(InternalSession, self).__init__(_IMANAGER, repo, cnxprops, |
|
555 |
_id='internal') |
|
556 |
self.cnxtype = 'inmemory' |
|
557 |
self.is_internal_session = True |
|
558 |
self.is_super_session = True |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
559 |
|
0 | 560 |
@property |
561 |
def super_session(self): |
|
562 |
return self |
|
563 |
||
564 |
||
565 |
class InternalManager(object): |
|
566 |
"""a manager user with all access rights used internally for task such as |
|
567 |
bootstrapping the repository or creating regular users according to |
|
568 |
repository content |
|
569 |
""" |
|
570 |
def __init__(self): |
|
571 |
self.eid = -1 |
|
572 |
self.login = u'__internal_manager__' |
|
573 |
self.properties = {} |
|
574 |
||
575 |
def matching_groups(self, groups): |
|
576 |
return 1 |
|
577 |
||
578 |
def is_in_group(self, group): |
|
579 |
return True |
|
580 |
||
581 |
def owns(self, eid): |
|
582 |
return True |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
583 |
|
0 | 584 |
def has_permission(self, pname, contexteid=None): |
585 |
return True |
|
586 |
||
587 |
def property_value(self, key): |
|
588 |
if key == 'ui.language': |
|
589 |
return 'en' |
|
590 |
return None |
|
591 |
||
1132 | 592 |
_IMANAGER = InternalManager() |
0 | 593 |
|
594 |
from logging import getLogger |
|
595 |
from cubicweb import set_log_methods |
|
596 |
set_log_methods(Session, getLogger('cubicweb.session')) |