merge default doc-dc
authorDamien Correia
Fri, 18 May 2018 11:32:22 +0200
branchdoc-dc
changeset 256 24d96b180e55
parent 255 ecccb445b517 (current diff)
parent 179 9fe970c45c7b (diff)
child 257 a958ccc01e40
merge default
--- a/src/pyams_utils/adapter.py	Wed May 16 14:05:38 2018 +0200
+++ b/src/pyams_utils/adapter.py	Fri May 18 11:32:22 2018 +0200
@@ -28,9 +28,13 @@
 import venusian
 
 # import interfaces
+from zope.annotation.interfaces import IAnnotations
 
 # import packages
-from zope.interface import implementedBy
+from pyams_utils.registry import get_current_registry
+from zope.interface import implementedBy, alsoProvides
+from zope.lifecycleevent import ObjectCreatedEvent
+from zope.location import locate as zope_locate
 
 
 class ContextAdapter(object):
@@ -125,3 +129,34 @@
 
         settings['_info'] = info.codeinfo  # fbo "action_method"
         return wrapped
+
+
+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
+    :param str name: if locate is not False, this is the name with which the new object is attached
+        to it's parent.
+    """
+    annotations = IAnnotations(context, None)
+    if annotations is None:
+        return None
+    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:
+            zope_locate(adapter, context if parent is None else parent, name)
+    return adapter
--- a/src/pyams_utils/doctests/request.txt	Wed May 16 14:05:38 2018 +0200
+++ b/src/pyams_utils/doctests/request.txt	Fri May 18 11:32:22 2018 +0200
@@ -13,7 +13,7 @@
     True
     >>> request = check_request()
     >>> request
-    <Request at ... GET http://localhost/>
+    <PyAMSRequest at ... GET http://localhost/>
 
 If a new request is created "from scratch", it's registry is assigned to global registry: