author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 03 Aug 2009 16:32:30 +0200 | |
changeset 2662 | 87aed8cb7ff6 |
parent 2658 | 5535857eeaa5 |
child 2689 | 44f041222d0f |
permissions | -rw-r--r-- |
0 | 1 |
""" |
2308
b478c3a8ad2a
typos, cleanup, lighter traced seletion output
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2173
diff
changeset
|
2 |
* the vregistry handles various types of objects interacting |
b478c3a8ad2a
typos, cleanup, lighter traced seletion output
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2173
diff
changeset
|
3 |
together. The vregistry handles registration of dynamically loaded |
b478c3a8ad2a
typos, cleanup, lighter traced seletion output
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2173
diff
changeset
|
4 |
objects and provides a convenient api to access those objects |
0 | 5 |
according to a context |
6 |
||
2308
b478c3a8ad2a
typos, cleanup, lighter traced seletion output
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2173
diff
changeset
|
7 |
* to interact with the vregistry, objects should inherit from the |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
8 |
AppObject abstract class |
1433 | 9 |
|
0 | 10 |
* the selection procedure has been generalized by delegating to a |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
11 |
selector, which is responsible to score the appobject according to the |
0 | 12 |
current state (req, rset, row, col). At the end of the selection, if |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
13 |
a appobject class has been found, an instance of this class is |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
14 |
returned. The selector is instantiated at appobject registration |
0 | 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 |
2083
f8f94c2951d3
add warning when rset isn't a named argument in select()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
28 |
from warnings import warn |
0 | 29 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
30 |
from logilab.common.deprecation import deprecated, class_moved |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
31 |
from logilab.common.logging_ext import set_log_methods |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
32 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
33 |
from cubicweb import CW_SOFTWARE_ROOT |
2651
3ad936634d2a
[registry] when a source file is changed, reset and reload the whole registry
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2589
diff
changeset
|
34 |
from cubicweb import (RegistryNotFound, ObjectNotFound, NoSelectableObject, |
3ad936634d2a
[registry] when a source file is changed, reset and reload the whole registry
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2589
diff
changeset
|
35 |
RegistryOutOfDate) |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
36 |
from cubicweb.appobject import AppObject |
0 | 37 |
|
2653
51bf32bbe78d
[uicfg] reload uicfg when vregistry is being reset
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2651
diff
changeset
|
38 |
# XXX depending on cubicweb.web is ugly, we should deal with uicfg |
51bf32bbe78d
[uicfg] reload uicfg when vregistry is being reset
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2651
diff
changeset
|
39 |
# reset with a good old event / callback system |
51bf32bbe78d
[uicfg] reload uicfg when vregistry is being reset
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2651
diff
changeset
|
40 |
try: |
51bf32bbe78d
[uicfg] reload uicfg when vregistry is being reset
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2651
diff
changeset
|
41 |
from cubicweb.web import uicfg |
51bf32bbe78d
[uicfg] reload uicfg when vregistry is being reset
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2651
diff
changeset
|
42 |
except ImportError: # cubicweb.web not installed |
51bf32bbe78d
[uicfg] reload uicfg when vregistry is being reset
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2651
diff
changeset
|
43 |
uicfg = None |
51bf32bbe78d
[uicfg] reload uicfg when vregistry is being reset
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2651
diff
changeset
|
44 |
|
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 |
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
|
46 |
"""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
|
47 |
(file, module name) to load |
0 | 48 |
""" |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
49 |
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
|
50 |
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
|
51 |
assert isinstance(path, list) |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
52 |
_toload = {}, [] |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
53 |
for fileordir in path: |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
54 |
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
|
55 |
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
|
56 |
_toload_info(subfiles, extrapath, _toload) |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
57 |
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
|
58 |
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
|
59 |
_toload[0][modname] = fileordir |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
60 |
_toload[1].append((fileordir, modname)) |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
61 |
return _toload |
0 | 62 |
|
63 |
||
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
64 |
class Registry(dict): |
0 | 65 |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
66 |
def __init__(self, config): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
67 |
super(Registry, self).__init__() |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
68 |
self.config = config |
0 | 69 |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
70 |
def __getitem__(self, name): |
0 | 71 |
"""return the registry (dictionary of class objects) associated to |
72 |
this name |
|
73 |
""" |
|
74 |
try: |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
75 |
return super(Registry, self).__getitem__(name) |
0 | 76 |
except KeyError: |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
77 |
raise ObjectNotFound(name), None, sys.exc_info()[-1] |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
78 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
79 |
def register(self, obj, oid=None, clear=False): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
80 |
"""base method to add an object in the registry""" |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
81 |
assert not '__abstract__' in obj.__dict__ |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
82 |
oid = oid or obj.id |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
83 |
assert oid |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
84 |
if clear: |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
85 |
appobjects = self[oid] = [] |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
86 |
else: |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
87 |
appobjects = self.setdefault(oid, []) |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
88 |
# registered() is technically a classmethod but is not declared |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
89 |
# as such because we need to compose registered in some cases |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
90 |
appobject = obj.registered.im_func(obj, self) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
91 |
assert not appobject in appobjects, \ |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
92 |
'object %s is already registered' % appobject |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
93 |
assert callable(appobject.__select__), appobject |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
94 |
appobjects.append(appobject) |
0 | 95 |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
96 |
def register_and_replace(self, obj, replaced): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
97 |
# XXXFIXME this is a duplication of unregister() |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
98 |
# remove register_and_replace in favor of unregister + register |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
99 |
# or simplify by calling unregister then register here |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
100 |
if hasattr(replaced, 'classid'): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
101 |
replaced = replaced.classid() |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
102 |
registered_objs = self.get(obj.id, ()) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
103 |
for index, registered in enumerate(registered_objs): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
104 |
if registered.classid() == replaced: |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
105 |
del registered_objs[index] |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
106 |
break |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
107 |
else: |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
108 |
self.warning('trying to replace an unregistered view %s by %s', |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
109 |
replaced, obj) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
110 |
self.register(obj) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
111 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
112 |
def unregister(self, obj): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
113 |
oid = obj.classid() |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
114 |
for registered in self.get(obj.id, ()): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
115 |
# use classid() to compare classes because vreg will probably |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
116 |
# have its own version of the class, loaded through execfile |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
117 |
if registered.classid() == oid: |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
118 |
# XXX automatic reloading management |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
119 |
self[obj.id].remove(registered) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
120 |
break |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
121 |
else: |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
122 |
self.warning('can\'t remove %s, no id %s in the registry', |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
123 |
oid, obj.id) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
124 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
125 |
def all_objects(self): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
126 |
"""return a list containing all objects in this registry. |
0 | 127 |
""" |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
128 |
result = [] |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
129 |
for objs in self.values(): |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
130 |
result += objs |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
131 |
return result |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
132 |
|
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
133 |
# dynamic selection methods ################################################ |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
134 |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
135 |
def object_by_id(self, oid, *args, **kwargs): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
136 |
"""return object with the given oid. Only one object is expected to be |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
137 |
found. |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
138 |
|
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
139 |
raise `ObjectNotFound` if not object with id <oid> in <registry> |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
140 |
raise `AssertionError` if there is more than one object there |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
141 |
""" |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
142 |
objects = self[oid] |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
143 |
assert len(objects) == 1, objects |
2658
5535857eeaa5
[appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
144 |
return objects[0](*args, **kwargs) |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
145 |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
146 |
def select(self, oid, *args, **kwargs): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
147 |
"""return the most specific object among those with the given oid |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
148 |
according to the given context. |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
149 |
|
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
150 |
raise `ObjectNotFound` if not object with id <oid> in <registry> |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
151 |
raise `NoSelectableObject` if not object apply |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
152 |
""" |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
153 |
return self.select_best(self[oid], *args, **kwargs) |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
154 |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
155 |
def select_object(self, oid, *args, **kwargs): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
156 |
"""return the most specific object among those with the given oid |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
157 |
according to the given context, or None if no object applies. |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
158 |
""" |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
159 |
try: |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
160 |
return self.select(oid, *args, **kwargs) |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
161 |
except (NoSelectableObject, ObjectNotFound): |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
162 |
return None |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
163 |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
164 |
def possible_objects(self, *args, **kwargs): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
165 |
"""return an iterator on possible objects in this registry for the given |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
166 |
context |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
167 |
""" |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
168 |
for appobjects in self.itervalues(): |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
169 |
try: |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
170 |
yield self.select_best(appobjects, *args, **kwargs) |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
171 |
except NoSelectableObject: |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
172 |
continue |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
173 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
174 |
def select_best(self, appobjects, *args, **kwargs): |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
175 |
"""return an instance of the most specific object according |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
176 |
to parameters |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
177 |
|
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
178 |
raise `NoSelectableObject` if not object apply |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
179 |
""" |
2083
f8f94c2951d3
add warning when rset isn't a named argument in select()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
180 |
if len(args) > 1: |
f8f94c2951d3
add warning when rset isn't a named argument in select()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2058
diff
changeset
|
181 |
warn('only the request param can not be named when calling select', |
2302
e0393451e9a5
fix stacklevel
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2181
diff
changeset
|
182 |
DeprecationWarning, stacklevel=3) |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
183 |
score, winners = 0, [] |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
184 |
for appobject in appobjects: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
185 |
appobjectscore = appobject.__select__(appobject, *args, **kwargs) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
186 |
if appobjectscore > score: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
187 |
score, winners = appobjectscore, [appobject] |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
188 |
elif appobjectscore > 0 and appobjectscore == score: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
189 |
winners.append(appobject) |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
190 |
if not winners: |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
191 |
raise NoSelectableObject('args: %s\nkwargs: %s %s' |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
192 |
% (args, kwargs.keys(), |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
193 |
[repr(v) for v in appobjects])) |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
194 |
if len(winners) > 1: |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
195 |
if self.config.mode == 'installed': |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
196 |
self.error('select ambiguity, args: %s\nkwargs: %s %s', |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
197 |
args, kwargs.keys(), [repr(v) for v in winners]) |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
198 |
else: |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
199 |
raise Exception('select ambiguity, args: %s\nkwargs: %s %s' |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
200 |
% (args, kwargs.keys(), |
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
201 |
[repr(v) for v in winners])) |
2658
5535857eeaa5
[appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
202 |
# return the result of calling the appobject |
5535857eeaa5
[appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
203 |
return winners[0](*args, **kwargs) |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
204 |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
205 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
206 |
class VRegistry(dict): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
207 |
"""class responsible to register, propose and select the various |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
208 |
elements used to build the web interface. Currently, we have templates, |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
209 |
views, actions and components. |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
210 |
""" |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
211 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
212 |
def __init__(self, config): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
213 |
super(VRegistry, self).__init__() |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
214 |
self.config = config |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
215 |
|
2662
87aed8cb7ff6
[uicfg reloading] don't reload uicfg when appobjects modules won't be reloaded as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
216 |
def reset(self, force_reload=None): |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
217 |
self.clear() |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
218 |
self._lastmodifs = {} |
2662
87aed8cb7ff6
[uicfg reloading] don't reload uicfg when appobjects modules won't be reloaded as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
219 |
# don't reload uicfg when appobjects modules won't be reloaded as well |
2655 | 220 |
if uicfg is not None: |
2662
87aed8cb7ff6
[uicfg reloading] don't reload uicfg when appobjects modules won't be reloaded as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
221 |
if force_reload is None: |
87aed8cb7ff6
[uicfg reloading] don't reload uicfg when appobjects modules won't be reloaded as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
222 |
force_reload = self.config.mode == 'dev' |
87aed8cb7ff6
[uicfg reloading] don't reload uicfg when appobjects modules won't be reloaded as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
223 |
if force_reload: |
87aed8cb7ff6
[uicfg reloading] don't reload uicfg when appobjects modules won't be reloaded as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2658
diff
changeset
|
224 |
reload(uicfg) |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
225 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
226 |
def __getitem__(self, name): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
227 |
"""return the registry (dictionary of class objects) associated to |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
228 |
this name |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
229 |
""" |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
230 |
try: |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
231 |
return super(VRegistry, self).__getitem__(name) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
232 |
except KeyError: |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
233 |
raise RegistryNotFound(name), None, sys.exc_info()[-1] |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
234 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
235 |
# dynamic selection methods ################################################ |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
236 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
237 |
@deprecated('use vreg[registry].object_by_id(oid, *args, **kwargs)') |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
238 |
def object_by_id(self, registry, oid, *args, **kwargs): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
239 |
"""return object in <registry>.<oid> |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
240 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
241 |
raise `ObjectNotFound` if not object with id <oid> in <registry> |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
242 |
raise `AssertionError` if there is more than one object there |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
243 |
""" |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
244 |
return self[registry].object_by_id(oid) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
245 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
246 |
@deprecated('use vreg[registry].select(oid, *args, **kwargs)') |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
247 |
def select(self, registry, oid, *args, **kwargs): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
248 |
"""return the most specific object in <registry>.<oid> according to |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
249 |
the given context |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
250 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
251 |
raise `ObjectNotFound` if not object with id <oid> in <registry> |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
252 |
raise `NoSelectableObject` if not object apply |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
253 |
""" |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
254 |
return self[registry].select(oid, *args, **kwargs) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
255 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
256 |
@deprecated('use vreg[registry].select_object(oid, *args, **kwargs)') |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
257 |
def select_object(self, registry, oid, *args, **kwargs): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
258 |
"""return the most specific object in <registry>.<oid> according to |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
259 |
the given context, or None if no object apply |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
260 |
""" |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
261 |
return self[registry].select_object(oid, *args, **kwargs) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
262 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
263 |
@deprecated('use vreg[registry].possible_objects(*args, **kwargs)') |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
264 |
def possible_objects(self, registry, *args, **kwargs): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
265 |
"""return an iterator on possible objects in <registry> for the given |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
266 |
context |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
267 |
""" |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
268 |
return self[registry].possible_objects(*args, **kwargs) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
269 |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
270 |
# methods for explicit (un)registration ################################### |
668
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
271 |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
272 |
# default class, when no specific class set |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
273 |
REGISTRY_FACTORY = {None: Registry} |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
274 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
275 |
def registry_class(self, regid): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
276 |
try: |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
277 |
return self.REGISTRY_FACTORY[regid] |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
278 |
except KeyError: |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
279 |
return self.REGISTRY_FACTORY[None] |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
280 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
281 |
def setdefault(self, regid): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
282 |
try: |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
283 |
return self[regid] |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
284 |
except KeyError: |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
285 |
self[regid] = self.registry_class(regid)(self.config) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
286 |
return self[regid] |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
287 |
|
668
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
288 |
# def clear(self, key): |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
289 |
# regname, oid = key.split('.') |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
290 |
# self[regname].pop(oid, None) |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2035
diff
changeset
|
291 |
|
695 | 292 |
def register_all(self, objects, modname, butclasses=()): |
293 |
for obj in objects: |
|
294 |
try: |
|
295 |
if obj.__module__ != modname or obj in butclasses: |
|
296 |
continue |
|
297 |
oid = obj.id |
|
298 |
except AttributeError: |
|
299 |
continue |
|
2587
79bbc5045288
[vreg] consider __abstract__ in register_all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2308
diff
changeset
|
300 |
if oid and not '__abstract__' in obj.__dict__: |
695 | 301 |
self.register(obj) |
1433 | 302 |
|
668
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
303 |
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
|
304 |
"""base method to add an object in the registry""" |
707 | 305 |
assert not '__abstract__' in obj.__dict__ |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
306 |
registryname = registryname or obj.__registry__ |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
307 |
registry = self.setdefault(registryname) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
308 |
registry.register(obj, oid=oid, clear=clear) |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
309 |
try: |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
310 |
vname = obj.__name__ |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
311 |
except AttributeError: |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
312 |
vname = obj.__class__.__name__ |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
313 |
self.debug('registered appobject %s in registry %s with id %s', |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
314 |
vname, registryname, oid) |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
315 |
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
|
316 |
|
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
317 |
def unregister(self, obj, registryname=None): |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
318 |
self[registryname or obj.__registry__].unregister(obj) |
1433 | 319 |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
320 |
def register_and_replace(self, obj, replaced, registryname=None): |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
321 |
self[registryname or obj.__registry__].register_and_replace(obj, replaced) |
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
322 |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
323 |
# initialization methods ################################################### |
1433 | 324 |
|
2035
946ed7349e1a
bugfix: extrapath is optional arg for init_registration
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2025
diff
changeset
|
325 |
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
|
326 |
# 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
|
327 |
self._toloadmods, filemods = _toload_info(path, extrapath) |
2651
3ad936634d2a
[registry] when a source file is changed, reset and reload the whole registry
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2589
diff
changeset
|
328 |
# XXX is _loadedmods still necessary ? It seems like it's useful |
3ad936634d2a
[registry] when a source file is changed, reset and reload the whole registry
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2589
diff
changeset
|
329 |
# to avoid loading same module twice, especially with the |
3ad936634d2a
[registry] when a source file is changed, reset and reload the whole registry
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2589
diff
changeset
|
330 |
# _load_ancestors_then_object logic but this needs to be checked |
1316
6d71d38822ee
introduce init_registration method and call it in repo initialization
sylvain.thenault@logilab.fr
parents:
1310
diff
changeset
|
331 |
self._loadedmods = {} |
6d71d38822ee
introduce init_registration method and call it in repo initialization
sylvain.thenault@logilab.fr
parents:
1310
diff
changeset
|
332 |
return filemods |
1433 | 333 |
|
2025
010a4b0fe855
fix lookup of files to load when using CW_CUBES_PATH
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
334 |
def register_objects(self, path, force_reload=None, extrapath=None): |
0 | 335 |
if force_reload is None: |
336 |
force_reload = self.config.mode == 'dev' |
|
337 |
elif not force_reload: |
|
338 |
# force_reload == False usually mean modules have been reloaded |
|
339 |
# by another connection, so we want to update the registry |
|
340 |
# content even if there has been no module content modification |
|
341 |
self.reset() |
|
342 |
# need to clean sys.path this to avoid import confusion pb (i.e. |
|
343 |
# having the same module loaded as 'cubicweb.web.views' subpackage and |
|
344 |
# as views' or 'web.views' subpackage |
|
345 |
# this is mainly for testing purpose, we should'nt need this in |
|
346 |
# production environment |
|
347 |
for webdir in (join(dirname(realpath(__file__)), 'web'), |
|
348 |
join(dirname(__file__), 'web')): |
|
349 |
if webdir in sys.path: |
|
350 |
sys.path.remove(webdir) |
|
351 |
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
|
352 |
sys.path.remove(CW_SOFTWARE_ROOT) |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
353 |
# load views from each directory in the instance'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
|
354 |
filemods = self.init_registration(path, extrapath) |
0 | 355 |
change = False |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
356 |
for filepath, modname in filemods: |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
357 |
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
|
358 |
change = True |
0 | 359 |
return change |
1433 | 360 |
|
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
361 |
def load_file(self, filepath, modname, force_reload=False): |
2651
3ad936634d2a
[registry] when a source file is changed, reset and reload the whole registry
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2589
diff
changeset
|
362 |
"""load app objects from a python file""" |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
363 |
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
|
364 |
if modname in self._loadedmods: |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
365 |
return |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
366 |
self._loadedmods[modname] = {} |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
367 |
try: |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
368 |
modified_on = stat(filepath)[-2] |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
369 |
except OSError: |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
370 |
# 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
|
371 |
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
|
372 |
filepath) |
0 | 373 |
return False |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
374 |
if filepath in self._lastmodifs: |
0 | 375 |
# 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
|
376 |
if modified_on <= self._lastmodifs[filepath]: |
0 | 377 |
return |
2651
3ad936634d2a
[registry] when a source file is changed, reset and reload the whole registry
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2589
diff
changeset
|
378 |
# if it was modified, raise RegistryOutOfDate to reload everything |
3ad936634d2a
[registry] when a source file is changed, reset and reload the whole registry
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2589
diff
changeset
|
379 |
self.info('File %s changed since last visit', filepath) |
3ad936634d2a
[registry] when a source file is changed, reset and reload the whole registry
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2589
diff
changeset
|
380 |
raise RegistryOutOfDate() |
0 | 381 |
# load the module |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
382 |
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
|
383 |
self.load_module(module) |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
384 |
self._lastmodifs[filepath] = modified_on |
0 | 385 |
return True |
386 |
||
387 |
def load_module(self, module): |
|
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
388 |
self.info('loading %s', module) |
668
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
389 |
if hasattr(module, 'registration_callback'): |
61b2328f9ed9
rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents:
665
diff
changeset
|
390 |
module.registration_callback(self) |
660
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
652
diff
changeset
|
391 |
else: |
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
652
diff
changeset
|
392 |
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
|
393 |
if objname.startswith('_'): |
5233a9457f6b
work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
652
diff
changeset
|
394 |
continue |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
395 |
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
|
396 |
self.debug('loaded %s', module) |
1433 | 397 |
|
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
398 |
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
|
399 |
# imported classes |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
400 |
objmodname = getattr(obj, '__module__', None) |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
401 |
if objmodname != modname: |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
402 |
if objmodname in self._toloadmods: |
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
403 |
self.load_file(self._toloadmods[objmodname], objmodname) |
0 | 404 |
return |
405 |
# skip non registerable object |
|
406 |
try: |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
407 |
if not issubclass(obj, AppObject): |
0 | 408 |
return |
409 |
except TypeError: |
|
410 |
return |
|
411 |
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
|
412 |
if objname in self._loadedmods[modname]: |
0 | 413 |
return |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
414 |
self._loadedmods[modname][objname] = obj |
0 | 415 |
for parent in obj.__bases__: |
1310
99dfced5673e
fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents:
1282
diff
changeset
|
416 |
self._load_ancestors_then_object(modname, parent) |
0 | 417 |
self.load_object(obj) |
1433 | 418 |
|
0 | 419 |
def load_object(self, obj): |
420 |
try: |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
421 |
self.register_appobject_class(obj) |
0 | 422 |
except Exception, ex: |
423 |
if self.config.mode in ('test', 'dev'): |
|
424 |
raise |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
425 |
self.exception('appobject %s registration failed: %s', obj, ex) |
1433 | 426 |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
427 |
# old automatic registration XXX deprecated ############################### |
1433 | 428 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
429 |
def register_appobject_class(self, cls): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
430 |
"""handle appobject class registration |
1433 | 431 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
432 |
appobject class with __abstract__ == True in their local dictionnary or |
0 | 433 |
with a name starting starting by an underscore are not registered. |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
434 |
Also a appobject class needs to have __registry__ and id attributes set |
0 | 435 |
to a non empty string to be registered. |
436 |
""" |
|
437 |
if (cls.__dict__.get('__abstract__') or cls.__name__[0] == '_' |
|
438 |
or not cls.__registry__ or not cls.id): |
|
439 |
return |
|
665
1305da1ce3f9
reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
440 |
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
|
441 |
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
|
442 |
return |
1432
2c3711d4570b
drop hopeless registerer bw compat
sylvain.thenault@logilab.fr
parents:
1316
diff
changeset
|
443 |
self.register(cls) |
1433 | 444 |
|
445 |
# init logging |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
446 |
set_log_methods(VRegistry, getLogger('cubicweb.vreg')) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
447 |
set_log_methods(Registry, getLogger('cubicweb.registry')) |
0 | 448 |
|
449 |
||
835
7dcb11dd443e
fix relation_possible, ensure we return int
sylvain.thenault@logilab.fr
parents:
827
diff
changeset
|
450 |
# 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
|
451 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
452 |
from cubicweb.appobject import objectify_selector, AndSelector, OrSelector, Selector |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
453 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
454 |
objectify_selector = deprecated('objectify_selector has been moved to appobject module')(objectify_selector) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
455 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
456 |
Selector = class_moved(Selector) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
457 |
|
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
458 |
@deprecated('use & operator (binary and)') |
693
cf0817dfc787
reimplements chainall/chainfirst using [And/Or], implemenent search_selector function on Selector base class
sylvain.thenault@logilab.fr
parents:
686
diff
changeset
|
459 |
def chainall(*selectors, **kwargs): |
0 | 460 |
"""return a selector chaining given selectors. If one of |
461 |
the selectors fail, selection will fail, else the returned score |
|
462 |
will be the sum of each selector'score |
|
463 |
""" |
|
464 |
assert selectors |
|
759
e044f28372bd
chainall / chainfirst ensures selectors are instantiated
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
741
diff
changeset
|
465 |
# 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
|
466 |
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
|
467 |
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
|
468 |
selector.__name__ = kwargs['name'] |
0 | 469 |
return selector |
470 |
||
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
471 |
@deprecated('use | operator (binary or)') |
693
cf0817dfc787
reimplements chainall/chainfirst using [And/Or], implemenent search_selector function on Selector base class
sylvain.thenault@logilab.fr
parents:
686
diff
changeset
|
472 |
def chainfirst(*selectors, **kwargs): |
0 | 473 |
"""return a selector chaining given selectors. If all |
474 |
the selectors fail, selection will fail, else the returned score |
|
475 |
will be the first non-zero selector score |
|
476 |
""" |
|
477 |
assert selectors |
|
780
5b6c93816871
fix selector search and refactor auto-instantiation
sylvain.thenault@logilab.fr
parents:
760
diff
changeset
|
478 |
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
|
479 |
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
|
480 |
selector.__name__ = kwargs['name'] |
0 | 481 |
return selector |