# HG changeset patch # User Thierry Florac # Date 1526635076 -7200 # Node ID 9fe970c45c7b2d239d4c92fb363acec5d4389066 # Parent dfeb8bea6602e68c5ba2cbf698bb0bdfeb690572 Added "markers" to adapter annotations factory helper diff -r dfeb8bea6602 -r 9fe970c45c7b src/pyams_utils/adapter.py --- a/src/pyams_utils/adapter.py Fri May 18 11:07:15 2018 +0200 +++ b/src/pyams_utils/adapter.py Fri May 18 11:17:56 2018 +0200 @@ -32,7 +32,7 @@ # import packages from pyams_utils.registry import get_current_registry -from zope.interface import implementedBy +from zope.interface import implementedBy, alsoProvides from zope.lifecycleevent import ObjectCreatedEvent from zope.location import locate as zope_locate @@ -131,13 +131,15 @@ return wrapped -def get_annotation_adapter(context, key, factory, notify=True, locate=True, parent=None, name=None): +def get_annotation_adapter(context, key, factory, markers=None, notify=True, + locate=True, parent=None, name=None): """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 create a new object + :param markers: if not None, list of marker interfaces which created adapter should provide :param bool notify: if 'False', no notification event will be sent on object creation :param bool locate: if 'False', the new object is not attached to any parent :param object parent: parent to which new object is attached @@ -150,6 +152,9 @@ adapter = annotations.get(key) if adapter is None: adapter = annotations[key] = factory() + if markers: + for marker in markers: + alsoProvides(adapter, marker) if notify: get_current_registry().notify(ObjectCreatedEvent(adapter)) if locate: