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