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