Added "markers" to adapter annotations factory helper
authorThierry Florac <thierry.florac@onf.fr>
Fri, 18 May 2018 11:17:56 +0200
changeset 179 9fe970c45c7b
parent 178 dfeb8bea6602
child 180 1bb9673c3be1
child 256 24d96b180e55
Added "markers" to adapter annotations factory helper
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: