vregistry.py
author Julien Cristau <julien.cristau@logilab.fr>
Thu, 23 Feb 2012 19:05:36 +0100
branchstable
changeset 8261 d4d9c88d4a5f
parent 7990 a673d1d9a738
child 8190 2a3c1b787688
permissions -rw-r--r--
[devtools] make a copy of the xvfb-run script so we can kill it properly Since changeset a3d3bdd46463 we don't run firefox directly but through a wrapper script. This meant that on stopping the test, we would kill that wrapper script but leave the Xvfb server and firefox processes running. So make a local copy of xvfb-run, and make it kill Xvfb when it goes down, not just when its client exits.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7990
a673d1d9a738 [diet] drop pre 3.6 API compatibility (but attempt to keep data cmopatibility). Closes #2017916
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7272
diff changeset
     1
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5273
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
"""
2308
b478c3a8ad2a typos, cleanup, lighter traced seletion output
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2173
diff changeset
    19
* 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
    20
  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
    21
  objects and provides a convenient api to access those objects
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
  according to a context
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
2308
b478c3a8ad2a typos, cleanup, lighter traced seletion output
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2173
diff changeset
    24
* 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
    25
  AppObject abstract class
1433
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
    26
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
* 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
    28
  selector, which is responsible to score the appobject according to the
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
  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
    30
  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
    31
  returned. The selector is instantiated at appobject registration
5693
8af6623f3d4e [pylint] fix detected name errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5651
diff changeset
    32
"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
import sys
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
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
    38
from os.path import dirname, join, realpath, isdir, exists
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
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
    40
from warnings import warn
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2656
diff changeset
    42
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
    43
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
    44
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2656
diff changeset
    45
from cubicweb import CW_SOFTWARE_ROOT
5273
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
    46
from cubicweb import RegistryNotFound, ObjectNotFound, NoSelectableObject
5569
cb14af012a96 [adapters] refactoring fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5446
diff changeset
    47
from cubicweb.appobject import AppObject, class_regid
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
6347
ef47a2100c66 exception: specify expected parameter in NoSelectableException
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6014
diff changeset
    49
2025
010a4b0fe855 fix lookup of files to load when using CW_CUBES_PATH
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    50
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
    51
    """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
    52
    (file, module name) to load
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
    """
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
    54
    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
    55
    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
    56
        assert isinstance(path, list)
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
    57
        _toload = {}, []
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
    58
    for fileordir in path:
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
    59
        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
    60
            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
    61
            _toload_info(subfiles, extrapath, _toload)
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
    62
        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
    63
            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
    64
            # 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
    65
            # 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
    66
            # 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
    67
            #
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
    68
            #   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
    69
            #
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
    70
            # 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
    71
            #
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
    72
            #  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
    73
            #
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
    74
            # 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
    75
            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
    76
                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
    77
            modname = '.'.join(modpath)
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
    78
            _toload[0][modname] = fileordir
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
    79
            _toload[1].append((fileordir, modname))
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
    80
    return _toload
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
2821
526f44213b70 no need for a classid class method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2820
diff changeset
    83
def classid(cls):
526f44213b70 no need for a classid class method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2820
diff changeset
    84
    """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
    85
    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
    86
5093
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
    87
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
    88
    if registryname:
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
    89
        return (registryname,)
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
    90
    return cls.__registries__
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
    91
2829
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
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
class Registry(dict):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
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
    95
    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
    96
        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
    97
        self.config = config
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
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
    99
    def __getitem__(self, name):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
        """return the registry (dictionary of class objects) associated to
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
        this name
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
        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
   104
            return super(Registry, self).__getitem__(name)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
        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
   106
            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
   107
4490
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   108
    def initialization_completed(self):
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   109
        for appobjects in self.itervalues():
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   110
            for appobjectcls in appobjects:
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   111
                appobjectcls.__registered__(self)
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   112
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
   113
    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
   114
        """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
   115
        assert not '__abstract__' in obj.__dict__
2829
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
   116
        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
   117
        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
   118
        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
   119
            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
   120
        else:
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2656
diff changeset
   121
            appobjects = self.setdefault(oid, [])
