Updated annotations adapters
authorThierry Florac <thierry.florac@onf.fr>
Mon, 28 May 2018 14:07:10 +0200
changeset 186 c08b45bbed38
parent 185 b713404fd6e5
child 187 fe50c0414855
child 260 1ed791a40890
Updated annotations adapters
src/pyams_utils/adapter.py
src/pyams_utils/site.py
--- 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
--- a/src/pyams_utils/site.py	Fri May 25 15:12:17 2018 +0200
+++ b/src/pyams_utils/site.py	Mon May 28 14:07:10 2018 +0200
@@ -26,7 +26,7 @@
 
 # import packages
 from persistent.dict import PersistentDict
-from pyams_utils.adapter import adapter_config, ContextAdapter
+from pyams_utils.adapter import adapter_config, ContextAdapter, get_annotation_adapter
 from pyams_utils.registry import get_utilities_for, query_utility
 from pyramid.exceptions import NotFound
 from pyramid.path import DottedNameResolver
@@ -130,10 +130,8 @@
     if application is not None:
         try:
             hooks.setSite(application)
-            annotations = IAnnotations(application)
-            generations = annotations.get(SITE_GENERATIONS_KEY)
-            if generations is None:
-                generations = annotations[SITE_GENERATIONS_KEY] = PersistentDict()
+            generations = get_annotation_adapter(application, SITE_GENERATIONS_KEY, PersistentDict,
+                                                 notify=False, locate=False)
             for name, utility in sorted(get_utilities_for(ISiteGenerations),
                                         key=lambda x: x[1].order):
                 if not name: