vregistry.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 31 Mar 2010 09:58:44 +0200
branchstable
changeset 5096 e25979a6d795
parent 5095 eaf522b9b998
child 5103 2b242604fc42
permissions -rw-r--r--
[cleanup] add some notes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     5
  according to a context
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     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
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
     9
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    15
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    16
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    17
:organization: Logilab
4212
ab6573088b4a update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3641
diff changeset
    18
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
import sys
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
from os import listdir, stat
4719
aaed3f813ef8 kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4576
diff changeset
    26
from os.path import dirname, join, realpath, isdir, exists
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
2025
010a4b0fe855 fix lookup of files to load when using CW_CUBES_PATH
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    38
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
    39
    """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
    40
    (file, module name) to load
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
    """
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
    42
    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
    43
    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
    44
        assert isinstance(path, list)
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
    45
        _toload = {}, []
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
    46
    for fileordir in path:
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
    47
        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
    48
            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
    49
            _toload_info(subfiles, extrapath, _toload)
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
    50
        elif fileordir[-3:] == '.py':
5078
ea66c4aabb47 [appobject imports] don't include __init__ in package's name. See comment for more info
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    51
            modpath = modpath_from_file(fileordir, extrapath)
ea66c4aabb47 [appobject imports] don't include __init__ in package's name. See comment for more info
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    52
            # omit '__init__' from package's name to avoid loading that module
ea66c4aabb47 [appobject imports] don't include __init__ in package's name. See comment for more info
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    53
            # once for each name when it is imported by some other appobject
ea66c4aabb47 [appobject imports] don't include __init__ in package's name. See comment for more info
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    54
            # module. This supposes import in modules are done as::
ea66c4aabb47 [appobject imports] don't include __init__ in package's name. See comment for more info
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    55
            #
ea66c4aabb47 [appobject imports] don't include __init__ in package's name. See comment for more info
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    56
            #   from package import something
ea66c4aabb47 [appobject imports] don't include __init__ in package's name. See comment for more info
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    57
            #
ea66c4aabb47 [appobject imports] don't include __init__ in package's name. See comment for more info
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    58
            # not::
ea66c4aabb47 [appobject imports] don't include __init__ in package's name. See comment for more info
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    59
            #
ea66c4aabb47 [appobject imports] don't include __init__ in package's name. See comment for more info
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    60
            #  from package.__init__ import something
ea66c4aabb47 [appobject imports] don't include __init__ in package's name. See comment for more info
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    61
            #
ea66c4aabb47 [appobject imports] don't include __init__ in package's name. See comment for more info
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    62
            # which seems quite correct.
ea66c4aabb47 [appobject imports] don't include __init__ in package's name. See comment for more info
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    63
            if modpath[-1] == '__init__':
ea66c4aabb47 [appobject imports] don't include __init__ in package's name. See comment for more info
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    64
                modpath.pop()
ea66c4aabb47 [appobject imports] don't include __init__ in package's name. See comment for more info
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    65
            modname = '.'.join(modpath)
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
    66
            _toload[0][modname] = fileordir
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
    67
            _toload[1].append((fileordir, modname))
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
    68
    return _toload
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
2821
526f44213b70 no need for a classid class method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2820
diff changeset
    71
def classid(cls):
526f44213b70 no need for a classid class method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2820
diff changeset
    72
    """returns a unique identifier for an appobject class"""
526f44213b70 no need for a classid class method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2820
diff changeset
    73
    return '%s.%s' % (cls.__module__, cls.__name__)
526f44213b70 no need for a classid class method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2820
diff changeset
    74
2829
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
    75
def class_regid(cls):
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
    76
    """returns a unique identifier for an appobject class"""
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
    77
    if 'id' in cls.__dict__:
3405
9d31c9cb8103 nicer deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3383
diff changeset
    78
        warn('[3.6] %s.%s: id is deprecated, use __regid__'
3383
c6aff16e5aed nicer warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3376
diff changeset
    79
             % (cls.__module__, cls.__name__), DeprecationWarning)
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3072
diff changeset
    80
        cls.__regid__ = cls.id
4013
f0d1038e5059 .id backward compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3674
diff changeset
    81
    if hasattr(cls, 'id') and not isinstance(cls.id, property):
2829
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
    82
        return cls.id
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3072
diff changeset
    83
    return cls.__regid__
2829
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
    84
5093
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
    85
def class_registries(cls, registryname):
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
    86
    if registryname:
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
    87
        return (registryname,)
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
    88
    return cls.__registries__
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
    89
2829
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
    90
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
    91
class Registry(dict):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
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
    93
    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
    94
        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
    95
        self.config = config
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
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
    97
    def __getitem__(self, name):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
        """return the registry (dictionary of class objects) associated to
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
        this name
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
        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
   102
            return super(Registry, self).__getitem__(name)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
        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
   104
            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
   105
4490
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   106
    def initialization_completed(self):
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   107
        for appobjects in self.itervalues():
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   108
            for appobjectcls in appobjects:
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   109
                appobjectcls.__registered__(self)
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   110
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
   111
    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
   112
        """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
   113
        assert not '__abstract__' in obj.__dict__
2829
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
   114
        oid = oid or class_regid(obj)
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
   115
        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
   116
        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
   117
            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
   118
        else:
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2656
diff changeset
   119
            appobjects = self.setdefault(oid, [])
4490
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   120
        assert not obj in appobjects, \
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   121
               'object %s is already registered' % obj
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   122
        appobjects.append(obj)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
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
   124
    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
   125
        # 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
   126
        # 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
   127
        # or simplify by calling unregister then register here
2821
526f44213b70 no need for a classid class method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2820
diff changeset
   128
        if not isinstance(replaced, basestring):
526f44213b70 no need for a classid class method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2820
diff changeset
   129
            replaced = classid(replaced)
2829
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
   130
        registered_objs = self.get(class_regid(obj), ())
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
   131
        for index, registered in enumerate(registered_objs):
2821
526f44213b70 no need for a classid class method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2820
diff changeset
   132
            if classid(registered) == replaced:
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
   133
                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
   134
                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
   135
        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
   136
            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
   137
                         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
   138
        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
   139
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
   140
    def unregister(self, obj):
2829
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
   141
        clsid = classid(obj)
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
   142
        oid = class_regid(obj)
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
   143
        for registered in self.get(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
   144
            # 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
   145
            # have its own version of the class, loaded through execfile
2829
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
   146
            if classid(registered) == clsid:
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
   147
                self[oid].remove(registered)
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
   148
                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
   149
        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
   150
            self.warning('can\'t remove %s, no id %s in the registry',
2829
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
   151
                         clsid, 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
   152
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
    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
   154
        """return a list containing all objects in this registry.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
        """
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
   156
        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
   157
        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
   158
            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
   159
        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
   160
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
    # dynamic selection methods ################################################
665
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   162
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
   163
    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
   164
        """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
   165
        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
   166
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
        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
   168
        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
   169
        """
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
   170
        objects = self[oid]
665
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   171
        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
   172
        return objects[0](*args, **kwargs)
665
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   173
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
   174
    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
   175
        """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
   176
        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
   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 `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
   179
        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
   180
        """
2769
1800aa0bf396 R [registry] changes in the API
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2689
diff changeset
   181
        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
   182
2769
1800aa0bf396 R [registry] changes in the API
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2689
diff changeset
   183
    def select_or_none(self, oid, *args, **kwargs):
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
   184
        """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
   185
        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
   186
        """
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
   187
        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
   188
            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
   189
        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
   190
            return None
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2844 2906
diff changeset
   191
    select_object = deprecated('[3.6] use select_or_none instead of select_object'
2769
1800aa0bf396 R [registry] changes in the API
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2689
diff changeset
   192
                               )(select_or_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
   193
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
   194
    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
   195
        """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
   196
        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
   197
        """
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2656
diff changeset
   198
        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
   199
            try:
2769
1800aa0bf396 R [registry] changes in the API
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2689
diff changeset
   200
                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
   201
            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
   202
                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
   203
2769
1800aa0bf396 R [registry] changes in the API
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2689
diff changeset
   204
    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
   205
        """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
   206
        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
   207
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
   208
        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
   209
        """
2083
f8f94c2951d3 add warning when rset isn't a named argument in select()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2058
diff changeset
   210
        if len(args) > 1:
3044
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3036
diff changeset
   211
            warn('[3.5] 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
   212
                 DeprecationWarning, stacklevel=3)
5095
eaf522b9b998 [optimisation] _select_best is used very intensivly. Peanuts optimization may count :)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
   213
        score, winners = 0, None
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2656
diff changeset
   214
        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
   215
            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
   216
            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
   217
                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
   218
            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
   219
                winners.append(appobject)
5095
eaf522b9b998 [optimisation] _select_best is used very intensivly. Peanuts optimization may count :)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
   220
        if winners is 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
   221
            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
   222
                                     % (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
   223
                                        [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
   224
        if len(winners) > 1:
5096
e25979a6d795 [cleanup] add some notes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5095
diff changeset
   225
            # don't we want the opposite: log in production environement, error
e25979a6d795 [cleanup] add some notes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5095
diff changeset
   226
            # while debugging?
3641
cf30e4498674 fix debug attribute conflict on configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3638
diff changeset
   227
            if self.config.debugmode:
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
   228
                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
   229
                           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
   230
            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
   231
                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
   232
                                % (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
   233
                                   [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
   234
        # 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
   235
        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
   236
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2844 2906
diff changeset
   237
    select_best = deprecated('[3.6] select_best is now private')(_select_best)
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
   238
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
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
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
   241
    """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
   242
    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
   243
    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
   244
    """
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
    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
   247
        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
   248
        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
   249
2760
30063071aee0 fix reloading, new .reset prototype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
   250
    def reset(self, path=None, force_reload=None):
2906
677fa98659a8 fix cw properties initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2836
diff changeset
   251
        # don't use self.clear, we want to keep existing subdictionaries
677fa98659a8 fix cw properties initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2836
diff changeset
   252
        for subdict in self.itervalues():
677fa98659a8 fix cw properties initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2836
diff changeset
   253
            subdict.clear()
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
   254
        self._lastmodifs = {}
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
    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
   257
        """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
   258
        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
   259
        """
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
        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
   261
            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
   262
        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
   263
            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
   264
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
    # 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
   266
2788
8d3dbe577d3a R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2771
diff changeset
   267
    @deprecated('[3.4] use vreg[registry].object_by_id(oid, *args, **kwargs)')
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
   268
    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
   269
        """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
   270
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
   271
        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
   272
        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
   273
        """
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
        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
   275
2788
8d3dbe577d3a R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2771
diff changeset
   276
    @deprecated('[3.4] use vreg[registry].select(oid, *args, **kwargs)')
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
   277
    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
   278
        """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
   279
        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
   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
        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
   282
        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
   283
        """
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
        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
   285
2788
8d3dbe577d3a R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2771
diff changeset
   286
    @deprecated('[3.4] use vreg[registry].select_or_none(oid, *args, **kwargs)')
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
   287
    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
   288
        """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
   289
        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
   290
        """
2770
356e9d7c356d R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2769
diff changeset
   291
        return self[registry].select_or_none(oid, *args, **kwargs)
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
   292
2788
8d3dbe577d3a R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2771
diff changeset
   293
    @deprecated('[3.4] use vreg[registry].possible_objects(*args, **kwargs)')
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
   294
    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
   295
        """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
   296
        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
   297
        """
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
   298
        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
   299
665
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   300
    # methods for explicit (un)registration ###################################
668
61b2328f9ed9 rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents: 665
diff changeset
   301
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
   302
    # 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
   303
    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
   304
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
   305
    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
   306
        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
   307
            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
   308
        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
   309
            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
   310
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
   311
    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
   312
        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
   313
            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
   314
        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
   315
            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
   316
            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
   317
668
61b2328f9ed9 rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents: 665
diff changeset
   318
#     def clear(self, key):
61b2328f9ed9 rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents: 665
diff changeset
   319
#         regname, oid = key.split('.')
61b2328f9ed9 rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents: 665
diff changeset
   320
#         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
   321
695
356da23c57f1 try a register_all method
sylvain.thenault@logilab.fr
parents: 693
diff changeset
   322
    def register_all(self, objects, modname, butclasses=()):
356da23c57f1 try a register_all method
sylvain.thenault@logilab.fr
parents: 693
diff changeset
   323
        for obj in objects:
356da23c57f1 try a register_all method
sylvain.thenault@logilab.fr
parents: 693
diff changeset
   324
            try:
356da23c57f1 try a register_all method
sylvain.thenault@logilab.fr
parents: 693
diff changeset
   325
                if obj.__module__ != modname or obj in butclasses:
356da23c57f1 try a register_all method
sylvain.thenault@logilab.fr
parents: 693
diff changeset
   326
                    continue
2829
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
   327
                oid = class_regid(obj)
695
356da23c57f1 try a register_all method
sylvain.thenault@logilab.fr
parents: 693
diff changeset
   328
            except AttributeError:
356da23c57f1 try a register_all method
sylvain.thenault@logilab.fr
parents: 693
diff changeset
   329
                continue
2587
79bbc5045288 [vreg] consider __abstract__ in register_all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2308
diff changeset
   330
            if oid and not '__abstract__' in obj.__dict__:
5093
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
   331
                self.register(obj, oid=oid)
1433
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
   332
668
61b2328f9ed9 rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents: 665
diff changeset
   333
    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
   334
        """base method to add an object in the registry"""
707
21a59b468f1a __selectors__ -> __select__
sylvain.thenault@logilab.fr
parents: 698
diff changeset
   335
        assert not '__abstract__' in obj.__dict__
665
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   336
        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
   337
            vname = obj.__name__
665
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   338
        except AttributeError:
5096
e25979a6d795 [cleanup] add some notes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5095
diff changeset
   339
            # XXX may occurs?
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
   340
            vname = obj.__class__.__name__
5093
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
   341
        for registryname in class_registries(obj, registryname):
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
   342
            registry = self.setdefault(registryname)
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
   343
            registry.register(obj, oid=oid, clear=clear)
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
   344
            self.debug('registered appobject %s in registry %s with id %s',
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
   345
                       vname, registryname, oid or class_regid(obj))
2844
4657d6e16e0c use class_regid / classid functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2838
diff changeset
   346
        self._loadedmods[obj.__module__][classid(obj)] = obj
665
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   347
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   348
    def unregister(self, obj, registryname=None):
5093
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
   349
        for registryname in class_registries(obj, registryname):
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
   350
            self[registryname].unregister(obj)
1433
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
   351
665
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   352
    def register_and_replace(self, obj, replaced, registryname=None):
5093
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
   353
        for registryname in class_registries(obj, registryname):
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
   354
            self[registryname].register_and_replace(obj, replaced)
665
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   355
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
   356
    # initialization methods ###################################################
1433
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
   357
2035
946ed7349e1a bugfix: extrapath is optional arg for init_registration
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2025
diff changeset
   358
    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
   359
        # 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
   360
        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
   361
        # 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
   362
        #     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
   363
        #     _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
   364
        self._loadedmods = {}
6d71d38822ee introduce init_registration method and call it in repo initialization
sylvain.thenault@logilab.fr
parents: 1310
diff changeset
   365
        return filemods
1433
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
   366
2760
30063071aee0 fix reloading, new .reset prototype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2689
diff changeset
   367
    def register_objects(self, path, force_reload, extrapath=None):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   368
        # need to clean sys.path this to avoid import confusion pb (i.e.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   369
        # having the same module loaded as 'cubicweb.web.views' subpackage and
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   370
        # as views'  or 'web.views' subpackage
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   371
        # this is mainly for testing purpose, we should'nt need this in
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   372
        # production environment
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   373
        for webdir in (join(dirname(realpath(__file__)), 'web'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   374
                       join(dirname(__file__), 'web')):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   375
            if webdir in sys.path:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   376
                sys.path.remove(webdir)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   377
        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
   378
            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
   379
        # 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
   380
        filemods = self.init_registration(path, extrapath)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   381
        change = False
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   382
        for filepath, modname in filemods:
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   383
            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
   384
                change = True
4490
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   385
        if change:
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   386
            self.initialization_completed()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   387
        return change
1433
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
   388
4490
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   389
    def initialization_completed(self):
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   390
        for regname, reg in self.iteritems():
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   391
            reg.initialization_completed()
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   392
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   393
    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
   394
        """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
   395
        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
   396
        if modname in self._loadedmods:
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   397
            return
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   398
        self._loadedmods[modname] = {}
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   399
        try:
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   400
            modified_on = stat(filepath)[-2]
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   401
        except OSError:
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   402
            # 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
   403
            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
   404
                         filepath)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   405
            return False
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   406
        if filepath in self._lastmodifs:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   407
            # 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
   408
            if modified_on <= self._lastmodifs[filepath]:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   409
                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
   410
            # 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
   411
            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
   412
            raise RegistryOutOfDate()
3565
4db55832e8a9 [vreg] fix reloading pb w/ module level error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3071
diff changeset
   413
        # set update time before module loading, else we get some reloading
4db55832e8a9 [vreg] fix reloading pb w/ module level error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3071
diff changeset
   414
        # weirdness in case of syntax error or other error while importing the
4db55832e8a9 [vreg] fix reloading pb w/ module level error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3071
diff changeset
   415
        # module
4db55832e8a9 [vreg] fix reloading pb w/ module level error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3071
diff changeset
   416
        self._lastmodifs[filepath] = modified_on
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   417
        # load the module
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   418
        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
   419
        self.load_module(module)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   420
        return True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   421
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   422
    def load_module(self, module):
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   423
        self.info('loading %s', module)
668
61b2328f9ed9 rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents: 665
diff changeset
   424
        if hasattr(module, 'registration_callback'):
61b2328f9ed9 rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents: 665
diff changeset
   425
            module.registration_callback(self)
660
5233a9457f6b work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 652
diff changeset
   426
        else:
5233a9457f6b work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 652
diff changeset
   427
            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
   428
                if objname.startswith('_'):
5233a9457f6b work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 652
diff changeset
   429
                    continue
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   430
                self._load_ancestors_then_object(module.__name__, obj)
1433
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
   431
4491
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   432
    def _load_ancestors_then_object(self, modname, appobjectcls):
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   433
        """handle automatic appobject class registration:
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   434
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   435
        - first ensure parent classes are already registered
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   436
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   437
        - class with __abstract__ == True in their local dictionnary or
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   438
          with a name starting starting by an underscore are not registered
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   439
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   440
        - appobject class needs to have __registry__ and __regid__ attributes
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   441
          set to a non empty string to be registered.
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   442
        """
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   443
        # imported classes
4491
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   444
        objmodname = getattr(appobjectcls, '__module__', None)
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   445
        if objmodname != modname:
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   446
            if objmodname in self._toloadmods:
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   447
                self.load_file(self._toloadmods[objmodname], objmodname)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   448
            return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   449
        # skip non registerable object
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   450
        try:
4491
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   451
            if not issubclass(appobjectcls, AppObject):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   452
                return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   453
        except TypeError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   454
            return
4491
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   455
        clsid = classid(appobjectcls)
2844
4657d6e16e0c use class_regid / classid functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2838
diff changeset
   456
        if clsid in self._loadedmods[modname]:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   457
            return
4491
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   458
        self._loadedmods[modname][clsid] = appobjectcls
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   459
        for parent in appobjectcls.__bases__:
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   460
            self._load_ancestors_then_object(modname, parent)
4491
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   461
        if (appobjectcls.__dict__.get('__abstract__')
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   462
            or appobjectcls.__name__[0] == '_'
5093
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
   463
            or not appobjectcls.__registries__
4491
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   464
            or not class_regid(appobjectcls)):
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   465
            return
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   466
        try:
4491
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   467
            self.register(appobjectcls)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   468
        except Exception, ex:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   469
            if self.config.mode in ('test', 'dev'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   470
                raise
4491
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   471
            self.exception('appobject %s registration failed: %s',
a0f48c31b58a kill register_appobject_class method, simplifying autoregistration. Update test accordingly (test which manually registers object should also properly call there __register__ method). Drop the disable-appobjects config file entry: no one used it since its introduction years ago.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4490
diff changeset
   472
                           appobjectcls, ex)
1433
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
   473
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
   474
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
   475
# 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
   476
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
   477
set_log_methods(Registry, getLogger('cubicweb.registry'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   478
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   479
835
7dcb11dd443e fix relation_possible, ensure we return int
sylvain.thenault@logilab.fr
parents: 827
diff changeset
   480
# 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
   481
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2656
diff changeset
   482
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
   483
2788
8d3dbe577d3a R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2771
diff changeset
   484
objectify_selector = deprecated('[3.4] objectify_selector has been moved to appobject module')(objectify_selector)
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2656
diff changeset
   485
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2656
diff changeset
   486
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
   487
2788
8d3dbe577d3a R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2771
diff changeset
   488
@deprecated('[3.4] 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
   489
def chainall(*selectors, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   490
    """return a selector chaining given selectors. If one of
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   491
    the selectors fail, selection will fail, else the returned score
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   492
    will be the sum of each selector'score
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   493
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   494
    assert selectors
759
e044f28372bd chainall / chainfirst ensures selectors are instantiated
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 741
diff changeset
   495
    # 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
   496
    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
   497
    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
   498
        selector.__name__ = kwargs['name']
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   499
    return selector
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   500
2788
8d3dbe577d3a R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2771
diff changeset
   501
@deprecated('[3.4] 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
   502
def chainfirst(*selectors, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   503
    """return a selector chaining given selectors. If all
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   504
    the selectors fail, selection will fail, else the returned score
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   505
    will be the first non-zero selector score
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   506
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   507
    assert selectors
780
5b6c93816871 fix selector search and refactor auto-instantiation
sylvain.thenault@logilab.fr
parents: 760
diff changeset
   508
    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
   509
    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
   510
        selector.__name__ = kwargs['name']
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   511
    return selector