src/pyams_utils/adapter.py
changeset 186 c08b45bbed38
parent 185 b713404fd6e5
child 193 59bd282a06f8
--- a/src/pyams_utils/adapter.py	Fri May 25 15:12:17 2018 +0200
+++ b/src/pyams_utils/adapter.py	Mon May 28 14:07:10 2018 +0200
@@ -132,7 +132,7 @@
 
 
 def get_annotation_adapter(context, key, factory=None, markers=None, notify=True,
-                           locate=True, parent=None, name=None):
+                           locate=True, parent=None, name=None, callback=None, **kwargs):
     """Get an adapter via object's annotations, creating it if not existent
     
     :param object context: context object which should be adapted
@@ -145,20 +145,26 @@
     :param object=None parent: parent to which new object is attached
     :param str=None name: if locate is not False, this is the name with which the new object is attached
         to it's parent.
+    :param callback: if not None, callback function which will be called after
     """
     annotations = IAnnotations(context, None)
     if annotations is None:
         return None
     adapter = annotations.get(key)
     if adapter is None:
-        if factory is None:
+        if 'default' in kwargs:
+            return kwargs['default']
+        elif factory is None:
             return None
-        adapter = annotations[key] = factory()
-        if 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)
+        else:
+            adapter = annotations[key] = factory()
+            if 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