4490
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   122
        assert not obj in appobjects, \
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   123
               '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
   124
        appobjects.append(obj)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
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
   126
    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
   127
        # 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
   128
        # 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
   129
        # 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
   130
        if not isinstance(replaced, basestring):
526f44213b70 no need for a classid class method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2820
diff changeset
   131
            replaced = classid(replaced)
6872
23554151486c [vreg] check for identical app objects when replacing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6666
diff changeset
   132
        # prevent from misspelling
23554151486c [vreg] check for identical app objects when replacing
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6666
diff changeset
   133
        assert obj is not replaced, 'replacing an object by itself: %s' % obj
2829
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
   134
        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
   135
        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
   136
            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
   137
                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
   138
                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
   139
        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
   140
            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
   141
                         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
   142
        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
   143
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so 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
    def unregister(self, obj):
2829
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
   145
        clsid = classid(obj)
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
   146
        oid = class_regid(obj)
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
   147
        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
   148
            # 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
   149
            # 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
   150
            if classid(registered) == clsid:
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
   151
                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
   152
                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
   153
        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
   154
            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
   155
                         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
   156
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so 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
    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
   158
        """return a list containing all objects in this registry.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   159
        """
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
   160
        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
   161
        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
   162
            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
   163
        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
   164
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
   165
    # dynamic selection methods ################################################
665
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   166
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
   167
    def object_by_id(self, oid, *args, **kwargs):
5143
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   168
        """return object with the `oid` identifier. Only one object is expected
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   169
        to be 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
   170
5143
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   171
        raise :exc:`ObjectNotFound` if not object with id <oid> in <registry>
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   172
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   173
        raise :exc:`AssertionError` if there is more than one object there
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
   174
        """
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
   175
        objects = self[oid]
665
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   176
        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
   177
        return objects[0](*args, **kwargs)
