Package common :: Module registry :: Class RegistryStore
[frames] | no frames]

Class RegistryStore

source code

object --+    
         |    
      dict --+
             |
            RegistryStore

This class is responsible for loading implementations and storing them
in their registry which are created on the fly as needed.

It handles dynamic registration of objects and provides a convenient api to
access them. To be recognized as an object that should be stored into one of
the store's registry (:class:`Registry`), an object (usually a class) has
the following attributes, used control how they interact with the registry:

:attr:`__registry__` or `__registries__`
  name of the registry for this object (string like 'views', 'templates'...)
  or list of registry names if you want your object to be added to multiple
  registries

:attr:`__regid__`
  implementation's identifier in the registry (string like 'main',
  'primary', 'folder_box')

:attr:`__select__`
  the implementation's selector

Moreover, the :attr:`__abstract__` attribute may be set to `True` to
indicate that a class is abstract and should not be registered (inherited
attributes not considered).

.. Note::

  When using the store to load objects dynamically, you *always* have
  to use **super()** to get the methods and attributes of the
  superclasses, and not use the class identifier. Else, you'll get into
  trouble when reloading comes into the place.

  For example, instead of writing::

      class Thing(Parent):
          __regid__ = 'athing'
          __select__ = yes()
          def f(self, arg1):
              Parent.f(self, arg1)

  You must write::

      class Thing(Parent):
          __regid__ = 'athing'
          __select__ = yes()
          def f(self, arg1):
              super(Parent, self).f(arg1)

Controlling objects registration
--------------------------------

Dynamic loading is triggered by calling the :meth:`register_objects` method,
given a list of directory to inspect for python modules.

.. automethod: register_objects

For each module, by default, all compatible objects are registered
automatically, though if some objects have to replace other objects, or have
to be included only if some condition is met, you'll have to define a
`registration_callback(vreg)` function in your module and explicitly
register **all objects** in this module, using the api defined below.


.. automethod:: RegistryStore.register_all
.. automethod:: RegistryStore.register_and_replace
.. automethod:: RegistryStore.register
.. automethod:: RegistryStore.unregister

.. Note::
    Once the function `registration_callback(vreg)` is implemented in a
    module, all the objects from this module have to be explicitly
    registered as it disables the automatic objects registration.


Examples:

.. sourcecode:: python

   # cubicweb/web/views/basecomponents.py
   def registration_callback(store):
      # register everything in the module except SeeAlsoComponent
      store.register_all(globals().values(), __name__, (SeeAlsoVComponent,))
      # conditionally register SeeAlsoVComponent
      if 'see_also' in store.schema:
          store.register(SeeAlsoVComponent)

In this example, we register all application object classes defined in the module
except `SeeAlsoVComponent`. This class is then registered only if the 'see_also'
relation type is defined in the instance'schema.

.. sourcecode:: python

   # goa/appobjects/sessions.py
   def registration_callback(store):
      store.register(SessionsCleaner)
      # replace AuthenticationManager by GAEAuthenticationManager
      store.register_and_replace(GAEAuthenticationManager, AuthenticationManager)
      # replace PersistentSessionManager by GAEPersistentSessionManager
      store.register_and_replace(GAEPersistentSessionManager, PersistentSessionManager)

In this example, we explicitly register classes one by one:

* the `SessionCleaner` class
* the `GAEAuthenticationManager` to replace the `AuthenticationManager`
* the `GAEPersistentSessionManager` to replace the `PersistentSessionManager`

If at some point we register a new appobject class in this module, it won't be
registered at all without modification to the `registration_callback`
implementation. The previous example will register it though, thanks to the call
to the `register_all` method.

Controlling registry instantation
---------------------------------
The `REGISTRY_FACTORY` class dictionary allows to specify which class should
be instantiated for a given registry name. The class associated to `None` in
it will be the class used when there is no specific class for a name.

Instance Methods
new empty dictionary

__init__(self, debugmode=False)
x.__init__(...) initializes x; see help(type(x)) for signature
source code
 
reset(self)
clear all registries managed by this store
source code
 
__getitem__(self, name)
return the registry (dictionary of class objects) associated to...
source code
 
