author | sylvain.thenault@logilab.fr |
Tue, 17 Feb 2009 23:43:08 +0100 | |
branch | tls-sprint |
changeset 724 | 1ecba333cfb0 |
parent 721 | f76e89b52553 |
child 727 | 30fe8f5afbd8 |
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 |
||
712
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
134 |
@classmethod |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
135 |
def build___select__(cls): |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
136 |
classdict = cls.__dict__ |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
137 |
if '__select__' in classdict and '__selectors__' in classdict: |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
138 |
raise TypeError("__select__ and __selectors__ can't be used together") |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
139 |
if '__selectors__' in classdict: |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
140 |
# 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
|
141 |
# 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
|
142 |
selectors = classdict['__selectors__'] |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
143 |
if len(selectors) == 1: |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
144 |
# 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
|
145 |
# only one selector |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
146 |
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
|
147 |
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
|
148 |
for selector in selectors) |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
149 |
return cls.__select__ |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
150 |
|
0 | 151 |
|
152 |
class VRegistry(object): |
|
153 |
"""class responsible to register, propose and select the various |
|
154 |
elements used to build the web interface. Currently, we have templates, |
|
155 |
views, actions and components. |
|
156 |
""" |
|
157 |
||
158 |
def __init__(self, config):#, cache_size=1000): |
|
159 |
self.config = config |
|
160 |
# dictionnary of registry (themself dictionnary) by name |
|
161 |
self._registries = {} |
|
162 |
self._lastmodifs = {} |
|
163 |
||
164 |
def reset(self): |
|
165 |
self._registries = {} |
|
166 |
self._lastmodifs = {} |
|
167 |
||
168 |
def __getitem__(self, key): |
|
169 |
return self._registries[key] |
|
170 |
||
171 |
def get(self, key, default=None): |
|
172 |
return self._registries.get(key, default) |
|
173 |
||
174 |
def items(self): |
|
175 |
return self._registries.items() |
|
176 |
||
177 |
def values(self): |
|
178 |
return self._registries.values() |
|
179 |
||
180 |
def __contains__(self, key): |
|
181 |
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
|
182 |
|
0 | 183 |
def registry(self, name): |
184 |
"""return the registry (dictionary of class objects) associated to |
|
185 |
this name |
|
186 |
""" |
|
187 |
try: |
|
188 |
return self._registries[name] |
|
189 |
except KeyError: |
|
190 |
raise RegistryNotFound(name), None, sys.exc_info()[-1] |
|
191 |
||
192 |
def registry_objects(self, name, oid=None): |
|
193 |
"""returns objects registered with the given oid in the given registry. |
|
194 |
If no oid is given, return all objects in this registry |
|
195 |
""" |
|
196 |
registry = self.registry(name) |
|
197 |
if oid: |
|
198 |
try: |
|
199 |
return registry[oid] |
|
200 |
except KeyError: |
|
201 |
raise ObjectNotFound(oid), None, sys.exc_info()[-1] |
|
202 |
else: |
|
203 |
result = [] |
|
204 |
for objs in registry.values(): |
|
205 |
result += objs |
|
206 |
return result |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
207 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
208 |
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
|
209 |
"""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
|
210 |
objects = self[registry][cid] |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
211 |
assert len(objects) == 1, objects |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
212 |
return objects[0].selected(*args, **kwargs) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
213 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
214 |
# methods for explicit (un)registration ################################### |
668
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
215 |
|
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
216 |
# def clear(self, key): |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
217 |
# regname, oid = key.split('.') |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
218 |
# self[regname].pop(oid, None) |
695 | 219 |
def register_all(self, objects, modname, butclasses=()): |
220 |
for obj in objects: |
|
221 |
try: |
|
222 |
if obj.__module__ != modname or obj in butclasses: |
|
223 |
continue |
|
224 |
oid = obj.id |
|
225 |
except AttributeError: |
|
226 |
continue |
|
227 |
if oid: |
|
228 |
self.register(obj) |
|
229 |
||
668
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
230 |
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
|
231 |
"""base method to add an object in the registry""" |
707 | 232 |
assert not '__abstract__' in obj.__dict__ |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
233 |
registryname = registryname or obj.__registry__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
234 |
oid = oid or obj.id |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
235 |
registry = self._registries.setdefault(registryname, {}) |
668
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
236 |
if clear: |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
237 |
vobjects = registry[oid] = [] |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
238 |
else: |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
239 |
vobjects = registry.setdefault(oid, []) |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
240 |
# 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
|
241 |
# as such because we need to compose registered in some cases |
682 | 242 |
vobject = obj.registered.im_func(obj, self) |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
243 |
assert not vobject in vobjects |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
244 |
vobjects.append(vobject) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
245 |
try: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
246 |
vname = vobject.__name__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
247 |
except AttributeError: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
248 |
vname = vobject.__class__.__name__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
249 |
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
|
250 |
vname, registryname, oid) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
251 |
# automatic reloading management |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
252 |
self._registered['%s.%s' % (obj.__module__, oid)] = obj |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
253 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
254 |
def unregister(self, obj, registryname=None): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
255 |
registryname = registryname or obj.__registry__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
256 |
registry = self.registry(registryname) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
257 |
removed_id = obj.classid() |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
258 |
for registered in registry[obj.id]: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
259 |
# 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
|
260 |
# 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
|
261 |
if registered.classid() == removed_id: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
262 |
# XXX automatic reloading management |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
263 |
try: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
264 |
registry[obj.id].remove(registered) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
265 |
except ValueError: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
266 |
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
|
267 |
removed_id, obj.id, registryname) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
268 |
except ValueError: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
269 |
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
|
270 |
removed_id, registryname, obj.id) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
271 |
# else: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
272 |
# # if objects is empty, remove oid from registry |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
273 |
# if not registry[obj.id]: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
274 |
# del regcontent[oid] |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
275 |
break |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
276 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
277 |
def register_and_replace(self, obj, replaced, registryname=None): |
673 | 278 |
if hasattr(replaced, 'classid'): |
279 |
replaced = replaced.classid() |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
280 |
registryname = registryname or obj.__registry__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
281 |
registry = self.registry(registryname) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
282 |
registered_objs = registry[obj.id] |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
283 |
for index, registered in enumerate(registered_objs): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
284 |
if registered.classid() == replaced: |
673 | 285 |
del registry[obj.id][index] |
286 |
break |
|
287 |
self.register(obj, registryname=registryname) |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
288 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
289 |
# dynamic selection methods ############################################### |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
290 |
|
0 | 291 |
def select(self, vobjects, *args, **kwargs): |
292 |
"""return an instance of the most specific object according |
|
293 |
to parameters |
|
294 |
||
295 |
raise NoSelectableObject if not object apply |
|
296 |
""" |
|
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
|
297 |
score, winners = 0, [] |
0 | 298 |
for vobject in vobjects: |
299 |
vobjectscore = vobject.__select__(*args, **kwargs) |
|
300 |
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
|
301 |
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
|
302 |
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
|
303 |
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
|
304 |
if not winners: |
0 | 305 |
raise NoSelectableObject('args: %s\nkwargs: %s %s' |
306 |
% (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
|
307 |
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
|
308 |
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
|
309 |
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
|
310 |
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
|
311 |
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
|
312 |
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
|
313 |
% (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
|
314 |
winner = winners[0] |
0 | 315 |
# return the result of the .selected method of the vobject |
316 |
return winner.selected(*args, **kwargs) |
|
317 |
||
318 |
def possible_objects(self, registry, *args, **kwargs): |
|
319 |
"""return an iterator on possible objects in a registry for this result set |
|
320 |
||
321 |
actions returned are classes, not instances |
|
322 |
""" |
|
323 |
for vobjects in self.registry(registry).values(): |
|
324 |
try: |
|
325 |
yield self.select(vobjects, *args, **kwargs) |
|
326 |
except NoSelectableObject: |
|
327 |
continue |
|
328 |
||
329 |
def select_object(self, registry, cid, *args, **kwargs): |
|
330 |
"""return the most specific component according to the resultset""" |
|
331 |
return self.select(self.registry_objects(registry, cid), *args, **kwargs) |
|
332 |
||
333 |
# intialization methods ################################################### |
|
334 |
||
335 |
def register_objects(self, path, force_reload=None): |
|
336 |
if force_reload is None: |
|
337 |
force_reload = self.config.mode == 'dev' |
|
338 |
elif not force_reload: |
|
339 |
# force_reload == False usually mean modules have been reloaded |
|
340 |
# by another connection, so we want to update the registry |
|
341 |
# content even if there has been no module content modification |
|
342 |
self.reset() |
|
343 |
# need to clean sys.path this to avoid import confusion pb (i.e. |
|
344 |
# having the same module loaded as 'cubicweb.web.views' subpackage and |
|
345 |
# as views' or 'web.views' subpackage |
|
346 |
# this is mainly for testing purpose, we should'nt need this in |
|
347 |
# production environment |
|
348 |
for webdir in (join(dirname(realpath(__file__)), 'web'), |
|
349 |
join(dirname(__file__), 'web')): |
|
350 |
if webdir in sys.path: |
|
351 |
sys.path.remove(webdir) |
|
352 |
if CW_SOFTWARE_ROOT in sys.path: |
|
353 |
sys.path.remove(CW_SOFTWARE_ROOT) |
|
354 |
# load views from each directory in the application's path |
|
355 |
change = False |
|
356 |
for fileordirectory in path: |
|
357 |
if isdir(fileordirectory): |
|
358 |
if self.read_directory(fileordirectory, force_reload): |
|
359 |
change = True |
|
360 |
else: |
|
361 |
directory, filename = split(fileordirectory) |
|
362 |
if self.load_file(directory, filename, force_reload): |
|
363 |
change = True |
|
364 |
if change: |
|
365 |
for registry, objects in self.items(): |
|
366 |
self.debug('available in registry %s: %s', registry, |
|
367 |
sorted(objects)) |
|
368 |
return change |
|
369 |
||
370 |
def read_directory(self, directory, force_reload=False): |
|
371 |
"""read a directory and register available views""" |
|
372 |
modified_on = stat(realpath(directory))[-2] |
|
373 |
# only read directory if it was modified |
|
374 |
_lastmodifs = self._lastmodifs |
|
375 |
if directory in _lastmodifs and modified_on <= _lastmodifs[directory]: |
|
376 |
return False |
|
377 |
self.info('loading directory %s', directory) |
|
378 |
for filename in listdir(directory): |
|
379 |
if filename[-3:] == '.py': |
|
380 |
try: |
|
381 |
self.load_file(directory, filename, force_reload) |
|
382 |
except OSError: |
|
383 |
# this typically happens on emacs backup files (.#foo.py) |
|
384 |
self.warning('Unable to load file %s. It is likely to be a backup file', |
|
385 |
filename) |
|
386 |
except Exception, ex: |
|
387 |
if self.config.mode in ('dev', 'test'): |
|
388 |
raise |
|
389 |
self.exception('%r while loading file %s', ex, filename) |
|
390 |
_lastmodifs[directory] = modified_on |
|
391 |
return True |
|
392 |
||
393 |
def load_file(self, directory, filename, force_reload=False): |
|
394 |
"""load visual objects from a python file""" |
|
395 |
from logilab.common.modutils import load_module_from_modpath, modpath_from_file |
|
396 |
filepath = join(directory, filename) |
|
397 |
modified_on = stat(filepath)[-2] |
|
398 |
modpath = modpath_from_file(join(directory, filename)) |
|
399 |
modname = '.'.join(modpath) |
|
400 |
unregistered = {} |
|
401 |
_lastmodifs = self._lastmodifs |
|
402 |
if filepath in _lastmodifs: |
|
403 |
# only load file if it was modified |
|
404 |
if modified_on <= _lastmodifs[filepath]: |
|
405 |
return |
|
406 |
else: |
|
407 |
# if it was modified, unregister all exisiting objects |
|
408 |
# from this module, and keep track of what was unregistered |
|
409 |
unregistered = self.unregister_module_vobjects(modname) |
|
410 |
# load the module |
|
411 |
module = load_module_from_modpath(modpath, use_sys=not force_reload) |
|
412 |
registered = self.load_module(module) |
|
413 |
# if something was unregistered, we need to update places where it was |
|
414 |
# referenced |
|
415 |
if unregistered: |
|
416 |
# oldnew_mapping = {} |
|
417 |
oldnew_mapping = dict((unregistered[name], registered[name]) |
|
418 |
for name in unregistered if name in registered) |
|
419 |
self.update_registered_subclasses(oldnew_mapping) |
|
420 |
_lastmodifs[filepath] = modified_on |
|
421 |
return True |
|
422 |
||
423 |
def load_module(self, module): |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
424 |
self._registered = {} |
668
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
425 |
if hasattr(module, 'registration_callback'): |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
426 |
module.registration_callback(self) |
660
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
652
diff
changeset
|
427 |
else: |
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
652
diff
changeset
|
428 |
self.info('loading %s', module) |
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
652
diff
changeset
|
429 |
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
|
430 |
if objname.startswith('_'): |
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
652
diff
changeset
|
431 |
continue |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
432 |
self.load_ancestors_then_object(module.__name__, obj) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
433 |
return self._registered |
0 | 434 |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
435 |
def load_ancestors_then_object(self, modname, obj): |
0 | 436 |
# skip imported classes |
437 |
if getattr(obj, '__module__', None) != modname: |
|
438 |
return |
|
439 |
# skip non registerable object |
|
440 |
try: |
|
441 |
if not issubclass(obj, VObject): |
|
442 |
return |
|
443 |
except TypeError: |
|
444 |
return |
|
445 |
objname = '%s.%s' % (modname, obj.__name__) |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
446 |
if objname in self._registered: |
0 | 447 |
return |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
448 |
self._registered[objname] = obj |
0 | 449 |
for parent in obj.__bases__: |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
450 |
self.load_ancestors_then_object(modname, parent) |
0 | 451 |
self.load_object(obj) |
452 |
||
453 |
def load_object(self, obj): |
|
454 |
try: |
|
455 |
self.register_vobject_class(obj) |
|
456 |
except Exception, ex: |
|
457 |
if self.config.mode in ('test', 'dev'): |
|
458 |
raise |
|
459 |
self.exception('vobject %s registration failed: %s', obj, ex) |
|
460 |
||
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
461 |
# old automatic registration XXX deprecated ############################### |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
462 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
463 |
def register_vobject_class(self, cls): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
464 |
"""handle vobject class registration |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
465 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
466 |
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
|
467 |
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
|
468 |
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
|
469 |
to a non empty string to be registered. |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
470 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
471 |
Registration is actually handled by vobject's registerer. |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
472 |
""" |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
473 |
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
|
474 |
or not cls.__registry__ or not cls.id): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
475 |
return |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
476 |
regname = cls.__registry__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
477 |
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
|
478 |
return |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
479 |
registry = self._registries.setdefault(regname, {}) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
480 |
vobjects = registry.setdefault(cls.id, []) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
481 |
registerer = cls.__registerer__(self, cls) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
482 |
cls = registerer.do_it_yourself(vobjects) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
483 |
if cls: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
484 |
self.register(cls) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
485 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
486 |
def unregister_module_vobjects(self, modname): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
487 |
"""removes registered objects coming from a given module |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
488 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
489 |
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
|
490 |
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
|
491 |
in the <modname> 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 |
unregistered = {} |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
494 |
# browse each registered object |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
495 |
for registry, objdict in self.items(): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
496 |
for oid, objects in objdict.items(): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
497 |
for obj in objects[:]: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
498 |
objname = obj.classid() |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
499 |
# 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
|
500 |
if objname.startswith(modname): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
501 |
unregistered[objname] = obj |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
502 |
objects.remove(obj) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
503 |
self.debug('unregistering %s in %s registry', |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
504 |
objname, registry) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
505 |
# 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
|
506 |
# (because we also want subclasses to be updated) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
507 |
else: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
508 |
if not isinstance(obj, type): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
509 |
obj = obj.__class__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
510 |
for baseclass in obj.__bases__: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
511 |
if hasattr(baseclass, 'classid'): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
512 |
baseclassid = baseclass.classid() |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
513 |
if baseclassid.startswith(modname): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
514 |
unregistered[baseclassid] = baseclass |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
515 |
# update oid entry |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
516 |
if objects: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
517 |
objdict[oid] = objects |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
518 |
else: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
519 |
del objdict[oid] |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
520 |
return unregistered |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
521 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
522 |
def update_registered_subclasses(self, oldnew_mapping): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
523 |
"""updates subclasses of re-registered vobjects |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
524 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
525 |
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
|
526 |
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
|
527 |
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
|
528 |
that's what this method does |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
529 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
530 |
: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
|
531 |
the new version |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
532 |
""" |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
533 |
# browse each registered object |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
534 |
for objdict in self.values(): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
535 |
for objects in objdict.values(): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
536 |
for obj in objects: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
537 |
if not isinstance(obj, type): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
538 |
obj = obj.__class__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
539 |
# build new baseclasses tuple |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
540 |
newbases = tuple(oldnew_mapping.get(baseclass, baseclass) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
541 |
for baseclass in obj.__bases__) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
542 |
# update obj's baseclasses tuple (__bases__) if needed |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
543 |
if newbases != obj.__bases__: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
544 |
self.debug('updating %s.%s base classes', |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
545 |
obj.__module__, obj.__name__) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
546 |
obj.__bases__ = newbases |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
547 |
|
0 | 548 |
# init logging |
549 |
set_log_methods(VObject, getLogger('cubicweb')) |
|
550 |
set_log_methods(VRegistry, getLogger('cubicweb.registry')) |
|
551 |
set_log_methods(registerer, getLogger('cubicweb.registration')) |
|
552 |
||
553 |
||
630
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
554 |
# 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
|
555 |
|
630
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
556 |
class Selector(object): |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
557 |
"""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
|
558 |
for operators ``&`` and ``|`` |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
559 |
|
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
560 |
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
|
561 |
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
|
562 |
|
f7011679437a
doc update, move yes_registerer here
sylvain.thenault@logilab.fr
parents:
714
diff
changeset
|
563 |
|
f7011679437a
doc update, move yes_registerer here
sylvain.thenault@logilab.fr
parents:
714
diff
changeset
|
564 |
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
|
565 |
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
|
566 |
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
|
567 |
|
f7011679437a
doc update, move yes_registerer here
sylvain.thenault@logilab.fr
parents:
714
diff
changeset
|
568 |
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
|
569 |
""" |
698
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
570 |
|
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
|
571 |
@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
|
572 |
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
|
573 |
# 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
|
574 |
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
|
575 |
|
698
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
576 |
def search_selector(self, selector): |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
577 |
"""search for the given selector or selector instance in the selectors |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
578 |
tree. Return it of None if not found |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
579 |
""" |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
580 |
if self is selector: |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
581 |
return self |
721 | 582 |
if isinstance(selector, type) and isinstance(self, selector): |
698
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
583 |
return self |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
584 |
return None |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
585 |
|
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
586 |
def __and__(self, other): |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
587 |
return AndSelector(self, other) |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
588 |
def __rand__(self, other): |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
589 |
return AndSelector(other, self) |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
590 |
|
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
591 |
def __or__(self, other): |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
592 |
return OrSelector(self, other) |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
593 |
def __ror__(self, other): |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
594 |
return OrSelector(other, self) |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
595 |
|
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
596 |
# XXX (function | function) or (function & function) not managed yet |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
597 |
|
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
598 |
def __call__(self, cls, *args, **kwargs): |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
599 |
return NotImplementedError("selector %s must implement its logic " |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
600 |
"in its __call__ method" % self.__class__) |
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 |
class MultiSelector(Selector): |
718
f7011679437a
doc update, move yes_registerer here
sylvain.thenault@logilab.fr
parents:
714
diff
changeset
|
603 |
"""base class for compound selector classes""" |
f7011679437a
doc update, move yes_registerer here
sylvain.thenault@logilab.fr
parents:
714
diff
changeset
|
604 |
|
630
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
605 |
def __init__(self, *selectors): |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
606 |
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
|
607 |
|
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
608 |
@classmethod |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
609 |
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
|
610 |
"""merge selectors when possible : |
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 |
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
|
613 |
==> 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
|
614 |
""" |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
615 |
merged_selectors = [] |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
616 |
for selector in selectors: |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
617 |
if isinstance(selector, cls): |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
618 |
merged_selectors += selector.selectors |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
619 |
else: |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
620 |
merged_selectors.append(selector) |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
621 |
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
|
622 |
|
cf0817dfc787
reimplements chainall/chainfirst using [And/Or], implemenent search_selector function on Selector base class
sylvain.thenault@logilab.fr
parents:
686
diff
changeset
|
623 |
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
|
624 |
"""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
|
625 |
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
|
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 |
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
|
628 |
try: |
cf0817dfc787
reimplements chainall/chainfirst using [And/Or], implemenent search_selector function on Selector base class
sylvain.thenault@logilab.fr
parents:
686
diff
changeset
|
629 |
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
|
630 |
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
|
631 |
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
|
632 |
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
|
633 |
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
|
634 |
return None |
630
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
635 |
|
697
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
695
diff
changeset
|
636 |
|
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
695
diff
changeset
|
637 |
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
|
638 |
"""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
|
639 |
would be overkill:: |
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 |
@objectify_selector |
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
695
diff
changeset
|
642 |
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
|
643 |
return 1 |
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 |
""" |
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
695
diff
changeset
|
646 |
return type(selector_func.__name__, (Selector,), |
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
695
diff
changeset
|
647 |
{'__call__': lambda self, *args: selector_func(*args)}) |
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
695
diff
changeset
|
648 |
|
712
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
649 |
def _instantiate_selector(selector): |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
650 |
"""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
|
651 |
|
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
652 |
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
|
653 |
""" |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
654 |
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
|
655 |
return objectify_selector(selector)() |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
656 |
if issubclass(selector, Selector): |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
657 |
return selector() |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
658 |
return selector |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
659 |
|
630
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
660 |
|
698
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
661 |
class AndSelector(MultiSelector): |
630
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
662 |
"""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
|
663 |
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
|
664 |
score = 0 |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
665 |
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
|
666 |
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
|
667 |
if not partscore: |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
668 |
return 0 |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
669 |
score += partscore |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
670 |
return score |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
671 |
|
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
672 |
|
698
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
673 |
class OrSelector(MultiSelector): |
630
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
674 |
"""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
|
675 |
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
|
676 |
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
|
677 |
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
|
678 |
if partscore: |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
679 |
return partscore |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
680 |
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
|
681 |
|
cf0817dfc787
reimplements chainall/chainfirst using [And/Or], implemenent search_selector function on Selector base class
sylvain.thenault@logilab.fr
parents:
686
diff
changeset
|
682 |
|
cf0817dfc787
reimplements chainall/chainfirst using [And/Or], implemenent search_selector function on Selector base class
sylvain.thenault@logilab.fr
parents:
686
diff
changeset
|
683 |
# 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
|
684 |
|
cf0817dfc787
reimplements chainall/chainfirst using [And/Or], implemenent search_selector function on Selector base class
sylvain.thenault@logilab.fr
parents:
686
diff
changeset
|
685 |
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
|
686 |
"""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
|
687 |
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
|
688 |
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
|
689 |
""" |
cf0817dfc787
reimplements chainall/chainfirst using [And/Or], implemenent search_selector function on Selector base class
sylvain.thenault@logilab.fr
parents:
686
diff
changeset
|
690 |
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
|
691 |
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
|
692 |
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
|
693 |
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
|
694 |
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
|
695 |
|
cf0817dfc787
reimplements chainall/chainfirst using [And/Or], implemenent search_selector function on Selector base class
sylvain.thenault@logilab.fr
parents:
686
diff
changeset
|
696 |
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
|
697 |
"""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
|
698 |
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
|
699 |
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
|
700 |
""" |
cf0817dfc787
reimplements chainall/chainfirst using [And/Or], implemenent search_selector function on Selector base class
sylvain.thenault@logilab.fr
parents:
686
diff
changeset
|
701 |
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
|
702 |
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
|
703 |
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
|
704 |
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
|
705 |
return selector |