author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 09 Jun 2009 18:46:20 +0200 | |
branch | stable |
changeset 2077 | 50beca829273 |
parent 2065 | f9f4bc048ec5 |
child 2078 | 362703bff3d4 |
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 |
|
1433 | 9 |
|
0 | 10 |
* the selection procedure has been generalized by delegating to a |
11 |
selector, which is responsible to score the vobject according to the |
|
12 |
current state (req, rset, row, col). At the end of the selection, if |
|
13 |
a vobject class has been found, an instance of this class is |
|
14 |
returned. The selector is instantiated at vobject registration |
|
15 |
||
16 |
||
17 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1943
diff
changeset
|
18 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 19 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1943
diff
changeset
|
20 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 21 |
""" |
22 |
__docformat__ = "restructuredtext en" |
|
23 |
||
24 |
import sys |
|
25 |
from os import listdir, stat |
|
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
26 |
from os.path import dirname, join, realpath, split, isdir, exists |
0 | 27 |
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
|
28 |
import types |
0 | 29 |
|
30 |
from cubicweb import CW_SOFTWARE_ROOT, set_log_methods |
|
31 |
from cubicweb import RegistryNotFound, ObjectNotFound, NoSelectableObject |
|
32 |
||
33 |
||
2025
010a4b0fe855
fix lookup of files to load when using CW_CUBES_PATH
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
34 |
def _toload_info(path, extrapath, _toload=None): |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
35 |
"""return a dictionary of <modname>: <modpath> and an ordered list of |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
36 |
(file, module name) to load |
0 | 37 |
""" |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
38 |
from logilab.common.modutils import modpath_from_file |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
39 |
if _toload is None: |
2025
010a4b0fe855
fix lookup of files to load when using CW_CUBES_PATH
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
40 |
assert isinstance(path, list) |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
41 |
_toload = {}, [] |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
42 |
for fileordir in path: |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
43 |
if isdir(fileordir) and exists(join(fileordir, '__init__.py')): |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
44 |
subfiles = [join(fileordir, fname) for fname in listdir(fileordir)] |
2025
010a4b0fe855
fix lookup of files to load when using CW_CUBES_PATH
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
45 |
_toload_info(subfiles, extrapath, _toload) |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
46 |
elif fileordir[-3:] == '.py': |
2025
010a4b0fe855
fix lookup of files to load when using CW_CUBES_PATH
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
47 |
modname = '.'.join(modpath_from_file(fileordir, extrapath)) |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
48 |
_toload[0][modname] = fileordir |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
49 |
_toload[1].append((fileordir, modname)) |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
50 |
return _toload |
0 | 51 |
|
52 |
||
53 |
class VObject(object): |
|
54 |
"""visual object, use to be handled somehow by the visual components |
|
55 |
registry. |
|
56 |
||
57 |
The following attributes should be set on concret vobject subclasses: |
|
1433 | 58 |
|
0 | 59 |
:__registry__: |
60 |
name of the registry for this object (string like 'views', |
|
61 |
'templates'...) |
|
62 |
:id: |
|
63 |
object's identifier in the registry (string like 'main', |
|
64 |
'primary', 'folder_box') |
|
65 |
:__select__: |
|
719 | 66 |
class'selector |
1433 | 67 |
|
0 | 68 |
Moreover, the `__abstract__` attribute may be set to True to indicate |
69 |
that a vobject is abstract and should not be registered |
|
70 |
""" |
|
71 |
# necessary attributes to interact with the registry |
|
72 |
id = None |
|
73 |
__registry__ = None |
|
74 |
__select__ = None |
|
75 |
||
76 |
@classmethod |
|
77 |
def registered(cls, registry): |
|
78 |
"""called by the registry when the vobject has been registered. |
|
79 |
||
80 |
It must return the object that will be actually registered (this |
|
81 |
may be the right hook to create an instance for example). By |
|
82 |
default the vobject is returned without any transformation. |
|
83 |
""" |
|
741 | 84 |
cls.build___select__() |
0 | 85 |
return cls |
86 |
||
87 |
@classmethod |
|
88 |
def selected(cls, *args, **kwargs): |
|
89 |
"""called by the registry when the vobject has been selected. |
|
1433 | 90 |
|
0 | 91 |
It must return the object that will be actually returned by the |
92 |
.select method (this may be the right hook to create an |
|
93 |
instance for example). By default the selected object is |
|
94 |
returned without any transformation. |
|
95 |
""" |
|
96 |
return cls |
|
97 |
||
98 |
@classmethod |
|
99 |
def classid(cls): |
|
100 |
"""returns a unique identifier for the vobject""" |
|
101 |
return '%s.%s' % (cls.__module__, cls.__name__) |
|
102 |
||
736
18c940e3f98d
need safety belt for class registered twice (during test for instance)
sylvain.thenault@logilab.fr
parents:
735
diff
changeset
|
103 |
# 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
|
104 |
@classmethod |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
105 |
def build___select__(cls): |
943
9fdfa8d38359
[selectors] for bacwkard compatibility, we must search the mro, not just the current class, in order not to miss __selectors__ in classes that do not redefine it
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
835
diff
changeset
|
106 |
for klass in cls.mro(): |
9fdfa8d38359
[selectors] for bacwkard compatibility, we must search the mro, not just the current class, in order not to miss __selectors__ in classes that do not redefine it
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
835
diff
changeset
|
107 |
if klass.__name__ == 'AppRsetObject': |
9fdfa8d38359
[selectors] for bacwkard compatibility, we must search the mro, not just the current class, in order not to miss __selectors__ in classes that do not redefine it
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
835
diff
changeset
|
108 |
continue # the bw compat __selector__ is there |
9fdfa8d38359
[selectors] for bacwkard compatibility, we must search the mro, not just the current class, in order not to miss __selectors__ in classes that do not redefine it
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
835
diff
changeset
|
109 |
klassdict = klass.__dict__ |
9fdfa8d38359
[selectors] for bacwkard compatibility, we must search the mro, not just the current class, in order not to miss __selectors__ in classes that do not redefine it
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
835
diff
changeset
|
110 |
if ('__select__' in klassdict and '__selectors__' in klassdict |
9fdfa8d38359
[selectors] for bacwkard compatibility, we must search the mro, not just the current class, in order not to miss __selectors__ in classes that do not redefine it
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
835
diff
changeset
|
111 |
and '__selgenerated__' not in klassdict): |
9fdfa8d38359
[selectors] for bacwkard compatibility, we must search the mro, not just the current class, in order not to miss __selectors__ in classes that do not redefine it
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
835
diff
changeset
|
112 |
raise TypeError("__select__ and __selectors__ can't be used together on class %s" % cls) |
9fdfa8d38359
[selectors] for bacwkard compatibility, we must search the mro, not just the current class, in order not to miss __selectors__ in classes that do not redefine it
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
835
diff
changeset
|
113 |
if '__selectors__' in klassdict and '__selgenerated__' not in klassdict: |
9fdfa8d38359
[selectors] for bacwkard compatibility, we must search the mro, not just the current class, in order not to miss __selectors__ in classes that do not redefine it
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
835
diff
changeset
|
114 |
cls.__selgenerated__ = True |
9fdfa8d38359
[selectors] for bacwkard compatibility, we must search the mro, not just the current class, in order not to miss __selectors__ in classes that do not redefine it
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
835
diff
changeset
|
115 |
# case where __selectors__ is defined locally (but __select__ |
9fdfa8d38359
[selectors] for bacwkard compatibility, we must search the mro, not just the current class, in order not to miss __selectors__ in classes that do not redefine it
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
835
diff
changeset
|
116 |
# is in a parent class) |
9fdfa8d38359
[selectors] for bacwkard compatibility, we must search the mro, not just the current class, in order not to miss __selectors__ in classes that do not redefine it
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
835
diff
changeset
|
117 |
selectors = klassdict['__selectors__'] |
9fdfa8d38359
[selectors] for bacwkard compatibility, we must search the mro, not just the current class, in order not to miss __selectors__ in classes that do not redefine it
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
835
diff
changeset
|
118 |
if len(selectors) == 1: |
9fdfa8d38359
[selectors] for bacwkard compatibility, we must search the mro, not just the current class, in order not to miss __selectors__ in classes that do not redefine it
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
835
diff
changeset
|
119 |
# micro optimization: don't bother with AndSelector if there's |
9fdfa8d38359
[selectors] for bacwkard compatibility, we must search the mro, not just the current class, in order not to miss __selectors__ in classes that do not redefine it
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
835
diff
changeset
|
120 |
# only one selector |
9fdfa8d38359
[selectors] for bacwkard compatibility, we must search the mro, not just the current class, in order not to miss __selectors__ in classes that do not redefine it
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
835
diff
changeset
|
121 |
select = _instantiate_selector(selectors[0]) |
9fdfa8d38359
[selectors] for bacwkard compatibility, we must search the mro, not just the current class, in order not to miss __selectors__ in classes that do not redefine it
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
835
diff
changeset
|
122 |
else: |
9fdfa8d38359
[selectors] for bacwkard compatibility, we must search the mro, not just the current class, in order not to miss __selectors__ in classes that do not redefine it
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
835
diff
changeset
|
123 |
select = AndSelector(*selectors) |
9fdfa8d38359
[selectors] for bacwkard compatibility, we must search the mro, not just the current class, in order not to miss __selectors__ in classes that do not redefine it
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
835
diff
changeset
|
124 |
cls.__select__ = select |
712
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
125 |
|
0 | 126 |
|
127 |
class VRegistry(object): |
|
128 |
"""class responsible to register, propose and select the various |
|
129 |
elements used to build the web interface. Currently, we have templates, |
|
130 |
views, actions and components. |
|
131 |
""" |
|
1433 | 132 |
|
0 | 133 |
def __init__(self, config):#, cache_size=1000): |
134 |
self.config = config |
|
135 |
# dictionnary of registry (themself dictionnary) by name |
|
136 |
self._registries = {} |
|
137 |
self._lastmodifs = {} |
|
138 |
||
139 |
def reset(self): |
|
140 |
self._registries = {} |
|
141 |
self._lastmodifs = {} |
|
142 |
||
143 |
def __getitem__(self, key): |
|
144 |
return self._registries[key] |
|
145 |
||
146 |
def get(self, key, default=None): |
|
147 |
return self._registries.get(key, default) |
|
148 |
||
149 |
def items(self): |
|
150 |
return self._registries.items() |
|
151 |
||
152 |
def values(self): |
|
153 |
return self._registries.values() |
|
154 |
||
155 |
def __contains__(self, key): |
|
156 |
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
|
157 |
|
0 | 158 |
def registry(self, name): |
159 |
"""return the registry (dictionary of class objects) associated to |
|
160 |
this name |
|
161 |
""" |
|
162 |
try: |
|
163 |
return self._registries[name] |
|
164 |
except KeyError: |
|
165 |
raise RegistryNotFound(name), None, sys.exc_info()[-1] |
|
166 |
||
167 |
def registry_objects(self, name, oid=None): |
|
168 |
"""returns objects registered with the given oid in the given registry. |
|
169 |
If no oid is given, return all objects in this registry |
|
170 |
""" |
|
171 |
registry = self.registry(name) |
|
172 |
if oid: |
|
173 |
try: |
|
174 |
return registry[oid] |
|
175 |
except KeyError: |
|
176 |
raise ObjectNotFound(oid), None, sys.exc_info()[-1] |
|
177 |
else: |
|
178 |
result = [] |
|
179 |
for objs in registry.values(): |
|
180 |
result += objs |
|
181 |
return result |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
182 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
183 |
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
|
184 |
"""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
|
185 |
objects = self[registry][cid] |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
186 |
assert len(objects) == 1, objects |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
187 |
return objects[0].selected(*args, **kwargs) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
188 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
189 |
# methods for explicit (un)registration ################################### |
668
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
190 |
|
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
191 |
# def clear(self, key): |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
192 |
# regname, oid = key.split('.') |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
193 |
# self[regname].pop(oid, None) |
695 | 194 |
def register_all(self, objects, modname, butclasses=()): |
195 |
for obj in objects: |
|
196 |
try: |
|
197 |
if obj.__module__ != modname or obj in butclasses: |
|
198 |
continue |
|
199 |
oid = obj.id |
|
200 |
except AttributeError: |
|
201 |
continue |
|
202 |
if oid: |
|
203 |
self.register(obj) |
|
1433 | 204 |
|
668
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
205 |
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
|
206 |
"""base method to add an object in the registry""" |
707 | 207 |
assert not '__abstract__' in obj.__dict__ |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
208 |
registryname = registryname or obj.__registry__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
209 |
oid = oid or obj.id |
739 | 210 |
assert oid |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
211 |
registry = self._registries.setdefault(registryname, {}) |
668
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
212 |
if clear: |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
213 |
vobjects = registry[oid] = [] |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
214 |
else: |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
215 |
vobjects = registry.setdefault(oid, []) |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
216 |
# 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
|
217 |
# as such because we need to compose registered in some cases |
682 | 218 |
vobject = obj.registered.im_func(obj, self) |
2077
50beca829273
nicer message on failure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2065
diff
changeset
|
219 |
assert not vobject in vobjects, vobject, \ |
50beca829273
nicer message on failure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2065
diff
changeset
|
220 |
'object %s is already registered' % vobject |
804 | 221 |
assert callable(vobject.__select__), vobject |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
222 |
vobjects.append(vobject) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
223 |
try: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
224 |
vname = vobject.__name__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
225 |
except AttributeError: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
226 |
vname = vobject.__class__.__name__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
227 |
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
|
228 |
vname, registryname, oid) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
229 |
# automatic reloading management |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
230 |
self._loadedmods[obj.__module__]['%s.%s' % (obj.__module__, oid)] = obj |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
231 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
232 |
def unregister(self, obj, registryname=None): |
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 |
registry = self.registry(registryname) |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
235 |
removed_id = obj.classid() |
2065
f9f4bc048ec5
fix unregister to be more error resilient as other unregistering methods + cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
236 |
for registered in registry.get(obj.id, ()): |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
237 |
# 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
|
238 |
# 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
|
239 |
if registered.classid() == removed_id: |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
240 |
# XXX automatic reloading management |
2065
f9f4bc048ec5
fix unregister to be more error resilient as other unregistering methods + cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
241 |
registry[obj.id].remove(registered) |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
242 |
break |
2065
f9f4bc048ec5
fix unregister to be more error resilient as other unregistering methods + cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
243 |
else: |
f9f4bc048ec5
fix unregister to be more error resilient as other unregistering methods + cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
244 |
self.warning('can\'t remove %s, no id %s in the %s registry', |
f9f4bc048ec5
fix unregister to be more error resilient as other unregistering methods + cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
245 |
removed_id, obj.id, registryname) |
1433 | 246 |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
247 |
def register_and_replace(self, obj, replaced, registryname=None): |
673 | 248 |
if hasattr(replaced, 'classid'): |
249 |
replaced = replaced.classid() |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
250 |
registryname = registryname or obj.__registry__ |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
251 |
registry = self.registry(registryname) |
1943
fb557b616c80
fix exception class, simply print warning when calling register_and_replace with an unregistered view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1641
diff
changeset
|
252 |
registered_objs = registry.get(obj.id, ()) |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
253 |
for index, registered in enumerate(registered_objs): |
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
254 |
if registered.classid() == replaced: |
673 | 255 |
del registry[obj.id][index] |
256 |
break |
|
1943
fb557b616c80
fix exception class, simply print warning when calling register_and_replace with an unregistered view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1641
diff
changeset
|
257 |
else: |
fb557b616c80
fix exception class, simply print warning when calling register_and_replace with an unregistered view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1641
diff
changeset
|
258 |
self.warning('trying to replace an unregistered view %s by %s', |
fb557b616c80
fix exception class, simply print warning when calling register_and_replace with an unregistered view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1641
diff
changeset
|
259 |
replaced, obj) |
673 | 260 |
self.register(obj, registryname=registryname) |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
261 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
262 |
# dynamic selection methods ############################################### |
1433 | 263 |
|
0 | 264 |
def select(self, vobjects, *args, **kwargs): |
265 |
"""return an instance of the most specific object according |
|
266 |
to parameters |
|
267 |
||
268 |
raise NoSelectableObject if not object apply |
|
269 |
""" |
|
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
|
270 |
score, winners = 0, [] |
0 | 271 |
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
|
272 |
vobjectscore = vobject.__select__(vobject, *args, **kwargs) |
0 | 273 |
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
|
274 |
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
|
275 |
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
|
276 |
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
|
277 |
if not winners: |
0 | 278 |
raise NoSelectableObject('args: %s\nkwargs: %s %s' |
1149 | 279 |
% (args, kwargs.keys(), |
280 |
[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
|
281 |
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
|
282 |
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
|
283 |
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
|
284 |
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
|
285 |
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
|
286 |
raise Exception('select ambiguity, args: %s\nkwargs: %s %s' |
1149 | 287 |
% (args, kwargs.keys(), |
288 |
[repr(v) for v in winners])) |
|
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
|
289 |
winner = winners[0] |
0 | 290 |
# return the result of the .selected method of the vobject |
291 |
return winner.selected(*args, **kwargs) |
|
1433 | 292 |
|
0 | 293 |
def possible_objects(self, registry, *args, **kwargs): |
294 |
"""return an iterator on possible objects in a registry for this result set |
|
295 |
||
296 |
actions returned are classes, not instances |
|
297 |
""" |
|
298 |
for vobjects in self.registry(registry).values(): |
|
299 |
try: |
|
300 |
yield self.select(vobjects, *args, **kwargs) |
|
301 |
except NoSelectableObject: |
|
302 |
continue |
|
303 |
||
304 |
def select_object(self, registry, cid, *args, **kwargs): |
|
305 |
"""return the most specific component according to the resultset""" |
|
306 |
return self.select(self.registry_objects(registry, cid), *args, **kwargs) |
|
1433 | 307 |
|
0 | 308 |
# intialization methods ################################################### |
1433 | 309 |
|
2035
946ed7349e1a
bugfix: extrapath is optional arg for init_registration
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2025
diff
changeset
|
310 |
def init_registration(self, path, extrapath=None): |
1316
6d71d38822ee
introduce init_registration method and call it in repo initialization
sylvain.thenault@logilab.fr
parents:
1310
diff
changeset
|
311 |
# compute list of all modules that have to be loaded |
2025
010a4b0fe855
fix lookup of files to load when using CW_CUBES_PATH
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
312 |
self._toloadmods, filemods = _toload_info(path, extrapath) |
1316
6d71d38822ee
introduce init_registration method and call it in repo initialization
sylvain.thenault@logilab.fr
parents:
1310
diff
changeset
|
313 |
self._loadedmods = {} |
6d71d38822ee
introduce init_registration method and call it in repo initialization
sylvain.thenault@logilab.fr
parents:
1310
diff
changeset
|
314 |
return filemods |
1433 | 315 |
|
2025
010a4b0fe855
fix lookup of files to load when using CW_CUBES_PATH
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
316 |
def register_objects(self, path, force_reload=None, extrapath=None): |
0 | 317 |
if force_reload is None: |
318 |
force_reload = self.config.mode == 'dev' |
|
319 |
elif not force_reload: |
|
320 |
# force_reload == False usually mean modules have been reloaded |
|
321 |
# by another connection, so we want to update the registry |
|
322 |
# content even if there has been no module content modification |
|
323 |
self.reset() |
|
324 |
# need to clean sys.path this to avoid import confusion pb (i.e. |
|
325 |
# having the same module loaded as 'cubicweb.web.views' subpackage and |
|
326 |
# as views' or 'web.views' subpackage |
|
327 |
# this is mainly for testing purpose, we should'nt need this in |
|
328 |
# production environment |
|
329 |
for webdir in (join(dirname(realpath(__file__)), 'web'), |
|
330 |
join(dirname(__file__), 'web')): |
|
331 |
if webdir in sys.path: |
|
332 |
sys.path.remove(webdir) |
|
333 |
if CW_SOFTWARE_ROOT in sys.path: |
|
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
334 |
sys.path.remove(CW_SOFTWARE_ROOT) |
0 | 335 |
# load views from each directory in the application's path |
2025
010a4b0fe855
fix lookup of files to load when using CW_CUBES_PATH
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
336 |
filemods = self.init_registration(path, extrapath) |
0 | 337 |
change = False |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
338 |
for filepath, modname in filemods: |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
339 |
if self.load_file(filepath, modname, force_reload): |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
340 |
change = True |
0 | 341 |
return change |
1433 | 342 |
|
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
343 |
def load_file(self, filepath, modname, force_reload=False): |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
344 |
"""load visual objects from a python file""" |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
345 |
from logilab.common.modutils import load_module_from_name |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
346 |
if modname in self._loadedmods: |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
347 |
return |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
348 |
self._loadedmods[modname] = {} |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
349 |
try: |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
350 |
modified_on = stat(filepath)[-2] |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
351 |
except OSError: |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
352 |
# this typically happens on emacs backup files (.#foo.py) |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
353 |
self.warning('Unable to load %s. It is likely to be a backup file', |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
354 |
filepath) |
0 | 355 |
return False |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
356 |
if filepath in self._lastmodifs: |
0 | 357 |
# only load file if it was modified |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
358 |
if modified_on <= self._lastmodifs[filepath]: |
0 | 359 |
return |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
360 |
# if it was modified, unregister all exisiting objects |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
361 |
# from this module, and keep track of what was unregistered |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
362 |
unregistered = self.unregister_module_vobjects(modname) |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
363 |
else: |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
364 |
unregistered = None |
0 | 365 |
# load the module |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
366 |
module = load_module_from_name(modname, use_sys=not force_reload) |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
367 |
self.load_module(module) |
0 | 368 |
# if something was unregistered, we need to update places where it was |
1433 | 369 |
# referenced |
0 | 370 |
if unregistered: |
371 |
# oldnew_mapping = {} |
|
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
372 |
registered = self._loadedmods[modname] |
0 | 373 |
oldnew_mapping = dict((unregistered[name], registered[name]) |
374 |
for name in unregistered if name in registered) |
|
375 |
self.update_registered_subclasses(oldnew_mapping) |
|
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
376 |
self._lastmodifs[filepath] = modified_on |
0 | 377 |
return True |
378 |
||
379 |
def load_module(self, module): |
|
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
380 |
self.info('loading %s', module) |
668
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
381 |
if hasattr(module, 'registration_callback'): |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
382 |
module.registration_callback(self) |
660
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
652
diff
changeset
|
383 |
else: |
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
652
diff
changeset
|
384 |
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
|
385 |
if objname.startswith('_'): |
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
652
diff
changeset
|
386 |
continue |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
387 |
self._load_ancestors_then_object(module.__name__, obj) |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
388 |
self.debug('loaded %s', module) |
1433 | 389 |
|
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
390 |
def _load_ancestors_then_object(self, modname, obj): |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
391 |
# imported classes |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
392 |
objmodname = getattr(obj, '__module__', None) |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
393 |
if objmodname != modname: |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
394 |
if objmodname in self._toloadmods: |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
395 |
self.load_file(self._toloadmods[objmodname], objmodname) |
0 | 396 |
return |
397 |
# skip non registerable object |
|
398 |
try: |
|
399 |
if not issubclass(obj, VObject): |
|
400 |
return |
|
401 |
except TypeError: |
|
402 |
return |
|
403 |
objname = '%s.%s' % (modname, obj.__name__) |
|
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
404 |
if objname in self._loadedmods[modname]: |
0 | 405 |
return |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
406 |
self._loadedmods[modname][objname] = obj |
0 | 407 |
for parent in obj.__bases__: |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
408 |
self._load_ancestors_then_object(modname, parent) |
0 | 409 |
self.load_object(obj) |
1433 | 410 |
|
0 | 411 |
def load_object(self, obj): |
412 |
try: |
|
413 |
self.register_vobject_class(obj) |
|
414 |
except Exception, ex: |
|
415 |
if self.config.mode in ('test', 'dev'): |
|
416 |
raise |
|
417 |
self.exception('vobject %s registration failed: %s', obj, ex) |
|
1433 | 418 |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
419 |
# old automatic registration XXX deprecated ############################### |
1433 | 420 |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
421 |
def register_vobject_class(self, cls): |
0 | 422 |
"""handle vobject class registration |
1433 | 423 |
|
0 | 424 |
vobject class with __abstract__ == True in their local dictionnary or |
425 |
with a name starting starting by an underscore are not registered. |
|
426 |
Also a vobject class needs to have __registry__ and id attributes set |
|
427 |
to a non empty string to be registered. |
|
428 |
""" |
|
429 |
if (cls.__dict__.get('__abstract__') or cls.__name__[0] == '_' |
|
430 |
or not cls.__registry__ or not cls.id): |
|
431 |
return |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
432 |
regname = cls.__registry__ |
1046
52ee022d87e3
simplify registry options to disable some appobjects to use a single option
sylvain.thenault@logilab.fr
parents:
946
diff
changeset
|
433 |
if '%s.%s' % (regname, cls.id) in self.config['disable-appobjects']: |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
434 |
return |
1432
2c3711d4570b
drop hopeless registerer bw compat
sylvain.thenault@logilab.fr
parents:
1316
diff
changeset
|
435 |
self.register(cls) |
1433 | 436 |
|
0 | 437 |
def unregister_module_vobjects(self, modname): |
438 |
"""removes registered objects coming from a given module |
|
439 |
||
440 |
returns a dictionnary classid/class of all classes that will need |
|
441 |
to be updated after reload (i.e. vobjects referencing classes defined |
|
442 |
in the <modname> module) |
|
443 |
""" |
|
444 |
unregistered = {} |
|
445 |
# browse each registered object |
|
446 |
for registry, objdict in self.items(): |
|
447 |
for oid, objects in objdict.items(): |
|
448 |
for obj in objects[:]: |
|
449 |
objname = obj.classid() |
|
450 |
# if the vobject is defined in this module, remove it |
|
451 |
if objname.startswith(modname): |
|
452 |
unregistered[objname] = obj |
|
453 |
objects.remove(obj) |
|
454 |
self.debug('unregistering %s in %s registry', |
|
455 |
objname, registry) |
|
456 |
# if not, check if the vobject can be found in baseclasses |
|
457 |
# (because we also want subclasses to be updated) |
|
458 |
else: |
|
459 |
if not isinstance(obj, type): |
|
460 |
obj = obj.__class__ |
|
461 |
for baseclass in obj.__bases__: |
|
462 |
if hasattr(baseclass, 'classid'): |
|
463 |
baseclassid = baseclass.classid() |
|
464 |
if baseclassid.startswith(modname): |
|
465 |
unregistered[baseclassid] = baseclass |
|
466 |
# update oid entry |
|
467 |
if objects: |
|
468 |
objdict[oid] = objects |
|
469 |
else: |
|
470 |
del objdict[oid] |
|
471 |
return unregistered |
|
472 |
||
473 |
def update_registered_subclasses(self, oldnew_mapping): |
|
474 |
"""updates subclasses of re-registered vobjects |
|
475 |
||
476 |
if baseviews.PrimaryView is changed, baseviews.py will be reloaded |
|
477 |
automatically and the new version of PrimaryView will be registered. |
|
478 |
But all existing subclasses must also be notified of this change, and |
|
479 |
that's what this method does |
|
480 |
||
481 |
:param oldnew_mapping: a dict mapping old version of a class to |
|
482 |
the new version |
|
483 |
""" |
|
484 |
# browse each registered object |
|
485 |
for objdict in self.values(): |
|
486 |
for objects in objdict.values(): |
|
487 |
for obj in objects: |
|
488 |
if not isinstance(obj, type): |
|
489 |
obj = obj.__class__ |
|
490 |
# build new baseclasses tuple |
|
491 |
newbases = tuple(oldnew_mapping.get(baseclass, baseclass) |
|
492 |
for baseclass in obj.__bases__) |
|
493 |
# update obj's baseclasses tuple (__bases__) if needed |
|
494 |
if newbases != obj.__bases__: |
|
495 |
self.debug('updating %s.%s base classes', |
|
496 |
obj.__module__, obj.__name__) |
|
497 |
obj.__bases__ = newbases |
|
498 |
||
1433 | 499 |
# init logging |
0 | 500 |
set_log_methods(VObject, getLogger('cubicweb')) |
501 |
set_log_methods(VRegistry, getLogger('cubicweb.registry')) |
|
502 |
||
503 |
||
630
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
504 |
# 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
|
505 |
|
630
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
506 |
class Selector(object): |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
507 |
"""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
|
508 |
for operators ``&`` and ``|`` |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
509 |
|
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
510 |
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
|
511 |
selector logic itself should be implemented in the __call__ method |
0 | 512 |
|
513 |
||
718
f7011679437a
doc update, move yes_registerer here
sylvain.thenault@logilab.fr
parents:
714
diff
changeset
|
514 |
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
|
515 |
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
|
516 |
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
|
517 |
|
f7011679437a
doc update, move yes_registerer here
sylvain.thenault@logilab.fr
parents:
714
diff
changeset
|
518 |
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
|
519 |
""" |
698
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
520 |
|
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
|
521 |
@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
|
522 |
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
|
523 |
# 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
|
524 |
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
|
525 |
|
698
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
526 |
def search_selector(self, selector): |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
527 |
"""search for the given selector or selector instance in the selectors |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
528 |
tree. Return it of None if not found |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
529 |
""" |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
530 |
if self is selector: |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
531 |
return self |
721 | 532 |
if isinstance(selector, type) and isinstance(self, selector): |
698
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
533 |
return self |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
534 |
return None |
780
5b6c93816871
fix selector search and refactor auto-instantiation
sylvain.thenault@logilab.fr
parents:
760
diff
changeset
|
535 |
|
5b6c93816871
fix selector search and refactor auto-instantiation
sylvain.thenault@logilab.fr
parents:
760
diff
changeset
|
536 |
def __str__(self): |
5b6c93816871
fix selector search and refactor auto-instantiation
sylvain.thenault@logilab.fr
parents:
760
diff
changeset
|
537 |
return self.__class__.__name__ |
1433 | 538 |
|
698
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
539 |
def __and__(self, other): |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
540 |
return AndSelector(self, other) |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
541 |
def __rand__(self, other): |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
542 |
return AndSelector(other, self) |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
543 |
|
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
544 |
def __or__(self, other): |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
545 |
return OrSelector(self, other) |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
546 |
def __ror__(self, other): |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
547 |
return OrSelector(other, self) |
827
3f08481e6e51
provide negation operator for selectors, unfortunately, it's not possible to user the python keyword 'not'
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
804
diff
changeset
|
548 |
|
3f08481e6e51
provide negation operator for selectors, unfortunately, it's not possible to user the python keyword 'not'
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
804
diff
changeset
|
549 |
def __invert__(self): |
3f08481e6e51
provide negation operator for selectors, unfortunately, it's not possible to user the python keyword 'not'
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
804
diff
changeset
|
550 |
return NotSelector(self) |
1433 | 551 |
|
698
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
552 |
# XXX (function | function) or (function & function) not managed yet |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
553 |
|
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
554 |
def __call__(self, cls, *args, **kwargs): |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
555 |
return NotImplementedError("selector %s must implement its logic " |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
556 |
"in its __call__ method" % self.__class__) |
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
557 |
|
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
558 |
class MultiSelector(Selector): |
718
f7011679437a
doc update, move yes_registerer here
sylvain.thenault@logilab.fr
parents:
714
diff
changeset
|
559 |
"""base class for compound selector classes""" |
1433 | 560 |
|
630
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
561 |
def __init__(self, *selectors): |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
562 |
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
|
563 |
|
780
5b6c93816871
fix selector search and refactor auto-instantiation
sylvain.thenault@logilab.fr
parents:
760
diff
changeset
|
564 |
def __str__(self): |
5b6c93816871
fix selector search and refactor auto-instantiation
sylvain.thenault@logilab.fr
parents:
760
diff
changeset
|
565 |
return '%s(%s)' % (self.__class__.__name__, |
5b6c93816871
fix selector search and refactor auto-instantiation
sylvain.thenault@logilab.fr
parents:
760
diff
changeset
|
566 |
','.join(str(s) for s in self.selectors)) |
5b6c93816871
fix selector search and refactor auto-instantiation
sylvain.thenault@logilab.fr
parents:
760
diff
changeset
|
567 |
|
630
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
568 |
@classmethod |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
569 |
def merge_selectors(cls, selectors): |
780
5b6c93816871
fix selector search and refactor auto-instantiation
sylvain.thenault@logilab.fr
parents:
760
diff
changeset
|
570 |
"""deal with selector instanciation when necessary and merge |
5b6c93816871
fix selector search and refactor auto-instantiation
sylvain.thenault@logilab.fr
parents:
760
diff
changeset
|
571 |
multi-selectors if possible: |
630
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
572 |
|
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
573 |
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
|
574 |
==> 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
|
575 |
""" |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
576 |
merged_selectors = [] |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
577 |
for selector in selectors: |
946
a130b5ceeca9
[selectors] _needs real fix_ whack 'till it works for me
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
943
diff
changeset
|
578 |
try: |
a130b5ceeca9
[selectors] _needs real fix_ whack 'till it works for me
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
943
diff
changeset
|
579 |
selector = _instantiate_selector(selector) |
a130b5ceeca9
[selectors] _needs real fix_ whack 'till it works for me
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
943
diff
changeset
|
580 |
except: |
a130b5ceeca9
[selectors] _needs real fix_ whack 'till it works for me
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
943
diff
changeset
|
581 |
pass |
a130b5ceeca9
[selectors] _needs real fix_ whack 'till it works for me
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
943
diff
changeset
|
582 |
#assert isinstance(selector, Selector), selector |
630
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
583 |
if isinstance(selector, cls): |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
584 |
merged_selectors += selector.selectors |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
585 |
else: |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
586 |
merged_selectors.append(selector) |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
587 |
return merged_selectors |
0 | 588 |
|
693
cf0817dfc787
reimplements chainall/chainfirst using [And/Or], implemenent search_selector function on Selector base class
sylvain.thenault@logilab.fr
parents:
686
diff
changeset
|
589 |
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
|
590 |
"""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
|
591 |
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
|
592 |
""" |
cf0817dfc787
reimplements chainall/chainfirst using [And/Or], implemenent search_selector function on Selector base class
sylvain.thenault@logilab.fr
parents:
686
diff
changeset
|
593 |
for childselector in self.selectors: |
780
5b6c93816871
fix selector search and refactor auto-instantiation
sylvain.thenault@logilab.fr
parents:
760
diff
changeset
|
594 |
if childselector is selector: |
5b6c93816871
fix selector search and refactor auto-instantiation
sylvain.thenault@logilab.fr
parents:
760
diff
changeset
|
595 |
return childselector |
5b6c93816871
fix selector search and refactor auto-instantiation
sylvain.thenault@logilab.fr
parents:
760
diff
changeset
|
596 |
found = childselector.search_selector(selector) |
5b6c93816871
fix selector search and refactor auto-instantiation
sylvain.thenault@logilab.fr
parents:
760
diff
changeset
|
597 |
if found is not None: |
5b6c93816871
fix selector search and refactor auto-instantiation
sylvain.thenault@logilab.fr
parents:
760
diff
changeset
|
598 |
return found |
693
cf0817dfc787
reimplements chainall/chainfirst using [And/Or], implemenent search_selector function on Selector base class
sylvain.thenault@logilab.fr
parents:
686
diff
changeset
|
599 |
return None |
630
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
600 |
|
1433 | 601 |
|
697
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
695
diff
changeset
|
602 |
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
|
603 |
"""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
|
604 |
would be overkill:: |
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
695
diff
changeset
|
605 |
|
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
695
diff
changeset
|
606 |
@objectify_selector |
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
695
diff
changeset
|
607 |
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
|
608 |
return 1 |
1433 | 609 |
|
697
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
695
diff
changeset
|
610 |
""" |
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
695
diff
changeset
|
611 |
return type(selector_func.__name__, (Selector,), |
734 | 612 |
{'__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
|
613 |
|
712
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
614 |
def _instantiate_selector(selector): |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
615 |
"""ensures `selector` is a `Selector` instance |
1433 | 616 |
|
712
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
617 |
NOTE: This should only be used locally in build___select__() |
946
a130b5ceeca9
[selectors] _needs real fix_ whack 'till it works for me
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
943
diff
changeset
|
618 |
XXX: then, why not do it ?? |
712
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
619 |
""" |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
620 |
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
|
621 |
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
|
622 |
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
|
623 |
return selector() |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
624 |
return selector |
ce49e3885453
remove autoselectors metaclass, __select__ is built during registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
711
diff
changeset
|
625 |
|
630
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
626 |
|
698
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
627 |
class AndSelector(MultiSelector): |
630
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
628 |
"""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
|
629 |
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
|
630 |
score = 0 |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
631 |
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
|
632 |
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
|
633 |
if not partscore: |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
634 |
return 0 |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
635 |
score += partscore |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
636 |
return score |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
637 |
|
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
638 |
|
698
7dfd03e9e810
introduce MultiSelector base class
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
639 |
class OrSelector(MultiSelector): |
630
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
640 |
"""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
|
641 |
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
|
642 |
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
|
643 |
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
|
644 |
if partscore: |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
645 |
return partscore |
66ff0b2f7d03
simple test implementation for binary operators on selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
615
diff
changeset
|
646 |
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
|
647 |
|
827
3f08481e6e51
provide negation operator for selectors, unfortunately, it's not possible to user the python keyword 'not'
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
804
diff
changeset
|
648 |
class NotSelector(Selector): |
3f08481e6e51
provide negation operator for selectors, unfortunately, it's not possible to user the python keyword 'not'
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
804
diff
changeset
|
649 |
"""negation selector""" |
3f08481e6e51
provide negation operator for selectors, unfortunately, it's not possible to user the python keyword 'not'
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
804
diff
changeset
|
650 |
def __init__(self, selector): |
3f08481e6e51
provide negation operator for selectors, unfortunately, it's not possible to user the python keyword 'not'
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
804
diff
changeset
|
651 |
self.selector = selector |
3f08481e6e51
provide negation operator for selectors, unfortunately, it's not possible to user the python keyword 'not'
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
804
diff
changeset
|
652 |
|
3f08481e6e51
provide negation operator for selectors, unfortunately, it's not possible to user the python keyword 'not'
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
804
diff
changeset
|
653 |
def __call__(self, cls, *args, **kwargs): |
3f08481e6e51
provide negation operator for selectors, unfortunately, it's not possible to user the python keyword 'not'
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
804
diff
changeset
|
654 |
score = self.selector(cls, *args, **kwargs) |
835
7dcb11dd443e
fix relation_possible, ensure we return int
sylvain.thenault@logilab.fr
parents:
827
diff
changeset
|
655 |
return int(not score) |
827
3f08481e6e51
provide negation operator for selectors, unfortunately, it's not possible to user the python keyword 'not'
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
804
diff
changeset
|
656 |
|
3f08481e6e51
provide negation operator for selectors, unfortunately, it's not possible to user the python keyword 'not'
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
804
diff
changeset
|
657 |
def __str__(self): |
3f08481e6e51
provide negation operator for selectors, unfortunately, it's not possible to user the python keyword 'not'
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
804
diff
changeset
|
658 |
return 'NOT(%s)' % super(NotSelector, self).__str__() |
693
cf0817dfc787
reimplements chainall/chainfirst using [And/Or], implemenent search_selector function on Selector base class
sylvain.thenault@logilab.fr
parents:
686
diff
changeset
|
659 |
|
835
7dcb11dd443e
fix relation_possible, ensure we return int
sylvain.thenault@logilab.fr
parents:
827
diff
changeset
|
660 |
|
7dcb11dd443e
fix relation_possible, ensure we return int
sylvain.thenault@logilab.fr
parents:
827
diff
changeset
|
661 |
# XXX bw compat functions ##################################################### |
693
cf0817dfc787
reimplements chainall/chainfirst using [And/Or], implemenent search_selector function on Selector base class
sylvain.thenault@logilab.fr
parents:
686
diff
changeset
|
662 |
|
cf0817dfc787
reimplements chainall/chainfirst using [And/Or], implemenent search_selector function on Selector base class
sylvain.thenault@logilab.fr
parents:
686
diff
changeset
|
663 |
def chainall(*selectors, **kwargs): |
0 | 664 |
"""return a selector chaining given selectors. If one of |
665 |
the selectors fail, selection will fail, else the returned score |
|
666 |
will be the sum of each selector'score |
|
667 |
""" |
|
668 |
assert selectors |
|
759
e044f28372bd
chainall / chainfirst ensures selectors are instantiated
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
741
diff
changeset
|
669 |
# XXX do we need to create the AndSelector here, a tuple might be enough |
780
5b6c93816871
fix selector search and refactor auto-instantiation
sylvain.thenault@logilab.fr
parents:
760
diff
changeset
|
670 |
selector = AndSelector(*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
|
671 |
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
|
672 |
selector.__name__ = kwargs['name'] |
0 | 673 |
return selector |
674 |
||
693
cf0817dfc787
reimplements chainall/chainfirst using [And/Or], implemenent search_selector function on Selector base class
sylvain.thenault@logilab.fr
parents:
686
diff
changeset
|
675 |
def chainfirst(*selectors, **kwargs): |
0 | 676 |
"""return a selector chaining given selectors. If all |
677 |
the selectors fail, selection will fail, else the returned score |
|
678 |
will be the first non-zero selector score |
|
679 |
""" |
|
680 |
assert selectors |
|
780
5b6c93816871
fix selector search and refactor auto-instantiation
sylvain.thenault@logilab.fr
parents:
760
diff
changeset
|
681 |
selector = OrSelector(*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
|
682 |
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
|
683 |
selector.__name__ = kwargs['name'] |
0 | 684 |
return selector |