author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 17 Aug 2009 12:11:04 +0200 | |
branch | stable |
changeset 2874 | acdd8d8c2cff |
parent 2705 | 30bcdbd92820 |
child 2790 | 968108e16066 |
child 2919 | 662f35236d1c |
permissions | -rw-r--r-- |
0 | 1 |
"""CubicWeb is a generic framework to quickly build applications which describes |
2 |
relations between entitites. |
|
3 |
||
4 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1954
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1030
2ead2111cfab
update license, specify which the version of LGPL under which the code is provided
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1022
diff
changeset
|
7 |
:license: Library General Public License version 2 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
10 |
from cubicweb.__pkginfo__ import version as __version__ |
|
11 |
||
12 |
import __builtin__ |
|
13 |
# '_' is available in builtins to mark internationalized string but should |
|
14 |
# not be used to do the actual translation |
|
15 |
if not hasattr(__builtin__, '_'): |
|
16 |
__builtin__._ = unicode |
|
17 |
||
18 |
CW_SOFTWARE_ROOT = __path__[0] |
|
19 |
||
20 |
import sys, os, logging |
|
21 |
from StringIO import StringIO |
|
22 |
from urllib import quote as urlquote, unquote as urlunquote |
|
23 |
||
24 |
from logilab.common.decorators import cached |
|
1741 | 25 |
from logilab.common.logging_ext import set_log_methods |
0 | 26 |
|
27 |
if os.environ.get('APYCOT_ROOT'): |
|
28 |
logging.basicConfig(level=logging.CRITICAL) |
|
29 |
else: |
|
30 |
logging.basicConfig() |
|
31 |
||
32 |
||
33 |
set_log_methods(sys.modules[__name__], logging.getLogger('cubicweb')) |
|
34 |
||
35 |
# make all exceptions accessible from the package |
|
36 |
from cubicweb._exceptions import * |
|
37 |
||
38 |
# convert eid to the right type, raise ValueError if it's not a valid eid |
|
39 |
typed_eid = int |
|
40 |
||
41 |
||
42 |
#def log_thread(f, w, a): |
|
43 |
# print f.f_code.co_filename, f.f_code.co_name |
|
44 |
#import threading |
|
45 |
#threading.settrace(log_thread) |
|
46 |
||
47 |
class Binary(StringIO): |
|
48 |
"""customize StringIO to make sure we don't use unicode""" |
|
1954 | 49 |
def __init__(self, buf=''): |
0 | 50 |
assert isinstance(buf, (str, buffer)), \ |
51 |
"Binary objects must use raw strings, not %s" % buf.__class__ |
|
52 |
StringIO.__init__(self, buf) |
|
53 |
||
54 |
def write(self, data): |
|
55 |
assert isinstance(data, (str, buffer)), \ |
|
56 |
"Binary objects must use raw strings, not %s" % data.__class__ |
|
57 |
StringIO.write(self, data) |
|
58 |
||
59 |
||
60 |
class RequestSessionMixIn(object): |
|
61 |
"""mixin class containing stuff shared by server session and web request |
|
62 |
""" |
|
63 |
def __init__(self, vreg): |
|
64 |
self.vreg = vreg |
|
65 |
try: |
|
66 |
encoding = vreg.property_value('ui.encoding') |
|
67 |
except: # no vreg or property not registered |
|
68 |
encoding = 'utf-8' |
|
69 |
self.encoding = encoding |
|
70 |
# cache result of execution for (rql expr / eids), |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
71 |
# should be emptied on commit/rollback of the server session / web |
0 | 72 |
# connection |
73 |
self.local_perm_cache = {} |
|
74 |
||
75 |
def property_value(self, key): |
|
76 |
if self.user: |
|
77 |
return self.user.property_value(key) |
|
78 |
return self.vreg.property_value(key) |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
79 |
|
0 | 80 |
def etype_rset(self, etype, size=1): |
81 |
"""return a fake result set for a particular entity type""" |
|
82 |
from cubicweb.rset import ResultSet |
|
83 |
rset = ResultSet([('A',)]*size, '%s X' % etype, |
|
84 |
description=[(etype,)]*size) |
|
1132 | 85 |
def get_entity(row, col=0, etype=etype, req=self, rset=rset): |
86 |
return req.vreg.etype_class(etype)(req, rset, row, col) |
|
0 | 87 |
rset.get_entity = get_entity |
88 |
return self.decorate_rset(rset) |
|
89 |
||
90 |
def eid_rset(self, eid, etype=None): |
|
91 |
"""return a result set for the given eid without doing actual query |
|
92 |
(we have the eid, we can suppose it exists and user has access to the |
|
93 |
entity) |
|
94 |
""" |
|
95 |
from cubicweb.rset import ResultSet |
|
96 |
eid = typed_eid(eid) |
|
97 |
if etype is None: |
|
98 |
etype = self.describe(eid)[0] |
|
99 |
rset = ResultSet([(eid,)], 'Any X WHERE X eid %(x)s', {'x': eid}, |
|
100 |
[(etype,)]) |
|
101 |
return self.decorate_rset(rset) |
|
102 |
||
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
103 |
def empty_rset(self): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
104 |
"""return a result set for the given eid without doing actual query |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
105 |
(we have the eid, we can suppose it exists and user has access to the |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
106 |
entity) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
107 |
""" |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
108 |
from cubicweb.rset import ResultSet |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
109 |
return self.decorate_rset(ResultSet([], 'Any X WHERE X eid -1')) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
110 |
|
0 | 111 |
def entity_from_eid(self, eid, etype=None): |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
112 |
try: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
113 |
return self.entity_cache(eid) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
114 |
except KeyError: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
115 |
rset = self.eid_rset(eid, etype) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
116 |
entity = rset.get_entity(0, 0) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
117 |
self.set_entity_cache(entity) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
118 |
return entity |
0 | 119 |
|
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
120 |
def entity_cache(self, eid): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
121 |
raise KeyError |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
122 |
def set_entity_cache(self, entity): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
123 |
pass |
0 | 124 |
# url generation methods ################################################## |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
125 |
|
2059
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
126 |
def build_url(self, *args, **kwargs): |
0 | 127 |
"""return an absolute URL using params dictionary key/values as URL |
128 |
parameters. Values are automatically URL quoted, and the |
|
129 |
publishing method to use may be specified or will be guessed. |
|
130 |
""" |
|
2059
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
131 |
# use *args since we don't want first argument to be "anonymous" to |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
132 |
# avoid potential clash with kwargs |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
133 |
assert len(args) == 1, 'only 0 or 1 non-named-argument expected' |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
134 |
method = args[0] |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
135 |
base_url = kwargs.pop('base_url', None) |
0 | 136 |
if base_url is None: |
137 |
base_url = self.base_url() |
|
138 |
if '_restpath' in kwargs: |
|
139 |
assert method == 'view', method |
|
140 |
path = kwargs.pop('_restpath') |
|
141 |
else: |
|
142 |
path = method |
|
143 |
if not kwargs: |
|
144 |
return u'%s%s' % (base_url, path) |
|
145 |
return u'%s%s?%s' % (base_url, path, self.build_url_params(**kwargs)) |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
146 |
|
0 | 147 |
|
148 |
def build_url_params(self, **kwargs): |
|
149 |
"""return encoded params to incorporate them in an URL""" |
|
150 |
args = [] |
|
151 |
for param, values in kwargs.items(): |
|
152 |
if not isinstance(values, (list, tuple)): |
|
153 |
values = (values,) |
|
154 |
for value in values: |
|
155 |
args.append(u'%s=%s' % (param, self.url_quote(value))) |
|
156 |
return '&'.join(args) |
|
157 |
||
158 |
def url_quote(self, value, safe=''): |
|
159 |
"""urllib.quote is not unicode safe, use this method to do the |
|
160 |
necessary encoding / decoding. Also it's designed to quote each |
|
161 |
part of a url path and so the '/' character will be encoded as well. |
|
162 |
""" |
|
163 |
if isinstance(value, unicode): |
|
164 |
quoted = urlquote(value.encode(self.encoding), safe=safe) |
|
165 |
return unicode(quoted, self.encoding) |
|
166 |
return urlquote(str(value), safe=safe) |
|
167 |
||
168 |
def url_unquote(self, quoted): |
|
169 |
"""returns a unicode unquoted string |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
170 |
|
0 | 171 |
decoding is based on `self.encoding` which is the encoding |
172 |
used in `url_quote` |
|
173 |
""" |
|
174 |
if isinstance(quoted, unicode): |
|
175 |
quoted = quoted.encode(self.encoding) |
|
176 |
try: |
|
177 |
return unicode(urlunquote(quoted), self.encoding) |
|
178 |
except UnicodeDecodeError: # might occurs on manually typed URLs |
|
179 |
return unicode(urlunquote(quoted), 'iso-8859-1') |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
180 |
|
0 | 181 |
|
182 |
# session's user related methods ##################################### |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
183 |
|
0 | 184 |
@cached |
185 |
def user_data(self): |
|
186 |
"""returns a dictionnary with this user's information""" |
|
187 |
userinfo = {} |
|
188 |
if self.is_internal_session: |
|
189 |
userinfo['login'] = "cubicweb" |
|
190 |
userinfo['name'] = "cubicweb" |
|
191 |
userinfo['email'] = "" |
|
192 |
return userinfo |
|
193 |
user = self.actual_session().user |
|
194 |
rql = "Any F,S,A where U eid %(x)s, U firstname F, U surname S, U primary_email E, E address A" |
|
195 |
try: |
|
196 |
firstname, lastname, email = self.execute(rql, {'x': user.eid}, 'x')[0] |
|
197 |
if firstname is None and lastname is None: |
|
198 |
userinfo['name'] = '' |
|
199 |
else: |
|
200 |
userinfo['name'] = ("%s %s" % (firstname, lastname)) |
|
201 |
userinfo['email'] = email |
|
202 |
except IndexError: |
|
203 |
userinfo['name'] = None |
|
204 |
userinfo['email'] = None |
|
205 |
userinfo['login'] = user.login |
|
206 |
return userinfo |
|
207 |
||
208 |
def is_internal_session(self): |
|
209 |
"""overrided on the server-side""" |
|
210 |
return False |
|
211 |
||
212 |
# abstract methods to override according to the web front-end ############# |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
213 |
|
0 | 214 |
def base_url(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
215 |
"""return the root url of the instance""" |
0 | 216 |
raise NotImplementedError |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
217 |
|
0 | 218 |
def decorate_rset(self, rset): |
219 |
"""add vreg/req (at least) attributes to the given result set """ |
|
220 |
raise NotImplementedError |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
221 |
|
0 | 222 |
def describe(self, eid): |
223 |
"""return a tuple (type, sourceuri, extid) for the entity with id <eid>""" |
|
224 |
raise NotImplementedError |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
225 |
|
0 | 226 |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
227 |
# XXX 2.45 is allowing nicer entity type names, use this map for bw compat |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
228 |
ETYPE_NAME_MAP = {# 3.2 migration |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
229 |
'ECache': 'CWCache', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
230 |
'EUser': 'CWUser', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
231 |
'EGroup': 'CWGroup', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
232 |
'EProperty': 'CWProperty', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
233 |
'EFRDef': 'CWAttribute', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
234 |
'ENFRDef': 'CWRelation', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
235 |
'ERType': 'CWRType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
236 |
'EEType': 'CWEType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
237 |
'EConstraintType': 'CWConstraintType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
238 |
'EConstraint': 'CWConstraint', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
239 |
'EPermission': 'CWPermission', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
240 |
# 2.45 migration |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
241 |
'Eetype': 'CWEType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
242 |
'Ertype': 'CWRType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
243 |
'Efrdef': 'CWAttribute', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
244 |
'Enfrdef': 'CWRelation', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
245 |
'Econstraint': 'CWConstraint', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
246 |
'Econstrainttype': 'CWConstraintType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
247 |
'Epermission': 'CWPermission', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
248 |
'Egroup': 'CWGroup', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
249 |
'Euser': 'CWUser', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
250 |
'Eproperty': 'CWProperty', |
0 | 251 |
'Emailaddress': 'EmailAddress', |
252 |
'Rqlexpression': 'RQLExpression', |
|
253 |
'Trinfo': 'TrInfo', |
|
254 |
} |
|
255 |
||
256 |
||
257 |
||
258 |
# XXX cubic web cube migration map |
|
259 |
CW_MIGRATION_MAP = {'erudi': 'cubicweb', |
|
260 |
||
261 |
'eaddressbook': 'addressbook', |
|
262 |
'ebasket': 'basket', |
|
263 |
'eblog': 'blog', |
|
264 |
'ebook': 'book', |
|
265 |
'ecomment': 'comment', |
|
266 |
'ecompany': 'company', |
|
267 |
'econference': 'conference', |
|
268 |
'eemail': 'email', |
|
269 |
'eevent': 'event', |
|
270 |
'eexpense': 'expense', |
|
271 |
'efile': 'file', |
|
272 |
'einvoice': 'invoice', |
|
273 |
'elink': 'link', |
|
274 |
'emailinglist': 'mailinglist', |
|
275 |
'eperson': 'person', |
|
276 |
'eshopcart': 'shopcart', |
|
277 |
'eskillmat': 'skillmat', |
|
278 |
'etask': 'task', |
|
279 |
'eworkcase': 'workcase', |
|
280 |
'eworkorder': 'workorder', |
|
281 |
'ezone': 'zone', |
|
282 |
'i18ncontent': 'i18ncontent', |
|
283 |
'svnfile': 'vcsfile', |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
284 |
|
0 | 285 |
'eclassschemes': 'keyword', |
286 |
'eclassfolders': 'folder', |
|
287 |
'eclasstags': 'tag', |
|
288 |
||
289 |
'jpl': 'jpl', |
|
290 |
'jplintra': 'jplintra', |
|
291 |
'jplextra': 'jplextra', |
|
292 |
'jplorg': 'jplorg', |
|
293 |
'jplrecia': 'jplrecia', |
|
294 |
'crm': 'crm', |
|
295 |
'agueol': 'agueol', |
|
296 |
'docaster': 'docaster', |
|
297 |
'asteretud': 'asteretud', |
|
298 |
} |
|
231
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
299 |
|
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
300 |
def neg_role(role): |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
301 |
if role == 'subject': |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
302 |
return 'object' |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
303 |
return 'subject' |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
304 |
|
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
305 |
def role(obj): |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
306 |
try: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
307 |
return obj.role |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
308 |
except AttributeError: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
309 |
return neg_role(obj.target) |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
310 |
|
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
311 |
def target(obj): |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
312 |
try: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
313 |
return obj.target |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
314 |
except AttributeError: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
315 |
return neg_role(obj.role) |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
316 |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
317 |
def underline_title(title, car='-'): |
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
318 |
return title+'\n'+(car*len(title)) |
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
319 |
|
2683
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
320 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
321 |
class CubicWebEventManager(object): |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
322 |
"""simple event / callback manager. |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
323 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
324 |
Typical usage to register a callback:: |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
325 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
326 |
>>> from cubicweb import CW_EVENT_MANAGER |
2705
30bcdbd92820
[events] renamed source-reload into registry-reload to avoid potential confusions with datasources
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2689
diff
changeset
|
327 |
>>> CW_EVENT_MANAGER.bind('after-registry-reload', mycallback) |
2683
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
328 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
329 |
Typical usage to emit an event:: |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
330 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
331 |
>>> from cubicweb import CW_EVENT_MANAGER |
2705
30bcdbd92820
[events] renamed source-reload into registry-reload to avoid potential confusions with datasources
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2689
diff
changeset
|
332 |
>>> CW_EVENT_MANAGER.emit('after-registry-reload') |
2683
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
333 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
334 |
emit() accepts an additional context parameter that will be passed |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
335 |
to the callback if specified (and only in that case) |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
336 |
""" |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
337 |
def __init__(self): |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
338 |
self.callbacks = {} |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
339 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
340 |
def bind(self, event, callback, *args, **kwargs): |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
341 |
self.callbacks.setdefault(event, []).append( (callback, args, kwargs) ) |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
342 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
343 |
def emit(self, event, context=None): |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
344 |
for callback, args, kwargs in self.callbacks.get(event, ()): |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
345 |
if context is None: |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
346 |
callback(*args, **kwargs) |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
347 |
else: |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
348 |
callback(context, *args, **kwargs) |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
349 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
350 |
CW_EVENT_MANAGER = CubicWebEventManager() |
2689
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
351 |
|
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
352 |
def onevent(event): |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
353 |
"""decorator to ease event / callback binding |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
354 |
|
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
355 |
>>> from cubicweb import onevent |
2705
30bcdbd92820
[events] renamed source-reload into registry-reload to avoid potential confusions with datasources
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2689
diff
changeset
|
356 |
>>> @onevent('before-registry-reload') |
2689
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
357 |
... def mycallback(): |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
358 |
... print 'hello' |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
359 |
... |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
360 |
>>> |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
361 |
""" |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
362 |
def _decorator(func): |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
363 |
CW_EVENT_MANAGER.bind(event, func) |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
364 |
return func |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
365 |
return _decorator |