src/pyams_utils/registry.py
branchdev-tf
changeset 408 cf2304af0fab
parent 405 063ccbe396de
child 419 05ff71a02b2d
equal deleted inserted replaced
407:0037199881fb 408:cf2304af0fab
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
    10 # FOR A PARTICULAR PURPOSE.
    10 # FOR A PARTICULAR PURPOSE.
    11 #
    11 #
    12 
    12 
    13 __doc__ = """Registry management package
    13 """PyAMS_utils.registry module
    14 
    14 
    15 This package is used to manage *local registry*. A local registry is a *site management* component
    15 This package is used to manage *local registry*. A local registry is a *site management* component
    16 created automatically on application startup by PyAMS_utils package. It can be used to store and register
    16 created automatically on application startup by PyAMS_utils package. It can be used to store and
    17 components, mainly utilities which are created and configured dynamically by a site administrator; this can include
    17 register components, mainly utilities which are created and configured dynamically by a site
    18 SQLAlchemy engines, ZEO connections, and several PyAMS utilities like security manager, medias converter,
    18 administrator; this can include SQLAlchemy engines, ZEO connections, and several PyAMS utilities
    19 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 
    24 
    37 from zope.traversing.interfaces import IBeforeTraverseEvent
    37 from zope.traversing.interfaces import IBeforeTraverseEvent
    38 
    38 
    39 
    39 
    40 __docformat__ = 'restructuredtext'
    40 __docformat__ = 'restructuredtext'
    41 
    41 
    42 logger = logging.getLogger('PyAMS (utils)')
    42 LOGGER = logging.getLogger('PyAMS (utils)')
    43 
    43 
    44 
    44 
    45 class LocalRegistry(threading.local):
    45 class LocalRegistry(threading.local):
    46     """Local registry
    46     """Local registry
    47 
    47 
   194         for utilities in registry.getAllUtilitiesRegisteredFor(interface):
   194         for utilities in registry.getAllUtilitiesRegisteredFor(interface):
   195             result.append(utilities)
   195             result.append(utilities)
   196     return result
   196     return result
   197 
   197 
   198 
   198 
   199 class utility_config(object):
   199 class utility_config(object):  # pylint: disable=invalid-name
   200     """Function or class decorator to register a utility in the global registry
   200     """Function or class decorator to register a utility in the global registry
   201 
   201 
   202     :param str name: default=''; name under which the utility is registered
   202     :param str name: default=''; name under which the utility is registered
   203     :param Interface provides: the interface for which the utility is registered
   203     :param Interface provides: the interface for which the utility is registered
   204 
   204 
   235                 if len(provides) == 1:
   235                 if len(provides) == 1:
   236                     provides = provides[0]
   236                     provides = provides[0]
   237                 else:
   237                 else:
   238                     raise TypeError("Missing 'provides' argument")
   238                     raise TypeError("Missing 'provides' argument")
   239 
   239 
   240             config = context.config.with_package(info.module)
   240             config = context.config.with_package(info.module)  # pylint: disable=no-member
   241             logger.debug("Registering utility {0} named '{1}' providing {2}".format(
   241             LOGGER.debug("Registering utility {0} named '{1}' providing {2}".format(
   242                 str(component) if component else str(factory),
   242                 str(component) if component else str(factory),
   243                 settings.get('name', ''),
   243                 settings.get('name', ''),
   244                 str(provides)))
   244                 str(provides)))
   245             config.registry.registerUtility(component=component, factory=factory,
   245             registry = settings.get('registry', config.registry)
   246                                             provided=provides, name=settings.get('name', ''))
   246             registry.registerUtility(component=component, factory=factory,
       
   247                                      provided=provides, name=settings.get('name', ''))
   247 
   248 
   248         info = self.venusian.attach(wrapped, callback, category='pyams_utility',
   249         info = self.venusian.attach(wrapped, callback, category='pyams_utility',
   249                                     depth=depth + 1)
   250                                     depth=depth + 1)
   250 
   251 
   251         if info.scope == 'class':
   252         if info.scope == 'class':  # pylint: disable=no-member
   252             # if the decorator was attached to a method in a class, or
   253             # if the decorator was attached to a method in a class, or
   253             # otherwise executed at class scope, we need to set an
   254             # otherwise executed at class scope, we need to set an
   254             # 'attr' into the settings if one isn't already in there
   255             # 'attr' into the settings if one isn't already in there
   255             if settings.get('attr') is None:
   256             if settings.get('attr') is None:
   256                 settings['attr'] = wrapped.__name__
   257                 settings['attr'] = wrapped.__name__
   257 
   258 
   258         settings['_info'] = info.codeinfo  # fbo "action_method"
   259         settings['_info'] = info.codeinfo  # pylint: disable=no-member
   259         return wrapped
   260         return wrapped