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