--- a/src/pyams_utils/adapter.py Wed Nov 20 19:26:23 2019 +0100
+++ b/src/pyams_utils/adapter.py Fri Nov 22 18:51:37 2019 +0100
@@ -12,8 +12,8 @@
"""Adapters management package
-This package provides a small set of standard base adapters for *context*, *context* and *request*, and
-*context* and *request* and *view*.
+This package provides a small set of standard base adapters for *context*, *context* and *request*,
+and *context* and *request* and *view*.
See :ref:`zca` to see how PyAMS can help components management.
"""
@@ -27,22 +27,22 @@
from zope.location import locate as zope_locate
from pyams_utils.factory import get_object_factory, is_interface
-from pyams_utils.registry import get_current_registry
+from pyams_utils.registry import get_current_registry, get_global_registry
__docformat__ = 'restructuredtext'
-logger = logging.getLogger('PyAMS (utils)')
+LOGGER = logging.getLogger('PyAMS (utils)')
-class ContextAdapter(object):
+class ContextAdapter:
"""Context adapter"""
def __init__(self, context):
self.context = context
-class ContextRequestAdapter(object):
+class ContextRequestAdapter:
"""Context + request multi-adapter"""
def __init__(self, context, request):
@@ -50,7 +50,7 @@
self.request = request
-class ContextRequestViewAdapter(object):
+class ContextRequestViewAdapter:
"""Context + request + view multi-adapter"""
def __init__(self, context, request, view):
@@ -59,17 +59,17 @@
self.view = view
-class NullAdapter(object):
+class NullAdapter:
"""An adapter which always return None!
Can be useful to override a default adapter...
"""
- def __new__(cls, *arsg, **kwargs):
+ def __new__(cls, *args, **kwargs): # pylint: disable=unused-argument
return None
-class adapter_config(object):
+class adapter_config: # pylint: disable=invalid-name
"""Function or class decorator to declare an adapter
Annotation parameters can be:
@@ -77,6 +77,7 @@
:param str='' name: name of the adapter
:param [Interface...] context: an interface, or a tuple of interfaces, that the component adapts
:param Interface provides: the interface that the adapter provides
+ :param registry: the registry into which adapter registration should be made
"""
venusian = venusian
@@ -91,48 +92,49 @@
settings = self.__dict__.copy()
depth = settings.pop('_depth', 0)
- def callback(context, name, ob):
+ def callback(context, name, obj):
adapts = settings.get('context')
if adapts is None:
- adapts = getattr(ob, '__component_adapts__', None)
+ adapts = getattr(obj, '__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)
+ "can't determine what the factory adapts." % obj)
if not isinstance(adapts, tuple):
adapts = (adapts,)
provides = settings.get('provides')
if provides is None:
- intfs = list(implementedBy(ob))
+ intfs = list(implementedBy(obj))
if len(intfs) == 1:
provides = intfs[0]
if provides is None:
raise TypeError("Missing 'provides' argument")
- config = context.config.with_package(info.module)
- logger.debug("Registering adapter {0} for {1} providing {2}".format(str(ob),
- str(adapts),
- str(provides)))
- config.registry.registerAdapter(ob, adapts, provides, settings.get('name', ''))
+ config = context.config.with_package(info.module) # pylint: disable=no-member
+ LOGGER.debug("Registering adapter %s for %s providing %s",
+ str(obj), str(adapts), str(provides))
+ registry = settings.get('registry', config.registry)
+ registry.registerAdapter(obj, adapts, provides, settings.get('name', ''))
info = self.venusian.attach(wrapped, callback, category='pyams_adapter',
depth=depth + 1)
- if info.scope == 'class':
+ if info.scope == 'class': # pylint: disable=no-member
# 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"
+ settings['_info'] = info.codeinfo # pylint: disable=no-member
return wrapped
def get_annotation_adapter(context, key, factory=None, markers=None, notify=True,
locate=True, parent=None, name=None, callback=None, **kwargs):
+ # pylint: disable=too-many-arguments
"""Get an adapter via object's annotations, creating it if not existent
-
+
:param object context: context object which should be adapted
:param str key: annotations key to look for
:param factory: if annotations key is not found, this is the factory which will be used to
@@ -151,28 +153,27 @@
annotations = IAnnotations(context, None)
if annotations is None:
return None
- adapter = annotations.get(key)
+ adapter = annotations.get(key) # pylint: disable=assignment-from-no-return
if adapter is None:
if 'default' in kwargs:
return kwargs['default']
- elif factory is None:
+ if factory is None:
return None
- else:
- if is_interface(factory):
- factory = get_object_factory(factory)
- assert factory is not None, "Missing object factory"
- adapter = annotations[key] = factory()
- if markers:
- if not isinstance(markers, (list, tuple, set)):
- markers = {markers}
- for marker in markers:
- alsoProvides(adapter, marker)
- if notify:
- get_current_registry().notify(ObjectCreatedEvent(adapter))
- if locate:
- zope_locate(adapter, context if parent is None else parent, name)
- if callback:
- callback(adapter)
+ if is_interface(factory):
+ factory = get_object_factory(factory)
+ assert factory is not None, "Missing object factory"
+ adapter = annotations[key] = factory()
+ if markers:
+ if not isinstance(markers, (list, tuple, set)):
+ markers = {markers}
+ for marker in markers:
+ alsoProvides(adapter, marker)
+ if notify:
+ get_current_registry().notify(ObjectCreatedEvent(adapter))
+ if locate:
+ zope_locate(adapter, context if parent is None else parent, name)
+ if callback:
+ callback(adapter)
return adapter