appobject.py
changeset 8664 29652410c317
parent 8654 7021bba2dcf2
child 8930 6a02be304486
equal deleted inserted replaced
8663:4e2dc5e61599 8664:29652410c317
    34 from logging import getLogger
    34 from logging import getLogger
    35 
    35 
    36 from logilab.common.deprecation import deprecated, class_renamed
    36 from logilab.common.deprecation import deprecated, class_renamed
    37 from logilab.common.decorators import classproperty
    37 from logilab.common.decorators import classproperty
    38 from logilab.common.logging_ext import set_log_methods
    38 from logilab.common.logging_ext import set_log_methods
    39 from logilab.common.registry import yes
       
    40 
    39 
    41 # XXX for bw compat
    40 # first line imports for bw compat
    42 from logilab.common.registry import objectify_predicate, traced_selection, Predicate
    41 from logilab.common.registry import (objectify_predicate, traced_selection, Predicate,
       
    42                                      RegistrableObject, yes)
    43 
    43 
    44 
    44 
    45 objectify_selector = deprecated('[3.15] objectify_selector has been '
    45 objectify_selector = deprecated('[3.15] objectify_selector has been '
    46                                 'renamed to objectify_predicates in '
    46                                 'renamed to objectify_predicates in '
    47                                 'logilab.common.registry')(objectify_predicate)
    47                                 'logilab.common.registry')(objectify_predicate)
    55 def lltrace(func):
    55 def lltrace(func):
    56     return func
    56     return func
    57 
    57 
    58 # the base class for all appobjects ############################################
    58 # the base class for all appobjects ############################################
    59 
    59 
    60 class AppObject(object):
    60 class AppObject(RegistrableObject):
    61     """This is the base class for CubicWeb application objects which are
    61     """This is the base class for CubicWeb application objects which are
    62     selected according to a context (usually at least a request and a result
    62     selected in a request context.
    63     set).
       
    64 
    63 
    65     The following attributes should be set on concrete appobject classes:
    64     The following attributes should be set on concrete appobject classes:
    66 
       
    67     :attr:`__registry__`
       
    68       name of the registry for this object (string like 'views',
       
    69       'templates'...)
       
    70 
       
    71     :attr:`__regid__`
       
    72       object's identifier in the registry (string like 'main',
       
    73       'primary', 'folder_box')
       
    74 
       
    75     :attr:`__select__`
       
    76       class'selector
       
    77 
       
    78     Moreover, the `__abstract__` attribute may be set to True to indicate that a
       
    79     class is abstract and should not be registered.
       
    80 
    65 
    81     At selection time, the following attributes are set on the instance:
    66     At selection time, the following attributes are set on the instance:
    82 
    67 
    83     :attr:`_cw`
    68     :attr:`_cw`
    84       current request
    69       current request
   105     .. Note::
    90     .. Note::
   106 
    91 
   107       * do not inherit directly from this class but from a more specific class
    92       * do not inherit directly from this class but from a more specific class
   108         such as `AnyEntity`, `EntityView`, `AnyRsetView`, `Action`...
    93         such as `AnyEntity`, `EntityView`, `AnyRsetView`, `Action`...
   109 
    94 
   110       * to be recordable, a subclass has to define its registry (attribute
       
   111         `__registry__`) and its identifier (attribute `__regid__`). Usually
       
   112         you don't have to take care of the registry since it's set by the base
       
   113         class, only the identifier `id`
       
   114 
       
   115       * application objects are designed to be loaded by the vregistry and
       
   116         should be accessed through it, not by direct instantiation, besides
       
   117         to use it as base classe.
       
   118 
       
   119 
       
   120       * When we inherit from `AppObject` (even not directly), you *always* have
       
   121         to use **super()** to get the methods and attributes of the superclasses,
       
   122         and not use the class identifier.
       
   123 
       
   124         For example, instead of writting::
       
   125 
       
   126           class Truc(PrimaryView):
       
   127               def f(self, arg1):
       
   128                   PrimaryView.f(self, arg1)
       
   129 
       
   130         You must write::
       
   131 
       
   132           class Truc(PrimaryView):
       
   133               def f(self, arg1):
       
   134                   super(Truc, self).f(arg1)
       
   135 
       
   136     """
    95     """
   137     __registry__ = None
       
   138     __regid__ = None
       
   139     __select__ = yes()
    96     __select__ = yes()
   140 
       
   141     @classproperty
       
   142     def __registries__(cls):
       
   143         if cls.__registry__ is None:
       
   144             return ()
       
   145         return (cls.__registry__,)
       
   146 
    97 
   147     @classmethod
    98     @classmethod
   148     def __registered__(cls, registry):
    99     def __registered__(cls, registry):
   149         """called by the registry when the appobject has been registered.
   100         """called by the registry when the appobject has been registered.
   150 
   101