--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_utils/registry.py Thu Feb 19 00:46:48 2015 +0100
@@ -0,0 +1,123 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+import venusian
+
+# import interfaces
+from zope.component.interfaces import ComponentLookupError
+
+# import packages
+from pyramid.threadlocal import get_current_registry, get_current_request
+from zope.interface import implementedBy, providedBy
+
+
+def _get_registries():
+ """Get list of component registries"""
+ registry = get_current_registry()
+ yield registry
+ request = get_current_request()
+ if (request is not None) and (request.registry != registry):
+ yield request.registry
+
+
+def registered_utilities():
+ """Get utilities registrations as generator"""
+ for registry in _get_registries():
+ for utility in registry.registeredUtilities():
+ yield utility
+
+
+def query_utility(provided, name='', default=None):
+ """Query utility registered with given interface"""
+ for registry in _get_registries():
+ utility = registry.queryUtility(provided, name, default)
+ if utility is not None:
+ return utility
+ return default
+
+
+def get_utility(provided, name=''):
+ """Get utility registered with given interface"""
+ for registry in _get_registries():
+ utility = registry.queryUtility(provided, name)
+ if utility is not None:
+ return utility
+ raise ComponentLookupError(provided, name)
+
+
+def get_utilities_for(interface):
+ """Get utilities registered with given interface as (name, util)"""
+ for registry in _get_registries():
+ for utility in registry.getUtilitiesFor(interface):
+ yield utility
+
+
+def get_all_utilities_registered_for(interface):
+ """Get list of registered utilities for given interface"""
+ result = []
+ for registry in _get_registries():
+ for utilities in registry.getAllUtilitiesRegisteredFor(interface):
+ result.extend(utilities)
+ return result
+
+
+class utility_config(object):
+ """Function or class decorator to declare a utility"""
+
+ venusian = venusian
+
+ def __init__(self, **settings):
+ self.__dict__.update(settings)
+
+ def __call__(self, wrapped):
+ settings = self.__dict__.copy()
+ depth = settings.pop('_depth', 0)
+
+ def callback(context, name, ob):
+ if type(ob) is type:
+ factory = ob
+ component = None
+ else:
+ factory = None
+ component = ob
+
+ provides = settings.get('provides')
+ if provides is None:
+ if factory:
+ provides = list(implementedBy(factory))
+ else:
+ provides = list(providedBy(component))
+ if len(provides) == 1:
+ provides = provides[0]
+ else:
+ raise TypeError("Missing 'provides' argument")
+
+ config = context.config.with_package(info.module)
+ config.registry.registerUtility(component=component, factory=factory,
+ provided=provides, name=settings.get('name', ''))
+
+ info = self.venusian.attach(wrapped, callback, category='pyams_utility',
+ depth=depth + 1)
+
+ if info.scope == 'class':
+ # if the decorator was attached to a method in a class, or
+ # otherwise executed at class scope, we need to set an
+ # 'attr' into the settings if one isn't already in there
+ if settings.get('attr') is None:
+ settings['attr'] = wrapped.__name__
+
+ settings['_info'] = info.codeinfo # fbo "action_method"
+ return wrapped