src/pyams_utils/registry.py
branchdev-tf
changeset 427 63284c98cdc1
parent 408 cf2304af0fab
child 437 b5131bd64de7
equal deleted inserted replaced
426:2022e4da3ad9 427:63284c98cdc1
    19 like security manager, medias converter, tasks scheduler and many other ones.
    19 like security manager, medias converter, tasks scheduler and many other ones.
    20 
    20 
    21 See :ref:`zca` to get a brief introduction about using a local registry with PyAMS packages.
    21 See :ref:`zca` to get a brief introduction about using a local registry with PyAMS packages.
    22 """
    22 """
    23 
    23 
    24 
       
    25 import logging
    24 import logging
    26 import threading
    25 import threading
    27 
    26 
    28 import venusian
    27 import venusian
    29 from ZODB.POSException import POSError
    28 from ZODB.POSException import POSError
    50     """
    49     """
    51 
    50 
    52     _registry = None
    51     _registry = None
    53 
    52 
    54     def get_registry(self):
    53     def get_registry(self):
       
    54         """Return local registry"""
    55         return self._registry
    55         return self._registry
    56 
    56 
    57     def set_registry(self, registry):
    57     def set_registry(self, registry):
       
    58         """Define local registry"""
    58         self._registry = registry
    59         self._registry = registry
    59 
    60 
    60 local_registry = LocalRegistry()
    61 local_registry = LocalRegistry()  # pylint: disable=invalid-name
    61 
    62 
    62 
    63 
    63 def get_local_registry():
    64 def get_local_registry():
    64     """Get local registry
    65     """Get local registry
    65 
    66 
    72     """Define local registry"""
    73     """Define local registry"""
    73     local_registry.set_registry(registry)
    74     local_registry.set_registry(registry)
    74 
    75 
    75 
    76 
    76 @subscriber(INewRequest)
    77 @subscriber(INewRequest)
    77 def handle_new_request(event):
    78 def handle_new_request(event):  # pylint: disable=unused-argument
    78     """New request event subscriber
    79     """New request event subscriber
    79 
    80 
    80     Is used to initialize local registry to None for any new request
    81     Is used to initialize local registry to None for any new request
    81     """
    82     """
    82     set_local_registry(None)
    83     set_local_registry(None)
   181     for registry in get_registries():
   182     for registry in get_registries():
   182         for utility in registry.getUtilitiesFor(interface):
   183         for utility in registry.getUtilitiesFor(interface):
   183             yield utility
   184             yield utility
   184 
   185 
   185 
   186 
   186 def get_all_utilities_registered_for(interface):
   187 def get_all_utilities_registered_for(interface):  # pylint: disable=invalid-name
   187     """Get list of registered utilities for given interface
   188     """Get list of registered utilities for given interface
   188 
   189 
   189     Do a registry lookup for matching utilities into local registry first, then on each registry
   190     Do a registry lookup for matching utilities into local registry first, then on each registry
   190     associated with current thread stack.
   191     associated with current thread stack.
   191     """
   192     """
   194         for utilities in registry.getAllUtilitiesRegisteredFor(interface):
   195         for utilities in registry.getAllUtilitiesRegisteredFor(interface):
   195             result.append(utilities)
   196             result.append(utilities)
   196     return result
   197     return result
   197 
   198 
   198 
   199 
   199 class utility_config(object):  # pylint: disable=invalid-name
   200 class utility_config:  # pylint: disable=invalid-name
   200     """Function or class decorator to register a utility in the global registry
   201     """Function or class decorator to register a utility in the global registry
   201 
   202 
   202     :param str name: default=''; name under which the utility is registered
   203     :param str name: default=''; name under which the utility is registered
   203     :param Interface provides: the interface for which the utility is registered
   204     :param Interface provides: the interface for which the utility is registered
   204 
   205 
   205     Please note that a single utility can be registered several times (using several annotations), with
   206     Please note that a single utility can be registered several times (using several annotations),
   206     different names.
   207     with different names.
   207 
   208 
   208     If several utilities are registered for the same interface with the same name, the last registered
   209     If several utilities are registered for the same interface with the same name, the last
   209     utility will override the previous ones.
   210     registered utility will override the previous ones.
   210     """
   211     """
   211 
   212 
   212     venusian = venusian
   213     venusian = venusian
   213 
   214 
   214     def __init__(self, **settings):
   215     def __init__(self, **settings):
   216 
   217 
   217     def __call__(self, wrapped):
   218     def __call__(self, wrapped):
   218         settings = self.__dict__.copy()
   219         settings = self.__dict__.copy()
   219         depth = settings.pop('_depth', 0)
   220         depth = settings.pop('_depth', 0)
   220 
   221 
   221         def callback(context, name, ob):
   222         def callback(context, name, obj):
   222             if type(ob) is type:
   223             if isinstance(obj, type):
   223                 factory = ob
   224                 factory = obj
   224                 component = None
   225                 component = None
   225             else:
   226             else:
   226                 factory = None
   227                 factory = None
   227                 component = ob
   228                 component = obj
   228 
   229 
   229             provides = settings.get('provides')
   230             provides = settings.get('provides')
   230             if provides is None:
   231             if provides is None:
   231                 if factory:
   232                 if factory:
   232                     provides = list(implementedBy(factory))
   233                     provides = list(implementedBy(factory))