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