author | Sylvain <syt@logilab.fr> |
Thu, 14 May 2009 14:51:52 +0200 | |
changeset 1819 | 7d3e8adcd559 |
parent 1769 | fb91d2b8a441 |
child 1917 | eaf6e0edc509 |
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" |
|
1769
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
8 |
_ = unicode |
0 | 9 |
|
10 |
from logilab.common.decorators import cached, clear_cache |
|
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 |
1769
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
16 |
from cubicweb.rtags import RTAGS |
0 | 17 |
|
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 |
|
1475
5c1ec97f317e
should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents:
1357
diff
changeset
|
26 |
return sorted(obj.accepts_interfaces) |
776 | 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""" |
|
1475
5c1ec97f317e
should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents:
1357
diff
changeset
|
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 |
|
1475
5c1ec97f317e
should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents:
1357
diff
changeset
|
51 |
|
0 | 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')] |
1475
5c1ec97f317e
should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents:
1357
diff
changeset
|
59 |
|
0 | 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) |
|
1475
5c1ec97f317e
should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents:
1357
diff
changeset
|
71 |
|
0 | 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()) |
|
1475
5c1ec97f317e
should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents:
1357
diff
changeset
|
78 |
|
0 | 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) |
|
0 | 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 |
1475
5c1ec97f317e
should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents:
1357
diff
changeset
|
113 |
|
0 | 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): |
1638 | 117 |
self.initialization_completed() |
1752
4b0b912ff5b7
fix rtags initialization: do it at the registry level to avoid multiple initialization of the same rtag
sylvain.thenault@logilab.fr
parents:
1723
diff
changeset
|
118 |
# call vreg_initialization_completed on appobjects and print |
4b0b912ff5b7
fix rtags initialization: do it at the registry level to avoid multiple initialization of the same rtag
sylvain.thenault@logilab.fr
parents:
1723
diff
changeset
|
119 |
# registry content |
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
|
120 |
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
|
121 |
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
|
122 |
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
|
123 |
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
|
124 |
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
|
125 |
appobject.vreg_initialization_completed() |
1769
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
126 |
# don't check rtags if we don't want to cleanup_interface_sobjects |
1752
4b0b912ff5b7
fix rtags initialization: do it at the registry level to avoid multiple initialization of the same rtag
sylvain.thenault@logilab.fr
parents:
1723
diff
changeset
|
127 |
for rtag in RTAGS: |
1769
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
128 |
rtag.init(self.schema, |
fb91d2b8a441
fix some rtags pb on i18n catalog generation
sylvain.thenault@logilab.fr
parents:
1752
diff
changeset
|
129 |
check=self.config.cleanup_interface_sobjects) |
0 | 130 |
|
1638 | 131 |
def initialization_completed(self): |
132 |
# clear etype cache if you don't want to run into deep weirdness |
|
133 |
clear_cache(self, 'etype_class') |
|
134 |
# we may want to keep interface dependent objects (e.g.for i18n |
|
135 |
# catalog generation) |
|
136 |
if self.config.cleanup_interface_sobjects: |
|
137 |
# remove vobjects that don't support any available interface |
|
138 |
implemented_interfaces = set() |
|
139 |
if 'Any' in self.get('etypes', ()): |
|
140 |
for etype in self.schema.entities(): |
|
141 |
cls = self.etype_class(etype) |
|
142 |
for iface in cls.__implements__: |
|
143 |
implemented_interfaces.update(iface.__mro__) |
|
144 |
implemented_interfaces.update(cls.__mro__) |
|
145 |
for obj, ifaces in self._needs_iface.items(): |
|
146 |
ifaces = frozenset(isinstance(iface, basestring) |
|
147 |
and iface in self.schema |
|
148 |
and self.etype_class(iface) |
|
149 |
or iface |
|
150 |
for iface in ifaces) |
|
151 |
if not ('Any' in ifaces or ifaces & implemented_interfaces): |
|
152 |
self.debug('kicking vobject %s (no implemented ' |
|
153 |
'interface among %s)', obj, ifaces) |
|
154 |
self.unregister(obj) |
|
155 |
# clear needs_iface so we don't try to remove some not-anymore-in |
|
156 |
# objects on automatic reloading |
|
157 |
self._needs_iface.clear() |
|
158 |
||
0 | 159 |
@cached |
160 |
def etype_class(self, etype): |
|
161 |
"""return an entity class for the given entity type. |
|
162 |
Try to find out a specific class for this kind of entity or |
|
163 |
default to a dump of the class registered for 'Any' |
|
164 |
""" |
|
165 |
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
|
166 |
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
|
167 |
return self.select(self.registry_objects('etypes', 'Any'), 'Any') |
0 | 168 |
eschema = self.schema.eschema(etype) |
169 |
baseschemas = [eschema] + eschema.ancestors() |
|
170 |
# browse ancestors from most specific to most generic and |
|
171 |
# try to find an associated custom entity class |
|
172 |
for baseschema in baseschemas: |
|
173 |
btype = str(baseschema) |
|
174 |
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
|
175 |
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
|
176 |
break |
0 | 177 |
except ObjectNotFound: |
178 |
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
|
179 |
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
|
180 |
# 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
|
181 |
# 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
|
182 |
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
|
183 |
return cls |
0 | 184 |
|
185 |
def render(self, registry, oid, req, **context): |
|
186 |
"""select an object in a given registry and render it |
|
187 |
||
188 |
- registry: the registry's name |
|
189 |
- oid : the view to call |
|
1475
5c1ec97f317e
should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents:
1357
diff
changeset
|
190 |
- req : the HTTP request |
0 | 191 |
""" |
192 |
objclss = self.registry_objects(registry, oid) |
|
193 |
try: |
|
194 |
rset = context.pop('rset') |
|
195 |
except KeyError: |
|
196 |
rset = None |
|
197 |
selected = self.select(objclss, req, rset, **context) |
|
1723 | 198 |
return selected.render(**context) |
1475
5c1ec97f317e
should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents:
1357
diff
changeset
|
199 |
|
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
816
diff
changeset
|
200 |
def main_template(self, req, oid='main-template', **context): |
0 | 201 |
"""display query by calling the given template (default to main), |
202 |
and returning the output as a string instead of requiring the [w]rite |
|
203 |
method as argument |
|
204 |
""" |
|
816
9cd49a910fce
kill Template class and 'templates' registry
sylvain.thenault@logilab.fr
parents:
785
diff
changeset
|
205 |
res = self.render('views', oid, req, **context) |
0 | 206 |
if isinstance(res, unicode): |
207 |
return res.encode(req.encoding) |
|
208 |
assert isinstance(res, str) |
|
209 |
return res |
|
210 |
||
211 |
def possible_vobjects(self, registry, *args, **kwargs): |
|
212 |
"""return an ordered list of possible app objects in a given registry, |
|
213 |
supposing they support the 'visible' and 'order' properties (as most |
|
214 |
visualizable objects) |
|
215 |
""" |
|
216 |
return [x for x in sorted(self.possible_objects(registry, *args, **kwargs), |
|
217 |
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
|
218 |
if x.propval('visible')] |
1475
5c1ec97f317e
should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents:
1357
diff
changeset
|
219 |
|
0 | 220 |
def possible_actions(self, req, rset, **kwargs): |
221 |
if rset is None: |
|
1381
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
1254
diff
changeset
|
222 |
actions = self.possible_vobjects('actions', req, rset, **kwargs) |
0 | 223 |
else: |
1381
6042f1b342bb
consider kwargs in possible_actions
sylvain.thenault@logilab.fr
parents:
1254
diff
changeset
|
224 |
actions = rset.possible_actions(**kwargs) # cached implementation |
0 | 225 |
result = {} |
226 |
for action in actions: |
|
227 |
result.setdefault(action.category, []).append(action) |
|
228 |
return result |
|
1475
5c1ec97f317e
should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents:
1357
diff
changeset
|
229 |
|
0 | 230 |
def possible_views(self, req, rset, **kwargs): |
231 |
"""return an iterator on possible views for this result set |
|
232 |
||
233 |
views returned are classes, not instances |
|
234 |
""" |
|
235 |
for vid, views in self.registry('views').items(): |
|
236 |
if vid[0] == '_': |
|
237 |
continue |
|
238 |
try: |
|
239 |
view = self.select(views, req, rset, **kwargs) |
|
240 |
if view.linkable(): |
|
241 |
yield view |
|
242 |
except NoSelectableObject: |
|
243 |
continue |
|
395
cce260264122
catch Exception in case there is some unexepected selector bug
sylvain.thenault@logilab.fr
parents:
355
diff
changeset
|
244 |
except Exception: |
cce260264122
catch Exception in case there is some unexepected selector bug
sylvain.thenault@logilab.fr
parents:
355
diff
changeset
|
245 |
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
|
246 |
vid, rset) |
1475
5c1ec97f317e
should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents:
1357
diff
changeset
|
247 |
|
0 | 248 |
def select_box(self, oid, *args, **kwargs): |
249 |
"""return the most specific view according to the result set""" |
|
250 |
try: |
|
251 |
return self.select_object('boxes', oid, *args, **kwargs) |
|
252 |
except NoSelectableObject: |
|
253 |
return |
|
254 |
||
255 |
def select_action(self, oid, *args, **kwargs): |
|
256 |
"""return the most specific view according to the result set""" |
|
257 |
try: |
|
258 |
return self.select_object('actions', oid, *args, **kwargs) |
|
259 |
except NoSelectableObject: |
|
260 |
return |
|
1475
5c1ec97f317e
should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents:
1357
diff
changeset
|
261 |
|
0 | 262 |
def select_component(self, cid, *args, **kwargs): |
263 |
"""return the most specific component according to the result set""" |
|
264 |
try: |
|
265 |
return self.select_object('components', cid, *args, **kwargs) |
|
266 |
except (NoSelectableObject, ObjectNotFound): |
|
267 |
return |
|
268 |
||
269 |
def select_view(self, __vid, req, rset, **kwargs): |
|
270 |
"""return the most specific view according to the result set""" |
|
271 |
views = self.registry_objects('views', __vid) |
|
272 |
return self.select(views, req, rset, **kwargs) |
|
273 |
||
1475
5c1ec97f317e
should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents:
1357
diff
changeset
|
274 |
|
0 | 275 |
# properties handling ##################################################### |
276 |
||
277 |
def user_property_keys(self, withsitewide=False): |
|
278 |
if withsitewide: |
|
279 |
return sorted(self['propertydefs']) |
|
280 |
return sorted(k for k, kd in self['propertydefs'].iteritems() |
|
281 |
if not kd['sitewide']) |
|
282 |
||
283 |
def register_property(self, key, type, help, default=None, vocabulary=None, |
|
284 |
sitewide=False): |
|
285 |
"""register a given property""" |
|
286 |
properties = self._registries['propertydefs'] |
|
287 |
assert type in YAMS_TO_PY |
|
1475
5c1ec97f317e
should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents:
1357
diff
changeset
|
288 |
properties[key] = {'type': type, 'vocabulary': vocabulary, |
0 | 289 |
'default': default, 'help': help, |
290 |
'sitewide': sitewide} |
|
291 |
||
292 |
def property_info(self, key): |
|
293 |
"""return dictionary containing description associated to the given |
|
294 |
property key (including type, defaut value, help and a site wide |
|
295 |
boolean) |
|
296 |
""" |
|
297 |
try: |
|
298 |
return self._registries['propertydefs'][key] |
|
299 |
except KeyError: |
|
300 |
if key.startswith('system.version.'): |
|
301 |
soft = key.split('.')[-1] |
|
302 |
return {'type': 'String', 'sitewide': True, |
|
303 |
'default': None, 'vocabulary': None, |
|
304 |
'help': _('%s software version of the database') % soft} |
|
305 |
raise UnknownProperty('unregistered property %r' % key) |
|
1475
5c1ec97f317e
should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents:
1357
diff
changeset
|
306 |
|
0 | 307 |
def property_value(self, key): |
308 |
try: |
|
309 |
return self._registries['propertyvalues'][key] |
|
310 |
except KeyError: |
|
311 |
return self._registries['propertydefs'][key]['default'] |
|
312 |
||
313 |
def typed_value(self, key, value): |
|
314 |
"""value is an unicode string, return it correctly typed. Let potential |
|
315 |
type error propagates. |
|
316 |
""" |
|
317 |
pdef = self.property_info(key) |
|
318 |
try: |
|
319 |
value = YAMS_TO_PY[pdef['type']](value) |
|
320 |
except (TypeError, ValueError): |
|
321 |
raise ValueError(_('bad value')) |
|
322 |
vocab = pdef['vocabulary'] |
|
323 |
if vocab is not None: |
|
324 |
if callable(vocab): |
|
325 |
vocab = vocab(key, None) # XXX need a req object |
|
326 |
if not value in vocab: |
|
327 |
raise ValueError(_('unauthorized value')) |
|
328 |
return value |
|
1475
5c1ec97f317e
should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents:
1357
diff
changeset
|
329 |
|
0 | 330 |
def init_properties(self, propvalues): |
331 |
"""init the property values registry using the given set of couple (key, value) |
|
332 |
""" |
|
333 |
self.initialized = True |
|
334 |
values = self._registries['propertyvalues'] |
|
335 |
for key, val in propvalues: |
|
336 |
try: |
|
337 |
values[key] = self.typed_value(key, val) |
|
338 |
except ValueError: |
|
339 |
self.warning('%s (you should probably delete that property ' |
|
340 |
'from the database)', ex) |
|
341 |
except UnknownProperty, ex: |
|
342 |
self.warning('%s (you should probably delete that property ' |
|
343 |
'from the database)', ex) |
|
344 |
||
345 |
def parse(self, session, rql, args=None): |
|
346 |
rqlst = self.rqlhelper.parse(rql) |
|
347 |
def type_from_eid(eid, session=session): |
|
348 |
return session.describe(eid)[0] |
|
1123
a8e2838f174a
catch UnknownEid and set empty solutions on select nodes
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
349 |
try: |
a8e2838f174a
catch UnknownEid and set empty solutions on select nodes
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
350 |
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
|
351 |
except UnknownEid: |
a8e2838f174a
catch UnknownEid and set empty solutions on select nodes
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
352 |
for select in rqlst.children: |
a8e2838f174a
catch UnknownEid and set empty solutions on select nodes
sylvain.thenault@logilab.fr
parents:
395
diff
changeset
|
353 |
select.solutions = [] |
0 | 354 |
return rqlst |
355 |
||
356 |
@property |
|
357 |
@cached |
|
358 |
def rqlhelper(self): |
|
359 |
return RQLHelper(self.schema, |
|
360 |
special_relations={'eid': 'uid', 'has_text': 'fti'}) |
|
361 |
||
169
0e031b66cb0b
don't systematically init_log, it may breaks client log configuration
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
362 |
|
0 | 363 |
class MulCnxCubicWebRegistry(CubicWebRegistry): |
364 |
"""special registry to be used when an application has to deal with |
|
365 |
connections to differents repository. This class add some additional wrapper |
|
366 |
trying to hide buggy class attributes since classes are not designed to be |
|
367 |
shared. |
|
368 |
""" |
|
369 |
def etype_class(self, etype): |
|
370 |
"""return an entity class for the given entity type. |
|
371 |
Try to find out a specific class for this kind of entity or |
|
372 |
default to a dump of the class registered for 'Any' |
|
373 |
""" |
|
374 |
usercls = super(MulCnxCubicWebRegistry, self).etype_class(etype) |
|
375 |
usercls.e_schema = self.schema.eschema(etype) |
|
376 |
return usercls |
|
377 |
||
378 |
def select(self, vobjects, *args, **kwargs): |
|
379 |
"""return an instance of the most specific object according |
|
380 |
to parameters |
|
381 |
||
382 |
raise NoSelectableObject if not object apply |
|
383 |
""" |
|
384 |
for vobject in vobjects: |
|
385 |
vobject.vreg = self |
|
386 |
vobject.schema = self.schema |
|
387 |
vobject.config = self.config |
|
388 |
return super(MulCnxCubicWebRegistry, self).select(vobjects, *args, **kwargs) |
|
1475
5c1ec97f317e
should not be necessary anymore to add entity class to __implements__
sylvain.thenault@logilab.fr
parents:
1357
diff
changeset
|
389 |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
390 |
from datetime import datetime, date, time, timedelta |
0 | 391 |
|
392 |
YAMS_TO_PY = { |
|
393 |
'Boolean': bool, |
|
394 |
'String' : unicode, |
|
395 |
'Password': str, |
|
396 |
'Bytes': Binary, |
|
397 |
'Int': int, |
|
398 |
'Float': float, |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
399 |
'Date': date, |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
400 |
'Datetime': datetime, |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
401 |
'Time': time, |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
402 |
'Interval': timedelta, |
0 | 403 |
} |
404 |