Package logilab :: Package common :: Module registry
[frames] | no frames]

Module registry

source code

This module provides bases for predicates dispatching (the pattern in use
here is similar to what's refered as multi-dispatch or predicate-dispatch in the
literature, though a bit different since the idea is to select across different
implementation 'e.g. classes), not to dispatch a message to a function or
method. It contains the following classes:

* :class:`RegistryStore`, the top level object which loads implementation
  objects and stores them into registries. You'll usually use it to access
  registries and their contained objects;

* :class:`Registry`, the base class which contains objects semantically grouped
  (for instance, sharing a same API, hence the 'implementation' name). You'll
  use it to select the proper implementation according to a context. Notice you
  may use registries on their own without using the store.

.. Note::

  implementation objects are usually designed to be accessed through the
  registry and not by direct instantiation, besides to use it as base classe.

The selection procedure is delegated to a selector, which is responsible for
scoring the object according to some context. At the end of the selection, if an
implementation has been found, an instance of this class is returned. A selector
is built from one or more predicates combined together using AND, OR, NOT
operators (actually `&`, `|` and `~`). You'll thus find some base classes to
build predicates:

* :class:`Predicate`, the abstract base predicate class

* :class:`AndPredicate`, :class:`OrPredicate`, :class:`NotPredicate`, which you
  shouldn't have to use directly. You'll use `&`, `|` and '~' operators between
  predicates directly

* :func:`objectify_predicate`

You'll eventually find one concrete predicate: :class:`yes`

.. autoclass:: RegistryStore
.. autoclass:: Registry

Predicates
----------
.. autoclass:: Predicate
.. autofunc:: objectify_predicate
.. autoclass:: yes

Debugging
---------
.. autoclass:: traced_selection

Exceptions
----------
.. autoclass:: RegistryException
.. autoclass:: RegistryNotFound
.. autoclass:: ObjectNotFound
.. autoclass:: NoSelectableObject

Classes
  RegistryException
Base class for registry exception.
  RegistryNotFound
Raised when an unknown registry is requested.
  ObjectNotFound
Raised when an unregistered object is requested.
  NoSelectableObject
Raised when no object is selectable for a given context.
  Registry
The registry store a set of implementations associated to identifier:
  RegistryStore
This class is responsible for loading implementations and storing them in their registry which are created on the fly as needed.
  traced_selection
Typical usage is :
  PredicateMetaClass
  Predicate
base class for selector classes providing implementation for operators ``&``, ``|`` and ``~``
  MultiPredicate
base class for compound selector classes
  AndPredicate
and-chained selectors
  OrPredicate
or-chained selectors
  NotPredicate
negation selector
  yes
Return the score given as parameter, with a default score of 0.5 so any other selector take precedence.
Functions
 
classid(cls)
returns a unique identifier for an object class
source code
 
class_registries(cls, registryname) source code
 
objectify_predicate(selector_func)
Most of the time, a simple score function is enough to build a selector.
source code
 
wrap_predicates(decorator) source code
Variables
  TRACED_OIDS = None
hash(x)
Function Details

objectify_predicate(selector_func)

source code 
Most of the time, a simple score function is enough to build a selector.
The :func:`objectify_predicate` decorator turn it into a proper selector
class::

    @objectify_predicate
    def one(cls, req, rset=None, **kwargs):
        return 1

    class MyView(View):
        __select__ = View.__select__ & one()