--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_utils/adapter.py Thu Feb 19 00:46:48 2015 +0100
@@ -0,0 +1,96 @@
+#
+# 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
+
+# import packages
+from zope.interface import implementedBy
+
+
+class ContextAdapter(object):
+ """Context adapter"""
+
+ def __init__(self, context):
+ self.context = context
+
+
+class ContextRequestAdapter(object):
+ """Context + request adapter"""
+
+ def __init__(self, context, request):
+ self.context = context
+ self.request = request
+
+
+class ContextRequestViewAdapter(object):
+ """Context + request + view adapter"""
+
+ def __init__(self, context, request, view):
+ self.context = context
+ self.request = request
+ self.view = view
+
+
+class adapter_config(object):
+ """Function or class decorator to declare an adapter"""
+
+ venusian = venusian
+
+ def __init__(self, **settings):
+ if 'for_' in settings:
+ if settings.get('context') is None:
+ settings['context'] = settings.pop('for_')
+ self.__dict__.update(settings)
+
+ def __call__(self, wrapped):
+ settings = self.__dict__.copy()
+ depth = settings.pop('_depth', 0)
+
+ def callback(context, name, ob):
+ adapts = settings.get('context')
+ if adapts is None:
+ adapts = getattr(ob, '__component_adapts__', None)
+ if adapts is None:
+ raise TypeError("No for argument was provided for %r and "
+ "can't determine what the factory adapts." % ob)
+ if not isinstance(adapts, tuple):
+ adapts = (adapts,)
+
+ provides = settings.get('provides')
+ if provides is None:
+ intfs = list(implementedBy(ob))
+ if len(intfs) == 1:
+ provides = intfs[0]
+ if provides is None:
+ raise TypeError("Missing 'provided' argument")
+
+ config = context.config.with_package(info.module)
+ config.registry.registerAdapter(ob, adapts, provides, settings.get('name', ''))
+
+ info = self.venusian.attach(wrapped, callback, category='pyams_adapter',
+ 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