665
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   178
6068
3bbc79a39d3a [vreg] rename select* object id argument to avoid name clash with arbitrary kwargs, just in case...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6014
diff changeset
   179
    def select(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
   180
        """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
   181
        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
   182
5143
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   183
        raise :exc:`ObjectNotFound` if not object with id <oid> in <registry>
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   184
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   185
        raise :exc:`NoSelectableObject` if not object apply
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
        """
7272
771f594c12a2 [vreg] vregistry._select_best was needlessly instanciating NoSelectableObject (closes #1626708)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7083
diff changeset
   187
        obj =  self._select_best(self[__oid], *args, **kwargs)
771f594c12a2 [vreg] vregistry._select_best was needlessly instanciating NoSelectableObject (closes #1626708)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7083
diff changeset
   188
        if obj is None:
771f594c12a2 [vreg] vregistry._select_best was needlessly instanciating NoSelectableObject (closes #1626708)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7083
diff changeset
   189
            raise NoSelectableObject(args, kwargs, self[__oid] )
771f594c12a2 [vreg] vregistry._select_best was needlessly instanciating NoSelectableObject (closes #1626708)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7083
diff changeset
   190
        return obj
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
   191
6068
3bbc79a39d3a [vreg] rename select* object id argument to avoid name clash with arbitrary kwargs, just in case...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6014
diff changeset
   192
    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
   193
        """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
   194
        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
   195
        """
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
        try:
6068
3bbc79a39d3a [vreg] rename select* object id argument to avoid name clash with arbitrary kwargs, just in case...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6014
diff changeset
   197
            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
   198
        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
   199
            return None
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2035
diff changeset
   200
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
   201
    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
   202
        """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
   203
        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
   204
        """
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2656
diff changeset
   205
        for appobjects in self.itervalues():
7272
771f594c12a2 [vreg] vregistry._select_best was needlessly instanciating NoSelectableObject (closes #1626708)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7083
diff changeset
   206
            obj = self._select_best(appobjects,  *args, **kwargs)
771f594c12a2 [vreg] vregistry._select_best was needlessly instanciating NoSelectableObject (closes #1626708)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7083
diff changeset
   207
            if obj 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
   208
                continue
7272
771f594c12a2 [vreg] vregistry._select_best was needlessly instanciating NoSelectableObject (closes #1626708)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7083
diff changeset
   209
            yield obj
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
   210
2769
1800aa0bf396 R [registry] changes in the API
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2689
diff changeset
   211
    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
   212
        """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
   213
        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
   214
7272
771f594c12a2 [vreg] vregistry._select_best was needlessly instanciating NoSelectableObject (closes #1626708)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7083
diff changeset
   215
        return None if not object apply (don't raise `NoSelectableObject` since
771f594c12a2 [vreg] vregistry._select_best was needlessly instanciating NoSelectableObject (closes #1626708)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7083
diff changeset
   216
        it's costly when searching appobjects using `possible_objects`
771f594c12a2 [vreg] vregistry._select_best was needlessly instanciating NoSelectableObject (closes #1626708)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7083
diff changeset
   217
        (e.g. searching for hooks).
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
   218
        """
5095
eaf522b9b998 [optimisation] _select_best is used very intensivly. Peanuts optimization may count :)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
   219
        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
   220
        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
   221
            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
   222
            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
   223
                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
   224
            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
   225
                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
   226
        if winners is None:
7272
771f594c12a2 [vreg] vregistry._select_best was needlessly instanciating NoSelectableObject (closes #1626708)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7083
diff changeset
   227
            return 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
   228
        if len(winners) > 1:
5572
df5551d697e8 [vreg] should raise exception on select ambiguity when running in test mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5569
diff changeset
   229
            # log in production environement / test, error while debugging
6347
ef47a2100c66 exception: specify expected parameter in NoSelectableException
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6014
diff changeset
   230
            msg = 'select ambiguity: %s\n(args: %s, kwargs: %s)'
5572
df5551d697e8 [vreg] should raise exception on select ambiguity when running in test mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5569
diff changeset
   231
            if self.config.debugmode or self.config.mode == 'test':
6347
ef47a2100c66 exception: specify expected parameter in NoSelectableException
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6014
diff changeset
   232
                # raise bare exception in debug mode
6386
af296184efd6 [vreg] fix bad copy and paste introduced in 6347:ef47a2100c66
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6347
diff changeset
   233
                raise Exception(msg % (winners, args, kwargs.keys()))
af296184efd6 [vreg] fix bad copy and paste introduced in 6347:ef47a2100c66
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6347
diff changeset
   234
            self.error(msg, winners, args, kwargs.keys())
2658
5535857eeaa5 [appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   235
        # 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
   236
        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
   237
7083
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6872
diff changeset
   238
    # these are overridden by set_log_methods below
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6872
diff changeset
   239
    # only defining here to prevent pylint from complaining
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6872
diff changeset
   240
    info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6872
diff changeset
   241
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
   242
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so 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
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
   244
    """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
   245
    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
   246
    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
   247
    """
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so 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
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so 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
    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
   250
        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
   251
        self.config = config
5273
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   252
        # need to clean sys.path this to avoid import confusion pb (i.e.  having
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   253
        # the same module loaded as 'cubicweb.web.views' subpackage and as
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   254
        # views' or 'web.views' subpackage. This is mainly for testing purpose,
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   255
        # we should'nt need this in production environment
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   256
        for webdir in (join(dirname(realpath(__file__)), 'web'),
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   257
                       join(dirname(__file__), 'web')):
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   258
            if webdir in sys.path:
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   259
                sys.path.remove(webdir)
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   260
        if CW_SOFTWARE_ROOT in sys.path:
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   261
            sys.path.remove(CW_SOFTWARE_ROOT)
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
   262
5273
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   263
    def reset(self):
2906
677fa98659a8 fix cw properties initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2836
diff changeset
   264
        # 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
   265
        for subdict in self.itervalues():
677fa98659a8 fix cw properties initialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2836
diff changeset
   266
            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
   267
        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
   268
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so 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
    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
   270
        """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
   271
        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
   272
        """
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so 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
        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
   274
            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
   275
        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
   276
            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
   277
665
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   278
    # methods for explicit (un)registration ###################################
668
61b2328f9ed9 rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents: 665
diff changeset
   279
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
   280
    # 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
   281
    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
   282
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so 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
    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
   284
        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
   285
            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
   286
        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
   287
            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
   288
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so 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
    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
   290
        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
   291
            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
   292
        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
   293
            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
   294
            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
   295
668
61b2328f9ed9 rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents: 665
diff changeset
   296
#     def clear(self, key):
61b2328f9ed9 rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents: 665
diff changeset
   297
#         regname, oid = key.split('.')
61b2328f9ed9 rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents: 665
diff changeset
   298
#         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
   299
695
356da23c57f1 try a register_all method
sylvain.thenault@logilab.fr
parents: 693
diff changeset
   300
    def register_all(self, objects, modname, butclasses=()):
5143
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   301
        """register all `objects` given. Objects which are not from the module
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   302
        `modname` or which are in `butclasses` won't be registered.
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   303
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   304
        Typical usage is:
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   305
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   306
        .. sourcecode:: python
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   307
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   308
            vreg.register_all(globals().values(), __name__, (ClassIWantToRegisterExplicitly,))
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   309
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   310
        So you get partially automatic registration, keeping manual registration
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   311
        for some object (to use
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   312
        :meth:`~cubicweb.cwvreg.CubicWebRegistry.register_and_replace` for
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   313
        instance)
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   314
        """
695
356da23c57f1 try a register_all method
sylvain.thenault@logilab.fr
parents: 693
diff changeset
   315
        for obj in objects:
356da23c57f1 try a register_all method
sylvain.thenault@logilab.fr
parents: 693
diff changeset
   316
            try:
356da23c57f1 try a register_all method
sylvain.thenault@logilab.fr
parents: 693
diff changeset
   317
                if obj.__module__ != modname or obj in butclasses:
356da23c57f1 try a register_all method
sylvain.thenault@logilab.fr
parents: 693
diff changeset
   318
                    continue
2829
054a8805da52 turn id into __id__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2821
diff changeset
   319
                oid = class_regid(obj)
695
356da23c57f1 try a register_all method
sylvain.thenault@logilab.fr
parents: 693
diff changeset
   320
            except AttributeError:
356da23c57f1 try a register_all method
sylvain.thenault@logilab.fr
parents: 693
diff changeset
   321
                continue
2587
79bbc5045288 [vreg] consider __abstract__ in register_all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2308
diff changeset
   322
            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
   323
                self.register(obj, oid=oid)
1433
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
   324
668
61b2328f9ed9 rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents: 665
diff changeset
   325
    def register(self, obj, registryname=None, oid=None, clear=False):
5143
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   326
        """register `obj` application object into `registryname` or
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   327
        `obj.__registry__` if not specified, with identifier `oid` or
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   328
        `obj.__regid__` if not specified.
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   329
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   330
        If `clear` is true, all objects with the same identifier will be
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   331
        previously unregistered.
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   332
        """
707
21a59b468f1a __selectors__ -> __select__
sylvain.thenault@logilab.fr
parents: 698
diff changeset
   333
        assert not '__abstract__' in obj.__dict__
665
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   334
        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
   335
            vname = obj.__name__
665
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   336
        except AttributeError:
5096
e25979a6d795 [cleanup] add some notes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5095
diff changeset
   337
            # 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
   338
            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
   339
        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
   340
            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
   341
            registry.register(obj, oid=oid, clear=clear)
6014
a5e22657f6f4 [debug] improve debug messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5752
diff changeset
   342
            self.debug('register %s in %s[\'%s\']',
5093
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
   343
                       vname, registryname, oid or class_regid(obj))
5651
95f7285e85e2 [vreg] ensure you can register an object from module not known yet.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5607
diff changeset
   344
        self._loadedmods.setdefault(obj.__module__, {})[classid(obj)] = obj
665
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   345
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   346
    def unregister(self, obj, registryname=None):
5143
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   347
        """unregister `obj` application object from the registry `registryname` or
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   348
        `obj.__registry__` if not specified.
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   349
        """
5093
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
   350
        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
   351
            self[registryname].unregister(obj)
1433
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
   352
665
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   353
    def register_and_replace(self, obj, replaced, registryname=None):
5143
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   354
        """register `obj` application object into `registryname` or
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   355
        `obj.__registry__` if not specified. If found, the `replaced` object
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   356
        will be unregistered first (else a warning will be issued as it's
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   357
        generally unexpected).
43afbdd5c8b4 improved doc on selectors an vregistry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   358
        """
5093
8d073d2e089d [optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5078
diff changeset
   359
        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
   360
            self[registryname].register_and_replace(obj, replaced)
665
1305da1ce3f9 reorganize a bit, some cleanup and fixes
sylvain.thenault@logilab.fr
parents: 660
diff changeset
   361
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
   362
    # initialization methods ###################################################
1433
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
   363
2035
946ed7349e1a bugfix: extrapath is optional arg for init_registration
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2025
diff changeset
   364
    def init_registration(self, path, extrapath=None):
5446
b018324198d4 [vreg] we should now reset in init_registration since we don't do it in __init__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   365
        self.reset()
1316
6d71d38822ee introduce init_registration method and call it in repo initialization
sylvain.thenault@logilab.fr
parents: 1310
diff changeset
   366
        # 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
   367
        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
   368
        # 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
   369
        #     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
   370
        #     _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
   371
        self._loadedmods = {}
6d71d38822ee introduce init_registration method and call it in repo initialization
sylvain.thenault@logilab.fr
parents: 1310
diff changeset
   372
        return filemods
1433
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
   373
5748
487ab6ac90cc [auto-reload] we should never call load_module with use_sys=False, that may lead to inconsistency with module interdependancy (eg module get imported by another one, then is reimported by the vreg to load its appobjects. Cleanup of sys.modules done before reloading should be enough.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5693
diff changeset
   374
    def register_objects(self, path, extrapath=None):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2381
diff changeset
   375
        # 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
   376
        filemods = self.init_registration(path, extrapath)
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   377
        for filepath, modname in filemods:
5748
487ab6ac90cc [auto-reload] we should never call load_module with use_sys=False, that may lead to inconsistency with module interdependancy (eg module get imported by another one, then is reimported by the vreg to load its appobjects. Cleanup of sys.modules done before reloading should be enough.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5693
diff changeset
   378
            self.load_file(filepath, modname)
5273
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   379
        self.initialization_completed()
1433
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
   380
4490
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   381
    def initialization_completed(self):
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   382
        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
   383
            reg.initialization_completed()
d45cde54d464 backport stable branch and some vreg cleanups:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   384
5273
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   385
    def _mdate(self, filepath):
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   386
        try:
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   387
            return stat(filepath)[-2]
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   388
        except OSError:
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   389
            # this typically happens on emacs backup files (.#foo.py)
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   390
            self.warning('Unable to load %s. It is likely to be a backup file',
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   391
                         filepath)
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   392
            return None
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   393
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   394
    def is_reload_needed(self, path):
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   395
        """return True if something module changed and the registry should be
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   396
        reloaded
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   397
        """
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   398
        lastmodifs = self._lastmodifs
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   399
        for fileordir in path:
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   400
            if isdir(fileordir) and exists(join(fileordir, '__init__.py')):
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   401
                if self.is_reload_needed([join(fileordir, fname)
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   402
                                          for fname in listdir(fileordir)]):
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   403
                    return True
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   404
            elif fileordir[-3:] == '.py':
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   405
                mdate = self._mdate(fileordir)
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   406
                if mdate is None:
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   407
                    continue # backup file, see _mdate implementation
5607
38d43dc5ee57 skip flymake junk when reloading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5424
diff changeset
   408
                elif "flymake" in fileordir:
38d43dc5ee57 skip flymake junk when reloading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5424
diff changeset
   409
                    # flymake + pylint in use, don't consider these they will corrupt the registry
38d43dc5ee57 skip flymake junk when reloading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5424
diff changeset
   410
                    continue
5273
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   411
                if fileordir not in lastmodifs or lastmodifs[fileordir] < mdate:
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   412
                    self.info('File %s changed since last visit', fileordir)
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   413
                    return True
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   414
        return False
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   415
5748
487ab6ac90cc [auto-reload] we should never call load_module with use_sys=False, that may lead to inconsistency with module interdependancy (eg module get imported by another one, then is reimported by the vreg to load its appobjects. Cleanup of sys.modules done before reloading should be enough.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5693
diff changeset
   416
    def load_file(self, filepath, modname):
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
   417
        """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
   418
        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
   419
        if modname in self._loadedmods:
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   420
            return
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   421
        self._loadedmods[modname] = {}
5273
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   422
        mdate = self._mdate(filepath)
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   423
        if mdate is None:
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   424
            return # backup file, see _mdate implementation
5607
38d43dc5ee57 skip flymake junk when reloading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5424
diff changeset
   425
        elif "flymake" in filepath:
38d43dc5ee57 skip flymake junk when reloading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5424
diff changeset
   426
            # flymake + pylint in use, don't consider these they will corrupt the registry
38d43dc5ee57 skip flymake junk when reloading
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5424
diff changeset
   427
            return
3565
4db55832e8a9 [vreg] fix reloading pb w/ module level error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3071
diff changeset
   428
        # 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
   429
        # 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
   430
        # module
5273
c4caef6f09c9 [vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5143
diff changeset
   431
        self._lastmodifs[filepath] = mdate
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   432
        # load the module
5748
487ab6ac90cc [auto-reload] we should never call load_module with use_sys=False, that may lead to inconsistency with module interdependancy (eg module get imported by another one, then is reimported by the vreg to load its appobjects. Cleanup of sys.modules done before reloading should be enough.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5693
diff changeset
   433
        module = load_module_from_name(modname)
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   434
        self.load_module(module)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   435
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   436
    def load_module(self, module):
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6068
diff changeset
   437
        self.info('loading %s from %s', module.__name__, module.__file__)
668
61b2328f9ed9 rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents: 665
diff changeset
   438
        if hasattr(module, 'registration_callback'):
61b2328f9ed9 rename registration callback, clear argument to register
sylvain.thenault@logilab.fr
parents: 665
diff changeset
   439
            module.registration_callback(self)
660
5233a9457f6b work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 652
diff changeset
   440
        else:
5233a9457f6b work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 652
diff changeset
   441
            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
   442
                if objname.startswith('_'):
5233a9457f6b work in progress, draft for manual registration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 652
diff changeset
   443
                    continue
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   444
                self._load_ancestors_then_object(module.__name__, obj)
1433
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
   445
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
   446
    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
   447
        """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
   448
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
   449
        - 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
   450
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
        - class with __abstract__ == True in their local dictionnary or
6661
1719137de7da fix grammar in docstring
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6386
diff changeset
   452
          with a name starting with an underscore are not registered
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
   453
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
   454
        - 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
   455
          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
   456
        """
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   457
        # 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
   458
        objmodname = getattr(appobjectcls, '__module__', None)
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   459
        if objmodname != modname:
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   460
            if objmodname in self._toloadmods:
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   461
                self.load_file(self._toloadmods[objmodname], objmodname)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   462
            return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   463
        # skip non registerable object
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   464
        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
   465
            if not issubclass(appobjectcls, AppObject):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   466
                return
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   467
        except TypeError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   468
            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
   469
        clsid = classid(appobjectcls)
2844
4657d6e16e0c use class_regid / classid functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2838
diff changeset
   470
        if clsid in self._loadedmods[modname]:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   471
            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
   472
        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
   473
        for parent in appobjectcls.__bases__:
1310
99dfced5673e fix vobjects registration to deal with objects inter-dependancy
sylvain.thenault@logilab.fr
parents: 1282
diff changeset
   474
            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
   475
        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
   476
            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
   477
            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
   478
            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
   479
            return
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   480
        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
   481
            self.register(appobjectcls)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   482
        except Exception, ex:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   483
            if self.config.mode in ('test', 'dev'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   484
                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
   485
            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
   486
                           appobjectcls, ex)
7083
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6872
diff changeset
   487
    # these are overridden by set_log_methods below
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6872
diff changeset
   488
    # only defining here to prevent pylint from complaining
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6872
diff changeset
   489
    info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None
1433
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
   490
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
   491
091ac3ba5d51 remove trailing white spaces
sylvain.thenault@logilab.fr
parents: 1432
diff changeset
   492
# 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
   493
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
   494
set_log_methods(Registry, getLogger('cubicweb.registry'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   495
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   496
835
7dcb11dd443e fix relation_possible, ensure we return int
sylvain.thenault@logilab.fr
parents: 827
diff changeset
   497
# 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
   498
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2656
diff changeset
   499
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
   500
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2656
diff changeset
   501
Selector = class_moved(Selector)