registry_class(self, regid)
return existing registry named regid or use factory to create one and...
source code
D.get(k,d), also set D[k]=d if k not in D
setdefault(self, regid) source code
 
register_all(self, objects, modname, butclasses=())
register all `objects` given.
source code
 
register(self, obj, registryname=None, oid=None, clear=False)
register `obj` implementation into `registryname` or `obj.__registry__` if not specified, with identifier `oid` or `obj.__regid__` if not specified.
source code
 
unregister(self, obj, registryname=None)
unregister `obj` implementation object from the registry `registryname` or `obj.__registry__` if not specified.
source code
 
register_and_replace(self, obj, replaced, registryname=None)
register `obj` implementation object into `registryname` or `obj.__registry__` if not specified.
source code
 
init_registration(self, path, extrapath=None)
reset registry and walk down path to return list of (path, name)...
source code
 
register_objects(self, path, extrapath=None)
register all objects found walking down <path>
source code
 
initialization_completed(self)
call initialization_completed() on all known registries
source code
 
is_reload_needed(self, path)
return True if something module changed and the registry should be...
source code
 
load_file(self, filepath, modname)
load app objects from a python file
source code
 
load_module(self, module)
load objects from a module using registration_callback() when it exists...
source code

Inherited from dict: __cmp__, __contains__, __delitem__, __eq__, __ge__, __getattribute__, __gt__, __iter__, __le__, __len__, __lt__, __ne__, __new__, __repr__, __setitem__, __sizeof__, clear, copy, fromkeys, get, has_key, items, iteritems, iterkeys, itervalues, keys, pop, popitem, update, values, viewitems, viewkeys, viewvalues

Inherited from object: __delattr__, __format__, __reduce__, __reduce_ex__, __setattr__, __str__, __subclasshook__

Class Variables
  REGISTRY_FACTORY = {None: Registry}
  debug = lambda msg,* a,** kw:
  exception = lambda msg,* a,** kw:
  critical = lambda msg,* a,** kw:
  error = lambda msg,* a,** kw:
  warning = lambda msg,* a,** kw:
  info = lambda msg,* a,** kw:

Inherited from dict: __hash__

Properties

Inherited from object: __class__

Method Details

__init__(self, debugmode=False)
(Constructor)

source code 

x.__init__(...) initializes x; see help(type(x)) for signature

Returns:
new empty dictionary

Overrides: object.__init__
(inherited documentation)

__getitem__(self, name)
(Indexing operator)

source code 
return the registry (dictionary of class objects) associated to
this name

Overrides: dict.__getitem__

registry_class(self, regid)

source code 
return existing registry named regid or use factory to create one and
return it

setdefault(self, regid)

source code 
Returns: D.get(k,d), also set D[k]=d if k not in D
Overrides: dict.setdefault
(inherited documentation)

register_all(self, objects, modname, butclasses=())

source code 
register all `objects` given. Objects which are not from the module
`modname` or which are in `butclasses` won't be registered.

Typical usage is:

.. sourcecode:: python

    store.register_all(globals().values(), __name__, (ClassIWantToRegisterExplicitly,))

So you get partially automatic registration, keeping manual registration
for some object (to use
:meth:`~logilab.common.registry.RegistryStore.register_and_replace`
for instance)

register(self, obj, registryname=None, oid=None, clear=False)

source code 
register `obj` implementation into `registryname` or
`obj.__registry__` if not specified, with identifier `oid` or
`obj.__regid__` if not specified.

If `clear` is true, all objects with the same identifier will be
previously unregistered.

register_and_replace(self, obj, replaced, registryname=None)

source code 
register `obj` implementation object into `registryname` or
`obj.__registry__` if not specified. If found, the `replaced` object
will be unregistered first (else a warning will be issued as it's
generally unexpected).

init_registration(self, path, extrapath=None)

source code 
reset registry and walk down path to return list of (path, name)
file modules to be loaded

is_reload_needed(self, path)

source code 
return True if something module changed and the registry should be
reloaded

load_module(self, module)

source code 
load objects from a module using registration_callback() when it exists