author | sylvain.thenault@logilab.fr |
Tue, 17 Feb 2009 17:31:01 +0100 | |
branch | tls-sprint |
changeset 673 | 64c50cb17d70 |
parent 668 | 61b2328f9ed9 |
child 682 | d95936d6ce5c |
permissions | -rw-r--r-- |
0 | 1 |
""" |
2 |
* the vregistry handle various type of objects interacting |
|
3 |
together. The vregistry handle registration of dynamically loaded |
|
4 |
objects and provide a convenient api access to those objects |
|
5 |
according to a context |
|
6 |
||
7 |
* to interact with the vregistry, object should inherit from the |
|
8 |
VObject abstract class |
|
9 |
|
|
10 |
* the registration procedure is delegated to a registerer. Each |
|
11 |
registerable vobject must defines its registerer class using the |
|
12 |
__registerer__ attribute. A registerer is instantianted at |
|
13 |
registration time after what the instance is lost |
|
14 |
|
|
15 |
* the selection procedure has been generalized by delegating to a |
|
16 |
selector, which is responsible to score the vobject according to the |
|
17 |
current state (req, rset, row, col). At the end of the selection, if |
|
18 |
a vobject class has been found, an instance of this class is |
|
19 |
returned. The selector is instantiated at vobject registration |
|
20 |
||
21 |
||
22 |
:organization: Logilab |
|
615
38bc11ac845b
don't use chainall when it's not necessary
sylvain.thenault@logilab.fr
parents:
355
diff
changeset
|
23 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 24 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
25 |
""" |
|
26 |
__docformat__ = "restructuredtext en" |
|
27 |
||
28 |
import sys |
|
29 |
from os import listdir, stat |
|
30 |
from os.path import dirname, join, realpath, split, isdir |
|
31 |
from logging import getLogger |
|
32 |
||
33 |
from cubicweb import CW_SOFTWARE_ROOT, set_log_methods |
|
34 |
from cubicweb import RegistryNotFound, ObjectNotFound, NoSelectableObject |
|
35 |
||
36 |
||
37 |
class vobject_helper(object): |
|
38 |
"""object instantiated at registration time to help a wrapped |
|
39 |
VObject subclass |
|
40 |
""" |
|
41 |
||
42 |
def __init__(self, registry, vobject): |
|
43 |
self.registry = registry |
|
44 |
self.vobject = vobject |
|
45 |
self.config = registry.config |
|
46 |
self.schema = registry.schema |
|
47 |
||
48 |
||
49 |
class registerer(vobject_helper): |
|
50 |
"""do whatever is needed at registration time for the wrapped |
|
51 |
class, according to current application schema and already |
|
52 |
registered objects of the same kind (i.e. same registry name and |
|
53 |
same id). |
|
54 |
||
55 |
The wrapped class may be skipped, some previously selected object |
|
56 |
may be kicked out... After whatever works needed, if the object or |
|
57 |
a transformed object is returned, it will be added to previously |
|
58 |
registered objects. |
|
59 |
""" |
|
60 |
||
61 |
def __init__(self, registry, vobject): |
|
62 |
super(registerer, self).__init__(registry, vobject) |
|
63 |
self.kicked = set() |
|
64 |
||
65 |
def do_it_yourself(self, registered): |
|
66 |
raise NotImplementedError(str(self.vobject)) |
|
67 |
||
68 |
def kick(self, registered, kicked): |
|
355
89ad20af9e4c
oops, these were committed unintentionnally
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
354
diff
changeset
|
69 |
self.debug('kicking vobject %s', kicked) |
0 | 70 |
registered.remove(kicked) |
71 |
self.kicked.add(kicked.classid()) |
|
72 |
||
73 |
def skip(self): |
|
74 |
self.debug('no schema compat, skipping %s', self.vobject) |
|
75 |
||
76 |
||
77 |
def selector(cls, *args, **kwargs): |
|
78 |
"""selector is called to help choosing the correct object for a |
|
79 |
particular request and result set by returning a score. |
|
80 |
||
81 |
it must implement a .score_method taking a request, a result set and |
|
82 |
optionaly row and col arguments which return an int telling how well |
|
83 |
the wrapped class apply to the given request and result set. 0 score |
|
84 |
means that it doesn't apply. |
|
85 |
|
|
86 |
rset may be None. If not, row and col arguments may be optionally |
|
87 |
given if the registry is scoring a given row or a given cell of |
|
88 |
the result set (both row and col are int if provided). |
|
89 |
""" |
|
90 |
raise NotImplementedError(cls) |
|
91 |
||
92 |
||
93 |
class autoselectors(type): |
|
94 |
"""implements __selectors__ / __select__ compatibility layer so that: |
|
95 |
||
96 |
__select__ = chainall(classmethod(A, B, C)) |
|
97 |
||
98 |
can be replaced by something like: |
|
99 |
|
|
100 |
__selectors__ = (A, B, C) |
|
101 |
""" |
|
102 |
def __new__(mcs, name, bases, classdict): |
|
103 |
if '__select__' in classdict and '__selectors__' in classdict: |
|
104 |
raise TypeError("__select__ and __selectors__ " |
|
105 |
"can't be used together") |
|
106 |
if '__select__' not in classdict and '__selectors__' in classdict: |
|
107 |
selectors = classdict['__selectors__'] |
|
615
38bc11ac845b
don't use chainall when it's not necessary
sylvain.thenault@logilab.fr
parents:
355
diff
changeset
|
108 |
if len(selectors) > 1: |
38bc11ac845b
don't use chainall when it's not necessary
sylvain.thenault@logilab.fr
parents:
355
diff
changeset
|
109 |
classdict['__select__'] = classmethod(chainall(*selectors)) |
38bc11ac845b
don't use chainall when it's not necessary
sylvain.thenault@logilab.fr
parents:
355
diff
changeset
|
110 |
else: |
38bc11ac845b
don't use chainall when it's not necessary
sylvain.thenault@logilab.fr
parents:
355
diff
changeset
|
111 |
classdict['__select__'] = classmethod(selectors[0]) |
0 | 112 |
return super(autoselectors, mcs).__new__(mcs, name, bases, classdict) |
113 |
||
114 |
def __setattr__(self, attr, value): |
|
115 |
if attr == '__selectors__': |
|
116 |
self.__select__ = classmethod(chainall(*value)) |
|
117 |
super(autoselectors, self).__setattr__(attr, value) |
|
118 |
||
119 |
||
120 |
class VObject(object): |
|
121 |
"""visual object, use to be handled somehow by the visual components |
|
122 |
registry. |
|
123 |
||
124 |
The following attributes should be set on concret vobject subclasses: |
|
125 |
|
|
126 |
:__registry__: |
|
127 |
name of the registry for this object (string like 'views', |
|
128 |
'templates'...) |
|
129 |
:id: |
|
130 |
object's identifier in the registry (string like 'main', |
|
131 |
'primary', 'folder_box') |
|
132 |
:__registerer__: |
|
133 |
registration helper class |
|
134 |
:__select__: |
|
135 |
selection helper function |
|
136 |
:__selectors__: |
|
137 |
tuple of selectors to be chained |
|
138 |
(__select__ and __selectors__ are mutually exclusive) |
|
139 |
|
|
140 |
Moreover, the `__abstract__` attribute may be set to True to indicate |
|
141 |
that a vobject is abstract and should not be registered |
|
142 |
""" |
|
143 |
__metaclass__ = autoselectors |
|
144 |
# necessary attributes to interact with the registry |
|
145 |
id = None |
|
146 |
__registry__ = None |
|
147 |
__registerer__ = None |
|
148 |
__select__ = None |
|
149 |
||
150 |
@classmethod |
|
151 |
def registered(cls, registry): |
|
152 |
"""called by the registry when the vobject has been registered. |
|
153 |
||
154 |
It must return the object that will be actually registered (this |
|
155 |
may be the right hook to create an instance for example). By |
|
156 |
default the vobject is returned without any transformation. |
|
157 |
""" |
|
158 |
return cls |
|
159 |
||
160 |
@classmethod |
|
161 |
def selected(cls, *args, **kwargs): |
|
162 |
"""called by the registry when the vobject has been selected. |
|
163 |
|
|
164 |
It must return the object that will be actually returned by the |
|
165 |
.select method (this may be the right hook to create an |
|
166 |
instance for example). By default the selected object is |
|
167 |
returned without any transformation. |
|
168 |
""" |
|
169 |
return cls |
|
170 |
||
171 |
@classmethod |
|
172 |
def classid(cls): |
|
173 |
"""returns a unique identifier for the vobject""" |
|
174 |
return '%s.%s' % (cls.__module__, cls.__name__) |
|
175 |
||
176 |
||
177 |
class VRegistry(object): |
|
178 |
"""class responsible to register, propose and select the various |
|
179 |
elements used to build the web interface. Currently, we have templates, |
|
180 |
views, actions and components. |
|
181 |
""" |
|
182 |
||
183 |
def __init__(self, config):#, cache_size=1000): |
|
184 |
self.config = config |
|
185 |
# dictionnary of registry (themself dictionnary) by name |
|
186 |
self._registries = {} |
|
187 |
self._lastmodifs = {} |
|
188 |
||
189 |
def reset(self): |
|
190 |
self._registries = {} |
|
191 |
self._lastmodifs = {} |
|
192 |
||
193 |
def __getitem__(self, key): |
|
194 |
return self._registries[key] |
|
195 |
||
196 |
def get(self, key, default=None): |
|
197 |
return self._registries.get(key, default) |
|
198 |
||
199 |
def items(self): |
|
200 |
return self._registries.items() |
|
201 |
||
202 |
def values(self): |
|
203 |
return self._registries.values() |
|
204 |
||
205 |
def __contains__(self, key): |
|
206 |
return key in self._registries |
|
660
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
652
diff
changeset
|
207 |
|
0 | 208 |
def registry(self, name): |
209 |
"""return the registry (dictionary of class objects) associated to |
|
210 |
this name |
|
211 |
""" |
|
212 |
try: |
|
213 |
return self._registries[name] |
|
214 |
except KeyError: |
|
215 |
raise RegistryNotFound(name), None, sys.exc_info()[-1] |
|
216 |
||
217 |
def registry_objects(self, name, oid=None): |
|
218 |
"""returns objects registered with the given oid in the given registry. |
|
219 |
If no oid is given, return all objects in this registry |
|
220 |
""" |
|
221 |
registry = self.registry(name) |
|
222 |
if oid: |
|
223 |
try: |
|
224 |
return registry[oid] |
|
225 |
except KeyError: |
|
226 |
raise ObjectNotFound(oid), None, sys.exc_info()[-1] |
|
227 |
else: |
|
228 |
result = [] |
|
229 |
for objs in registry.values(): |
|
230 |
result += objs |
|
231 |
return result |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
232 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
233 |
def object_by_id(self, registry, cid, *args, **kwargs): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
234 |
"""return the most specific component according to the resultset""" |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
235 |
objects = self[registry][cid] |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
236 |
assert len(objects) == 1, objects |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
237 |
return objects[0].selected(*args, **kwargs) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
238 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
239 |
# methods for explicit (un)registration ################################### |
668
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
240 |
|
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
241 |
# def clear(self, key): |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
242 |
# regname, oid = key.split('.') |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
243 |
# self[regname].pop(oid, None) |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
244 |
|
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
245 |
def register(self, obj, registryname=None, oid=None, clear=False): |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
246 |
"""base method to add an object in the registry""" |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
247 |
registryname = registryname or obj.__registry__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
248 |
oid = oid or obj.id |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
249 |
registry = self._registries.setdefault(registryname, {}) |
668
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
250 |
if clear: |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
251 |
vobjects = registry[oid] = [] |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
252 |
else: |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
253 |
vobjects = registry.setdefault(oid, []) |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
254 |
# registered() is technically a classmethod but is not declared |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
255 |
# as such because we need to compose registered in some cases |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
256 |
vobject = obj.registered.im_func(cls, self) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
257 |
assert not vobject in vobjects |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
258 |
vobjects.append(vobject) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
259 |
try: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
260 |
vname = vobject.__name__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
261 |
except AttributeError: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
262 |
vname = vobject.__class__.__name__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
263 |
self.debug('registered vobject %s in registry %s with id %s', |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
264 |
vname, registryname, oid) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
265 |
# automatic reloading management |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
266 |
self._registered['%s.%s' % (obj.__module__, oid)] = obj |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
267 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
268 |
def unregister(self, obj, registryname=None): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
269 |
registryname = registryname or obj.__registry__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
270 |
registry = self.registry(registryname) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
271 |
removed_id = obj.classid() |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
272 |
for registered in registry[obj.id]: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
273 |
# use classid() to compare classes because vreg will probably |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
274 |
# have its own version of the class, loaded through execfile |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
275 |
if registered.classid() == removed_id: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
276 |
# XXX automatic reloading management |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
277 |
try: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
278 |
registry[obj.id].remove(registered) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
279 |
except ValueError: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
280 |
self.warning('can\'t remove %s, no id %s in the %s registry', |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
281 |
removed_id, obj.id, registryname) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
282 |
except ValueError: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
283 |
self.warning('can\'t remove %s, not in the %s registry with id %s', |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
284 |
removed_id, registryname, obj.id) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
285 |
# else: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
286 |
# # if objects is empty, remove oid from registry |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
287 |
# if not registry[obj.id]: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
288 |
# del regcontent[oid] |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
289 |
break |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
290 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
291 |
def register_and_replace(self, obj, replaced, registryname=None): |
673 | 292 |
if hasattr(replaced, 'classid'): |
293 |
replaced = replaced.classid() |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
294 |
registryname = registryname or obj.__registry__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
295 |
registry = self.registry(registryname) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
296 |
registered_objs = registry[obj.id] |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
297 |
for index, registered in enumerate(registered_objs): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
298 |
if registered.classid() == replaced: |
673 | 299 |
del registry[obj.id][index] |
300 |
break |
|
301 |
self.register(obj, registryname=registryname) |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
302 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
303 |
# dynamic selection methods ############################################### |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
304 |
|
0 | 305 |
def select(self, vobjects, *args, **kwargs): |
306 |
"""return an instance of the most specific object according |
|
307 |
to parameters |
|
308 |
||
309 |
raise NoSelectableObject if not object apply |
|
310 |
""" |
|
177
73aa03734425
check we don't get selection ambiguity: if yes, log error in production env, raise in other modes
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
311 |
score, winners = 0, [] |
0 | 312 |
for vobject in vobjects: |
313 |
vobjectscore = vobject.__select__(*args, **kwargs) |
|
314 |
if vobjectscore > score: |
|
177
73aa03734425
check we don't get selection ambiguity: if yes, log error in production env, raise in other modes
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
315 |
score, winners = vobjectscore, [vobject] |
73aa03734425
check we don't get selection ambiguity: if yes, log error in production env, raise in other modes
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
316 |
elif vobjectscore > 0 and vobjectscore == score: |
73aa03734425
check we don't get selection ambiguity: if yes, log error in production env, raise in other modes
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
317 |
winners.append(vobject) |
73aa03734425
check we don't get selection ambiguity: if yes, log error in production env, raise in other modes
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
318 |
if not winners: |
0 | 319 |
raise NoSelectableObject('args: %s\nkwargs: %s %s' |
320 |
% (args, kwargs.keys(), [repr(v) for v in vobjects])) |
|
177
73aa03734425
check we don't get selection ambiguity: if yes, log error in production env, raise in other modes
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
321 |
if len(winners) > 1: |
73aa03734425
check we don't get selection ambiguity: if yes, log error in production env, raise in other modes
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
322 |
if self.config.mode == 'installed': |
73aa03734425
check we don't get selection ambiguity: if yes, log error in production env, raise in other modes
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
323 |
self.error('select ambiguity, args: %s\nkwargs: %s %s', |
73aa03734425
check we don't get selection ambiguity: if yes, log error in production env, raise in other modes
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
324 |
args, kwargs.keys(), [repr(v) for v in winners]) |
73aa03734425
check we don't get selection ambiguity: if yes, log error in production env, raise in other modes
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
325 |
else: |
73aa03734425
check we don't get selection ambiguity: if yes, log error in production env, raise in other modes
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
326 |
raise Exception('select ambiguity, args: %s\nkwargs: %s %s' |
73aa03734425
check we don't get selection ambiguity: if yes, log error in production env, raise in other modes
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
327 |
% (args, kwargs.keys(), [repr(v) for v in winners])) |
73aa03734425
check we don't get selection ambiguity: if yes, log error in production env, raise in other modes
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
328 |
winner = winners[0] |
0 | 329 |
# return the result of the .selected method of the vobject |
330 |
return winner.selected(*args, **kwargs) |
|
331 |
||
332 |
def possible_objects(self, registry, *args, **kwargs): |
|
333 |
"""return an iterator on possible objects in a registry for this result set |
|
334 |
||
335 |
actions returned are classes, not instances |
|
336 |
""" |
|
337 |
for vobjects in self.registry(registry).values(): |
|
338 |
try: |
|
339 |
yield self.select(vobjects, *args, **kwargs) |
|
340 |
except NoSelectableObject: |
|
341 |
continue |
|
342 |
||
343 |
def select_object(self, registry, cid, *args, **kwargs): |
|
344 |
"""return the most specific component according to the resultset""" |
|
345 |
return self.select(self.registry_objects(registry, cid), *args, **kwargs) |
|
346 |
||
347 |
# intialization methods ################################################### |
|
348 |
||
349 |
def register_objects(self, path, force_reload=None): |
|
350 |
if force_reload is None: |
|
351 |
force_reload = self.config.mode == 'dev' |
|
352 |
elif not force_reload: |
|
353 |
# force_reload == False usually mean modules have been reloaded |
|
354 |
# by another connection, so we want to update the registry |
|
355 |
# content even if there has been no module content modification |
|
356 |
self.reset() |
|
357 |
# need to clean sys.path this to avoid import confusion pb (i.e. |
|
358 |
# having the same module loaded as 'cubicweb.web.views' subpackage and |
|
359 |
# as views' or 'web.views' subpackage |
|
360 |
# this is mainly for testing purpose, we should'nt need this in |
|
361 |
# production environment |
|
362 |
for webdir in (join(dirname(realpath(__file__)), 'web'), |
|
363 |
join(dirname(__file__), 'web')): |
|
364 |
if webdir in sys.path: |
|
365 |
sys.path.remove(webdir) |
|
366 |
if CW_SOFTWARE_ROOT in sys.path: |
|
367 |
sys.path.remove(CW_SOFTWARE_ROOT) |
|
368 |
# load views from each directory in the application's path |
|
369 |
change = False |
|
370 |
for fileordirectory in path: |
|
371 |
if isdir(fileordirectory): |
|
372 |
if self.read_directory(fileordirectory, force_reload): |
|
373 |
change = True |
|
374 |
else: |
|
375 |
directory, filename = split(fileordirectory) |
|
376 |
if self.load_file(directory, filename, force_reload): |
|
377 |
change = True |
|
378 |
if change: |
|
379 |
for registry, objects in self.items(): |
|
380 |
self.debug('available in registry %s: %s', registry, |
|
381 |
sorted(objects)) |
|
382 |
return change |
|
383 |
||
384 |
def read_directory(self, directory, force_reload=False): |
|
385 |
"""read a directory and register available views""" |
|
386 |
modified_on = stat(realpath(directory))[-2] |
|
387 |
# only read directory if it was modified |
|
388 |
_lastmodifs = self._lastmodifs |
|
389 |
if directory in _lastmodifs and modified_on <= _lastmodifs[directory]: |
|
390 |
return False |
|
391 |
self.info('loading directory %s', directory) |
|
392 |
for filename in listdir(directory): |
|
393 |
if filename[-3:] == '.py': |
|
394 |
try: |
|
395 |
self.load_file(directory, filename, force_reload) |
|
396 |
except OSError: |
|
397 |
# this typically happens on emacs backup files (.#foo.py) |
|
398 |
self.warning('Unable to load file %s. It is likely to be a backup file', |
|
399 |
filename) |
|
400 |
except Exception, ex: |
|
401 |
if self.config.mode in ('dev', 'test'): |
|
402 |
raise |
|
403 |
self.exception('%r while loading file %s', ex, filename) |
|
404 |
_lastmodifs[directory] = modified_on |
|
405 |
return True |
|
406 |
||
407 |
def load_file(self, directory, filename, force_reload=False): |
|
408 |
"""load visual objects from a python file""" |
|
409 |
from logilab.common.modutils import load_module_from_modpath, modpath_from_file |
|
410 |
filepath = join(directory, filename) |
|
411 |
modified_on = stat(filepath)[-2] |
|
412 |
modpath = modpath_from_file(join(directory, filename)) |
|
413 |
modname = '.'.join(modpath) |
|
414 |
unregistered = {} |
|
415 |
_lastmodifs = self._lastmodifs |
|
416 |
if filepath in _lastmodifs: |
|
417 |
# only load file if it was modified |
|
418 |
if modified_on <= _lastmodifs[filepath]: |
|
419 |
return |
|
420 |
else: |
|
421 |
# if it was modified, unregister all exisiting objects |
|
422 |
# from this module, and keep track of what was unregistered |
|
423 |
unregistered = self.unregister_module_vobjects(modname) |
|
424 |
# load the module |
|
425 |
module = load_module_from_modpath(modpath, use_sys=not force_reload) |
|
426 |
registered = self.load_module(module) |
|
427 |
# if something was unregistered, we need to update places where it was |
|
428 |
# referenced |
|
429 |
if unregistered: |
|
430 |
# oldnew_mapping = {} |
|
431 |
oldnew_mapping = dict((unregistered[name], registered[name]) |
|
432 |
for name in unregistered if name in registered) |
|
433 |
self.update_registered_subclasses(oldnew_mapping) |
|
434 |
_lastmodifs[filepath] = modified_on |
|
435 |
return True |
|
436 |
||
437 |
def load_module(self, module): |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
438 |
self._registered = {} |
668
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
439 |
if hasattr(module, 'registration_callback'): |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
440 |
module.registration_callback(self) |
660
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
652
diff
changeset
|
441 |
else: |
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
652
diff
changeset
|
442 |
self.info('loading %s', module) |
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
652
diff
changeset
|
443 |
for objname, obj in vars(module).items(): |
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
652
diff
changeset
|
444 |
if objname.startswith('_'): |
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
652
diff
changeset
|
445 |
continue |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
446 |
self.load_ancestors_then_object(module.__name__, obj) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
447 |
return self._registered |
0 | 448 |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
449 |
def load_ancestors_then_object(self, modname, obj): |
0 | 450 |
# skip imported classes |
451 |
if getattr(obj, '__module__', None) != modname: |
|
452 |
return |
|
453 |
# skip non registerable object |
|
454 |
try: |
|
455 |
if not issubclass(obj, VObject): |
|
456 |
return |
|
457 |
except TypeError: |
|
458 |
return |
|
459 |
objname = '%s.%s' % (modname, obj.__name__) |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
460 |
if objname in self._registered: |
0 | 461 |
return |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
462 |
self._registered[objname] = obj |
0 | 463 |
for parent in obj.__bases__: |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
464 |
self.load_ancestors_then_object(modname, parent) |
0 | 465 |
self.load_object(obj) |
466 |
||
467 |
def load_object(self, obj): |
|
468 |
try: |
|
469 |
self.register_vobject_class(obj) |
|
470 |
except Exception, ex: |
|
471 |
if self.config.mode in ('test', 'dev'): |
|
472 |
raise |
|
473 |
self.exception('vobject %s registration failed: %s', obj, ex) |
|
474 |
||
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
475 |
# old automatic registration XXX deprecated ############################### |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
476 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
477 |
def register_vobject_class(self, cls): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
478 |
"""handle vobject class registration |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
479 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
480 |
vobject class with __abstract__ == True in their local dictionnary or |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
481 |
with a name starting starting by an underscore are not registered. |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
482 |
Also a vobject class needs to have __registry__ and id attributes set |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
483 |
to a non empty string to be registered. |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
484 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
485 |
Registration is actually handled by vobject's registerer. |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
486 |
""" |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
487 |
if (cls.__dict__.get('__abstract__') or cls.__name__[0] == '_' |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
488 |
or not cls.__registry__ or not cls.id): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
489 |
return |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
490 |
regname = cls.__registry__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
491 |
if cls.id in self.config['disable-%s' % regname]: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
492 |
return |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
493 |
registry = self._registries.setdefault(regname, {}) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
494 |
vobjects = registry.setdefault(cls.id, []) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
495 |
registerer = cls.__registerer__(self, cls) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
496 |
cls = registerer.do_it_yourself(vobjects) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
497 |
if cls: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
498 |
self.register(cls) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
499 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
500 |
def unregister_module_vobjects(self, modname): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
501 |
"""removes registered objects coming from a given module |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
502 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
503 |
returns a dictionnary classid/class of all classes that will need |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
504 |
to be updated after reload (i.e. vobjects referencing classes defined |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
505 |
in the <modname> module) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
506 |
""" |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
507 |
unregistered = {} |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
508 |
# browse each registered object |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
509 |
for registry, objdict in self.items(): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
510 |
for oid, objects in objdict.items(): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
511 |
for obj in objects[:]: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
512 |
objname = obj.classid() |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
513 |
# if the vobject is defined in this module, remove it |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
514 |
if objname.startswith(modname): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
515 |
unregistered[objname] = obj |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
516 |
objects.remove(obj) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
517 |
self.debug('unregistering %s in %s registry', |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
518 |
objname, registry) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
519 |
# if not, check if the vobject can be found in baseclasses |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
520 |
# (because we also want subclasses to be updated) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
521 |
else: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
522 |
if not isinstance(obj, type): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
523 |
obj = obj.__class__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
524 |
for baseclass in obj.__bases__: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
525 |
if hasattr(baseclass, 'classid'): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
526 |
baseclassid = baseclass.classid() |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
527 |
if baseclassid.startswith(modname): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
528 |
unregistered[baseclassid] = baseclass |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
529 |
# update oid entry |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
530 |
if objects: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
531 |
objdict[oid] = objects |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
532 |
else: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
533 |
del objdict[oid] |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
534 |
return unregistered |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
535 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
536 |
def update_registered_subclasses(self, oldnew_mapping): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
537 |
"""updates subclasses of re-registered vobjects |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
538 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
539 |
if baseviews.PrimaryView is changed, baseviews.py will be reloaded |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
540 |
automatically and the new version of PrimaryView will be registered. |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
541 |
But all existing subclasses must also be notified of this change, and |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
542 |
that's what this method does |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
543 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
544 |
:param oldnew_mapping: a dict mapping old version of a class to |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
545 |
the new version |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
546 |
""" |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
547 |
# browse each registered object |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
548 |
for objdict in self.values(): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
549 |
for objects in objdict.values(): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
550 |
for obj in objects: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
551 |
if not isinstance(obj, type): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
552 |
obj = obj.__class__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
553 |
# build new baseclasses tuple |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
554 |
newbases = tuple(oldnew_mapping.get(baseclass, baseclass) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
555 |
for baseclass in obj.__bases__) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
556 |
# update obj's baseclasses tuple (__bases__) if needed |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
557 |
if newbases != obj.__bases__: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
558 |
self.debug('updating %s.%s base classes', |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
559 |
obj.__module__, obj.__name__) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
560 |
obj.__bases__ = newbases |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
561 |
|
0 | 562 |
# init logging |
563 |
set_log_methods(VObject, getLogger('cubicweb')) |
|
564 |
set_log_methods(VRegistry, getLogger('cubicweb.registry')) |
|
565 |
set_log_methods(registerer, getLogger('cubicweb.registration')) |
|
566 |
||
567 |
||
568 |
# advanced selector building functions ######################################## |
|
569 |
||
623
9dc7b3fa59f1
chainfirst / chainall can be given a name argument to use as inner function's name
sylvain.thenault@logilab.fr
parents:
615
diff
changeset
|
570 |
def chainall(*selectors, **kwargs): |
0 | 571 |
"""return a selector chaining given selectors. If one of |
572 |
the selectors fail, selection will fail, else the returned score |
|
573 |
will be the sum of each selector'score |
|
574 |
""" |
|
575 |
assert selectors |
|
576 |
def selector(cls, *args, **kwargs): |
|
577 |
score = 0 |
|
578 |
for selector in selectors: |
|
579 |
partscore = selector(cls, *args, **kwargs) |
|
580 |
if not partscore: |
|
581 |
return 0 |
|
582 |
score += partscore |
|
583 |
return score |
|
623
9dc7b3fa59f1
chainfirst / chainall can be given a name argument to use as inner function's name
sylvain.thenault@logilab.fr
parents:
615
diff
changeset
|
584 |
if 'name' in kwargs: |
9dc7b3fa59f1
chainfirst / chainall can be given a name argument to use as inner function's name
sylvain.thenault@logilab.fr
parents:
615
diff
changeset
|
585 |
selector.__name__ = kwargs['name'] |
0 | 586 |
return selector |
587 |
||
623
9dc7b3fa59f1
chainfirst / chainall can be given a name argument to use as inner function's name
sylvain.thenault@logilab.fr
parents:
615
diff
changeset
|
588 |
def chainfirst(*selectors, **kwargs): |
0 | 589 |
"""return a selector chaining given selectors. If all |
590 |
the selectors fail, selection will fail, else the returned score |
|
591 |
will be the first non-zero selector score |
|
592 |
""" |
|
593 |
assert selectors |
|
594 |
def selector(cls, *args, **kwargs): |
|
595 |
for selector in selectors: |
|
596 |
partscore = selector(cls, *args, **kwargs) |
|
597 |
if partscore: |
|
598 |
return partscore |
|
599 |
return 0 |
|
623
9dc7b3fa59f1
chainfirst / chainall can be given a name argument to use as inner function's name
sylvain.thenault@logilab.fr
parents:
615
diff
changeset
|
600 |
if 'name' in kwargs: |
9dc7b3fa59f1
chainfirst / chainall can be given a name argument to use as inner function's name
sylvain.thenault@logilab.fr
parents:
615
diff
changeset
|
601 |
selector.__name__ = kwargs['name'] |
0 | 602 |
return selector |
603 |
||
630
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
604 |
|
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
605 |
# selector base classes and operations ######################################## |
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:
630
diff
changeset
|
606 |
|
630
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
607 |
class Selector(object): |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
608 |
"""base class for selector classes providing implementation |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
609 |
for operators ``&`` and ``|`` |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
610 |
|
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
611 |
This class is only here to give access to binary operators, the |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
612 |
selector logic itself should be implemented in the __call__ method |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
613 |
""" |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
614 |
def __init__(self, *selectors): |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
615 |
self.selectors = self.merge_selectors(selectors) |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
616 |
|
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
617 |
@classmethod |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
618 |
def merge_selectors(cls, selectors): |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
619 |
"""merge selectors when possible : |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
620 |
|
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
621 |
AndSelector(AndSelector(sel1, sel2), AndSelector(sel3, sel4)) |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
622 |
==> AndSelector(sel1, sel2, sel3, sel4) |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
623 |
""" |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
624 |
merged_selectors = [] |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
625 |
for selector in selectors: |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
626 |
if isinstance(selector, cls): |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
627 |
merged_selectors += selector.selectors |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
628 |
else: |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
629 |
merged_selectors.append(selector) |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
630 |
return merged_selectors |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
631 |
|
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
632 |
def __and__(self, other): |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
633 |
return AndSelector(self, other) |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
634 |
|
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
635 |
def __or__(self, other): |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
636 |
return OrSelector(self, other) |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
637 |
|
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
638 |
__ror__ = __or__ # for cases like (function | selector) |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
639 |
__rand__ = __and__ # for cases like (function & selector) |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
640 |
# XXX (function | function) or (function & function) not managed yet |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
641 |
|
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
642 |
def __call__(self, cls, *args, **kwargs): |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
643 |
return NotImplementedError("selector %s must implement its logic " |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
644 |
"in its __call__ method" % self.__class__.__name__) |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
645 |
|
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
646 |
|
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
647 |
class AndSelector(Selector): |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
648 |
"""and-chained selectors (formerly known as chainall)""" |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
649 |
def __call__(self, cls, *args, **kwargs): |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
650 |
score = 0 |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
651 |
for selector in self.selectors: |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
652 |
partscore = selector(cls, *args, **kwargs) |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
653 |
if not partscore: |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
654 |
return 0 |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
655 |
score += partscore |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
656 |
return score |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
657 |
|
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
658 |
|
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
659 |
class OrSelector(Selector): |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
660 |
"""or-chained selectors (formerly known as chainfirst)""" |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
661 |
def __call__(self, cls, *args, **kwargs): |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
662 |
for selector in self.selectors: |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
663 |
partscore = selector(cls, *args, **kwargs) |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
664 |
if partscore: |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
665 |
return partscore |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
666 |
return 0 |