author | sylvain.thenault@logilab.fr |
Thu, 09 Apr 2009 19:58:37 +0200 | |
branch | tls-sprint |
changeset 1330 | 92343a468e2a |
parent 1309 | a4eb20f86cb0 |
child 1357 | e5a97779c7fc |
permissions | -rw-r--r-- |
0 | 1 |
"""extend the generic VRegistry with some cubicweb specific stuff |
2 |
||
3 |
:organization: Logilab |
|
1123
a8e2838f174a
catch UnknownEid and set empty solutions on select nodes
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
9 |
from logilab.common.decorators import cached, clear_cache |
|
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
10 |
from logilab.common.interface import extend |
0 | 11 |
|
12 |
from rql import RQLHelper |
|
13 |
||
1123
a8e2838f174a
catch UnknownEid and set empty solutions on select nodes
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
14 |
from cubicweb import Binary, UnknownProperty, UnknownEid |
0 | 15 |
from cubicweb.vregistry import VRegistry, ObjectNotFound, NoSelectableObject |
16 |
||
17 |
_ = unicode |
|
18 |
||
776 | 19 |
def use_interfaces(obj): |
1132 | 20 |
"""return interfaces used by the given object by searchinf for implements |
21 |
selectors, with a bw compat fallback to accepts_interfaces attribute |
|
22 |
""" |
|
776 | 23 |
from cubicweb.selectors import implements |
24 |
try: |
|
25 |
# XXX deprecated |
|
26 |
return sorted(obj.accepts_interfaces) |
|
27 |
except AttributeError: |
|
28 |
try: |
|
29 |
impl = obj.__select__.search_selector(implements) |
|
30 |
if impl: |
|
31 |
return sorted(impl.expected_ifaces) |
|
32 |
except AttributeError: |
|
33 |
pass # old-style vobject classes with no accepts_interfaces |
|
1044
3672a7c86784
print message to help debugging selector on error
sylvain.thenault@logilab.fr
parents:
1037
diff
changeset
|
34 |
except: |
3672a7c86784
print message to help debugging selector on error
sylvain.thenault@logilab.fr
parents:
1037
diff
changeset
|
35 |
print 'bad selector %s on %s' % (obj.__select__, obj) |
3672a7c86784
print message to help debugging selector on error
sylvain.thenault@logilab.fr
parents:
1037
diff
changeset
|
36 |
raise |
776 | 37 |
return () |
0 | 38 |
|
39 |
||
40 |
class CubicWebRegistry(VRegistry): |
|
41 |
"""extend the generic VRegistry with some cubicweb specific stuff""" |
|
42 |
||
169
0e031b66cb0b
don't systematically init_log, it may breaks client log configuration
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
43 |
def __init__(self, config, debug=None, initlog=True): |
0e031b66cb0b
don't systematically init_log, it may breaks client log configuration
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
44 |
if initlog: |
0e031b66cb0b
don't systematically init_log, it may breaks client log configuration
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
45 |
# first init log service |
0e031b66cb0b
don't systematically init_log, it may breaks client log configuration
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
46 |
config.init_log(debug=debug) |
0 | 47 |
super(CubicWebRegistry, self).__init__(config) |
48 |
self.schema = None |
|
49 |
self.reset() |
|
50 |
self.initialized = False |
|
51 |
||
52 |
def items(self): |
|
53 |
return [item for item in self._registries.items() |
|
54 |
if not item[0] in ('propertydefs', 'propertyvalues')] |
|
55 |
||
56 |
def values(self): |
|
1132 | 57 |
return [value for key, value in self._registries.items() |
0 | 58 |
if not key in ('propertydefs', 'propertyvalues')] |
59 |
||
60 |
def reset(self): |
|
61 |
self._registries = {} |
|
62 |
self._lastmodifs = {} |
|
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
63 |
self._needs_iface = {} |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
64 |
# two special registries, propertydefs which care all the property |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
65 |
# definitions, and propertyvals which contains values for those |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
66 |
# properties |
0 | 67 |
self._registries['propertydefs'] = {} |
68 |
self._registries['propertyvalues'] = self.eprop_values = {} |
|
69 |
for key, propdef in self.config.eproperty_definitions(): |
|
70 |
self.register_property(key, **propdef) |
|
71 |
||
72 |
def set_schema(self, schema): |
|
73 |
"""set application'schema and load application objects""" |
|
74 |
self.schema = schema |
|
75 |
clear_cache(self, 'rqlhelper') |
|
76 |
# now we can load application's web objects |
|
77 |
self.register_objects(self.config.vregistry_path()) |
|
78 |
||
79 |
def update_schema(self, schema): |
|
80 |
"""update .schema attribute on registered objects, necessary for some |
|
81 |
tests |
|
82 |
""" |
|
83 |
self.schema = schema |
|
84 |
for registry, regcontent in self._registries.items(): |
|
85 |
if registry in ('propertydefs', 'propertyvalues'): |
|
86 |
continue |
|
87 |
for objects in regcontent.values(): |
|
88 |
for obj in objects: |
|
89 |
obj.schema = schema |
|
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
90 |
|
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
91 |
def register_if_interface_found(self, obj, ifaces, **kwargs): |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
92 |
"""register an object but remove it if no entity class implements one of |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
93 |
the given interfaces |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
94 |
""" |
785 | 95 |
self.register(obj, **kwargs) |
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
96 |
if not isinstance(ifaces, (tuple, list)): |
785 | 97 |
self._needs_iface[obj] = (ifaces,) |
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
98 |
else: |
785 | 99 |
self._needs_iface[obj] = ifaces |
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
100 |
|
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
101 |
def register(self, obj, **kwargs): |
717 | 102 |
if kwargs.get('registryname', obj.__registry__) == 'etypes': |
1037
1f3fae8d82b2
don't fail if vobjects reference an unexistant class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
103 |
if obj.id != 'Any' and not obj.id in self.schema: |
1132 | 104 |
self.error('don\'t register %s, %s type not defined in the ' |
105 |
'schema', obj, obj.id) |
|
1037
1f3fae8d82b2
don't fail if vobjects reference an unexistant class
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
106 |
return |
717 | 107 |
kwargs['clear'] = True |
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
108 |
super(CubicWebRegistry, self).register(obj, **kwargs) |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
109 |
# XXX bw compat |
777
39695e98ba35
test and fix interface based objects cleaning
sylvain.thenault@logilab.fr
parents:
776
diff
changeset
|
110 |
ifaces = use_interfaces(obj) |
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
111 |
if ifaces: |
785 | 112 |
self._needs_iface[obj] = ifaces |
0 | 113 |
|
114 |
def register_objects(self, path, force_reload=None): |
|
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
115 |
"""overriden to remove objects requiring a missing interface""" |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
116 |
if super(CubicWebRegistry, self).register_objects(path, force_reload): |
0 | 117 |
# clear etype cache if you don't want to run into deep weirdness |
118 |
clear_cache(self, 'etype_class') |
|
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
119 |
# we may want to keep interface dependent objects (e.g.for i18n |
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
120 |
# catalog generation) |
0 | 121 |
if not self.config.cleanup_interface_sobjects: |
122 |
return |
|
123 |
# remove vobjects that don't support any available interface |
|
1145 | 124 |
implemented_interfaces = set() |
0 | 125 |
for classes in self.get('etypes', {}).values(): |
126 |
for cls in classes: |
|
777
39695e98ba35
test and fix interface based objects cleaning
sylvain.thenault@logilab.fr
parents:
776
diff
changeset
|
127 |
for iface in cls.__implements__: |
1145 | 128 |
implemented_interfaces.update(iface.__mro__) |
129 |
implemented_interfaces.update(cls.__mro__) |
|
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
130 |
for obj, ifaces in self._needs_iface.items(): |
1132 | 131 |
ifaces = frozenset(isinstance(iface, basestring) |
132 |
and iface in self.schema |
|
133 |
and self.etype_class(iface) |
|
134 |
or iface |
|
785 | 135 |
for iface in ifaces) |
1146
547681592765
don't kick objects accepting 'Any'
sylvain.thenault@logilab.fr
parents:
1145
diff
changeset
|
136 |
if not ('Any' in ifaces or ifaces & implemented_interfaces): |
547681592765
don't kick objects accepting 'Any'
sylvain.thenault@logilab.fr
parents:
1145
diff
changeset
|
137 |
self.debug('kicking vobject %s (no implemented interface ' |
547681592765
don't kick objects accepting 'Any'
sylvain.thenault@logilab.fr
parents:
1145
diff
changeset
|
138 |
'among %s)', obj, ifaces) |
666
8ad9885ea45a
interface handling should be done here
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
139 |
self.unregister(obj) |
1176
0ff3d29e91c9
we have to clear _needs_iface to avoir crash on auto-reload
sylvain.thenault@logilab.fr
parents:
1146
diff
changeset
|
140 |
# clear needs_iface so we don't try to remove some not-anymore-in |
0ff3d29e91c9
we have to clear _needs_iface to avoir crash on auto-reload
sylvain.thenault@logilab.fr
parents:
1146
diff
changeset
|
141 |
# objects on automatic reloading |
0ff3d29e91c9
we have to clear _needs_iface to avoir crash on auto-reload
sylvain.thenault@logilab.fr
parents:
1146
diff
changeset
|
142 |
self._needs_iface.clear() |
1282
272d8ec6f308
* print vreg content once fully initialized (require move of print code from vregistry to cwvreg)
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
143 |
# print registry content |
272d8ec6f308
* print vreg content once fully initialized (require move of print code from vregistry to cwvreg)
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
144 |
for registry, objects in self.items(): |
272d8ec6f308
* print vreg content once fully initialized (require move of print code from vregistry to cwvreg)
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
145 |
self.debug('available in registry %s: %s', registry, |
272d8ec6f308
* print vreg content once fully initialized (require move of print code from vregistry to cwvreg)
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
146 |
sorted(objects)) |
272d8ec6f308
* print vreg content once fully initialized (require move of print code from vregistry to cwvreg)
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
147 |
for appobjects in objects.itervalues(): |
272d8ec6f308
* print vreg content once fully initialized (require move of print code from vregistry to cwvreg)
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
148 |
for appobject in appobjects: |
272d8ec6f308
* print vreg content once fully initialized (require move of print code from vregistry to cwvreg)
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
149 |
appobject.vreg_initialization_completed() |
0 | 150 |
|
151 |
@cached |
|
152 |
def etype_class(self, etype): |
|
153 |
"""return an entity class for the given entity type. |
|
154 |
Try to find out a specific class for this kind of entity or |
|
155 |
default to a dump of the class registered for 'Any' |
|
156 |
""" |
|
157 |
etype = str(etype) |
|
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
158 |
if etype == 'Any': |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
159 |
return self.select(self.registry_objects('etypes', 'Any'), 'Any') |
0 | 160 |
eschema = self.schema.eschema(etype) |
161 |
baseschemas = [eschema] + eschema.ancestors() |
|
162 |
# browse ancestors from most specific to most generic and |
|
163 |
# try to find an associated custom entity class |
|
164 |
for baseschema in baseschemas: |
|
165 |
btype = str(baseschema) |
|
166 |
try: |
|
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
167 |
cls = self.select(self.registry_objects('etypes', btype), etype) |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
168 |
break |
0 | 169 |
except ObjectNotFound: |
170 |
pass |
|
631
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
171 |
else: |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
172 |
# no entity class for any of the ancestors, fallback to the default |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
173 |
# one |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
174 |
cls = self.select(self.registry_objects('etypes', 'Any'), etype) |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
175 |
# add class itself to the list of implemented interfaces, as well as the |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
176 |
# Any entity class so we can select according to class using the |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
177 |
# `implements` selector |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
178 |
extend(cls, cls) |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
179 |
extend(cls, self.etype_class('Any')) |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
180 |
return cls |
99f5852f8604
major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
181 |
|
0 | 182 |
def render(self, registry, oid, req, **context): |
183 |
"""select an object in a given registry and render it |
|
184 |
||
185 |
- registry: the registry's name |
|
186 |
- oid : the view to call |
|
187 |
- req : the HTTP request |
|
188 |
""" |
|
189 |
objclss = self.registry_objects(registry, oid) |
|
190 |
try: |
|
191 |
rset = context.pop('rset') |
|
192 |
except KeyError: |
|
193 |
rset = None |
|
194 |
selected = self.select(objclss, req, rset, **context) |
|
195 |
return selected.dispatch(**context) |
|
196 |
||
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
816
diff
changeset
|
197 |
def main_template(self, req, oid='main-template', **context): |
0 | 198 |
"""display query by calling the given template (default to main), |
199 |
and returning the output as a string instead of requiring the [w]rite |
|
200 |
method as argument |
|
201 |
""" |
|
816
9cd49a910fce
kill Template class and 'templates' registry
sylvain.thenault@logilab.fr
parents:
785
diff
changeset
|
202 |
res = self.render('views', oid, req, **context) |
0 | 203 |
if isinstance(res, unicode): |
204 |
return res.encode(req.encoding) |
|
205 |
assert isinstance(res, str) |
|
206 |
return res |
|
207 |
||
208 |
def possible_vobjects(self, registry, *args, **kwargs): |
|
209 |
"""return an ordered list of possible app objects in a given registry, |
|
210 |
supposing they support the 'visible' and 'order' properties (as most |
|
211 |
visualizable objects) |
|
212 |
""" |
|
213 |
return [x for x in sorted(self.possible_objects(registry, *args, **kwargs), |
|
214 |
key=lambda x: x.propval('order')) |
|
213
6842c3dee34b
adding files (formely appearing in jpl) specific to cubicweb
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
169
diff
changeset
|
215 |
if x.propval('visible')] |
0 | 216 |
|
217 |
def possible_actions(self, req, rset, **kwargs): |
|
218 |
if rset is None: |
|
219 |
actions = self.possible_vobjects('actions', req, rset) |
|
220 |
else: |
|
221 |
actions = rset.possible_actions() # cached implementation |
|
222 |
result = {} |
|
223 |
for action in actions: |
|
224 |
result.setdefault(action.category, []).append(action) |
|
225 |
return result |
|
226 |
||
227 |
def possible_views(self, req, rset, **kwargs): |
|
228 |
"""return an iterator on possible views for this result set |
|
229 |
||
230 |
views returned are classes, not instances |
|
231 |
""" |
|
232 |
for vid, views in self.registry('views').items(): |
|
233 |
if vid[0] == '_': |
|
234 |
continue |
|
235 |
try: |
|
236 |
view = self.select(views, req, rset, **kwargs) |
|
237 |
if view.linkable(): |
|
238 |
yield view |
|
239 |
except NoSelectableObject: |
|
240 |
continue |
|
395
cce260264122
catch Exception in case there is some unexepected selector bug
sylvain.thenault@logilab.fr
parents:
355
diff
changeset
|
241 |
except Exception: |
cce260264122
catch Exception in case there is some unexepected selector bug
sylvain.thenault@logilab.fr
parents:
355
diff
changeset
|
242 |
self.exception('error while trying to list possible %s views for %s', |
cce260264122
catch Exception in case there is some unexepected selector bug
sylvain.thenault@logilab.fr
parents:
355
diff
changeset
|
243 |
vid, rset) |
cce260264122
catch Exception in case there is some unexepected selector bug
sylvain.thenault@logilab.fr
parents:
355
diff
changeset
|
244 |
|
0 | 245 |
def select_box(self, oid, *args, **kwargs): |
246 |
"""return the most specific view according to the result set""" |
|
247 |
try: |
|
248 |
return self.select_object('boxes', oid, *args, **kwargs) |
|
249 |
except NoSelectableObject: |
|
250 |
return |
|
251 |
||
252 |
def select_action(self, oid, *args, **kwargs): |
|
253 |
"""return the most specific view according to the result set""" |
|
254 |
try: |
|
255 |
return self.select_object('actions', oid, *args, **kwargs) |
|
256 |
except NoSelectableObject: |
|
257 |
return |
|
258 |
||
259 |
def select_component(self, cid, *args, **kwargs): |
|
260 |
"""return the most specific component according to the result set""" |
|
261 |
try: |
|
262 |
return self.select_object('components', cid, *args, **kwargs) |
|
263 |
except (NoSelectableObject, ObjectNotFound): |
|
264 |
return |
|
265 |
||
266 |
def select_view(self, __vid, req, rset, **kwargs): |
|
267 |
"""return the most specific view according to the result set""" |
|
268 |
views = self.registry_objects('views', __vid) |
|
269 |
return self.select(views, req, rset, **kwargs) |
|
270 |
||
271 |
||
272 |
# properties handling ##################################################### |
|
273 |
||
274 |
def user_property_keys(self, withsitewide=False): |
|
275 |
if withsitewide: |
|
276 |
return sorted(self['propertydefs']) |
|
277 |
return sorted(k for k, kd in self['propertydefs'].iteritems() |
|
278 |
if not kd['sitewide']) |
|
279 |
||
280 |
def register_property(self, key, type, help, default=None, vocabulary=None, |
|
281 |
sitewide=False): |
|
282 |
"""register a given property""" |
|
283 |
properties = self._registries['propertydefs'] |
|
284 |
assert type in YAMS_TO_PY |
|
285 |
properties[key] = {'type': type, 'vocabulary': vocabulary, |
|
286 |
'default': default, 'help': help, |
|
287 |
'sitewide': sitewide} |
|
288 |
||
289 |
def property_info(self, key): |
|
290 |
"""return dictionary containing description associated to the given |
|
291 |
property key (including type, defaut value, help and a site wide |
|
292 |
boolean) |
|
293 |
""" |
|
294 |
try: |
|
295 |
return self._registries['propertydefs'][key] |
|
296 |
except KeyError: |
|
297 |
if key.startswith('system.version.'): |
|
298 |
soft = key.split('.')[-1] |
|
299 |
return {'type': 'String', 'sitewide': True, |
|
300 |
'default': None, 'vocabulary': None, |
|
301 |
'help': _('%s software version of the database') % soft} |
|
302 |
raise UnknownProperty('unregistered property %r' % key) |
|
303 |
||
304 |
def property_value(self, key): |
|
305 |
try: |
|
306 |
return self._registries['propertyvalues'][key] |
|
307 |
except KeyError: |
|
308 |
return self._registries['propertydefs'][key]['default'] |
|
309 |
||
310 |
def typed_value(self, key, value): |
|
311 |
"""value is an unicode string, return it correctly typed. Let potential |
|
312 |
type error propagates. |
|
313 |
""" |
|
314 |
pdef = self.property_info(key) |
|
315 |
try: |
|
316 |
value = YAMS_TO_PY[pdef['type']](value) |
|
317 |
except (TypeError, ValueError): |
|
318 |
raise ValueError(_('bad value')) |
|
319 |
vocab = pdef['vocabulary'] |
|
320 |
if vocab is not None: |
|
321 |
if callable(vocab): |
|
322 |
vocab = vocab(key, None) # XXX need a req object |
|
323 |
if not value in vocab: |
|
324 |
raise ValueError(_('unauthorized value')) |
|
325 |
return value |
|
326 |
||
327 |
def init_properties(self, propvalues): |
|
328 |
"""init the property values registry using the given set of couple (key, value) |
|
329 |
""" |
|
330 |
self.initialized = True |
|
331 |
values = self._registries['propertyvalues'] |
|
332 |
for key, val in propvalues: |
|
333 |
try: |
|
334 |
values[key] = self.typed_value(key, val) |
|
335 |
except ValueError: |
|
336 |
self.warning('%s (you should probably delete that property ' |
|
337 |
'from the database)', ex) |
|
338 |
except UnknownProperty, ex: |
|
339 |
self.warning('%s (you should probably delete that property ' |
|
340 |
'from the database)', ex) |
|
341 |
||
342 |
def parse(self, session, rql, args=None): |
|
343 |
rqlst = self.rqlhelper.parse(rql) |
|
344 |
def type_from_eid(eid, session=session): |
|
345 |
return session.describe(eid)[0] |
|
1123
a8e2838f174a
catch UnknownEid and set empty solutions on select nodes
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
346 |
try: |
a8e2838f174a
catch UnknownEid and set empty solutions on select nodes
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
347 |
self.rqlhelper.compute_solutions(rqlst, {'eid': type_from_eid}, args) |
a8e2838f174a
catch UnknownEid and set empty solutions on select nodes
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
348 |
except UnknownEid: |
a8e2838f174a
catch UnknownEid and set empty solutions on select nodes
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
349 |
for select in rqlst.children: |
a8e2838f174a
catch UnknownEid and set empty solutions on select nodes
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
350 |
select.solutions = [] |
0 | 351 |
return rqlst |
352 |
||
353 |
@property |
|
354 |
@cached |
|
355 |
def rqlhelper(self): |
|
356 |
return RQLHelper(self.schema, |
|
357 |
special_relations={'eid': 'uid', 'has_text': 'fti'}) |
|
358 |
||
169
0e031b66cb0b
don't systematically init_log, it may breaks client log configuration
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
359 |
|
0 | 360 |
class MulCnxCubicWebRegistry(CubicWebRegistry): |
361 |
"""special registry to be used when an application has to deal with |
|
362 |
connections to differents repository. This class add some additional wrapper |
|
363 |
trying to hide buggy class attributes since classes are not designed to be |
|
364 |
shared. |
|
365 |
""" |
|
366 |
def etype_class(self, etype): |
|
367 |
"""return an entity class for the given entity type. |
|
368 |
Try to find out a specific class for this kind of entity or |
|
369 |
default to a dump of the class registered for 'Any' |
|
370 |
""" |
|
371 |
usercls = super(MulCnxCubicWebRegistry, self).etype_class(etype) |
|
372 |
usercls.e_schema = self.schema.eschema(etype) |
|
373 |
return usercls |
|
374 |
||
375 |
def select(self, vobjects, *args, **kwargs): |
|
376 |
"""return an instance of the most specific object according |
|
377 |
to parameters |
|
378 |
||
379 |
raise NoSelectableObject if not object apply |
|
380 |
""" |
|
381 |
for vobject in vobjects: |
|
382 |
vobject.vreg = self |
|
383 |
vobject.schema = self.schema |
|
384 |
vobject.config = self.config |
|
385 |
return super(MulCnxCubicWebRegistry, self).select(vobjects, *args, **kwargs) |
|
386 |
||
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
387 |
from datetime import datetime, date, time, timedelta |
0 | 388 |
|
389 |
YAMS_TO_PY = { |
|
390 |
'Boolean': bool, |
|
391 |
'String' : unicode, |
|
392 |
'Password': str, |
|
393 |
'Bytes': Binary, |
|
394 |
'Int': int, |
|
395 |
'Float': float, |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
396 |
'Date': date, |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
397 |
'Datetime': datetime, |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
398 |
'Time': time, |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
399 |
'Interval': timedelta, |
0 | 400 |
} |
401 |