[Zope3-checkins] SVN: Zope3/trunk/src/zope/a o Added an optional
context argument to the standard IAnnotations adapters.
Jacob Holm
jh at improva.dk
Mon Mar 5 16:36:40 EST 2007
Log message for revision 72995:
o Added an optional context argument to the standard IAnnotations adapters.
The argument is ignored by the IAttributeAnnotatable adapter, and used to
look up a IPrincipalAnnotationUtility by the IPrincipal adapter.
o Added registrations for the two-argument form of the IAnnotations adapters.
o Changed zope.app.preference.preference to use the two-argument form, undoing
a semantic change unwittingly introduced (by me) in revision 72783.
Changed:
U Zope3/trunk/src/zope/annotation/attribute.py
U Zope3/trunk/src/zope/annotation/configure.zcml
U Zope3/trunk/src/zope/app/preference/README.txt
U Zope3/trunk/src/zope/app/preference/preference.py
U Zope3/trunk/src/zope/app/principalannotation/__init__.py
U Zope3/trunk/src/zope/app/principalannotation/configure.zcml
-=-
Modified: Zope3/trunk/src/zope/annotation/attribute.py
===================================================================
--- Zope3/trunk/src/zope/annotation/attribute.py 2007-03-05 17:51:17 UTC (rev 72994)
+++ Zope3/trunk/src/zope/annotation/attribute.py 2007-03-05 21:36:39 UTC (rev 72995)
@@ -32,7 +32,7 @@
interface.implements(interfaces.IAnnotations)
component.adapts(interfaces.IAttributeAnnotatable)
- def __init__(self, obj):
+ def __init__(self, obj, context=None):
self.obj = obj
def __nonzero__(self):
Modified: Zope3/trunk/src/zope/annotation/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/annotation/configure.zcml 2007-03-05 17:51:17 UTC (rev 72994)
+++ Zope3/trunk/src/zope/annotation/configure.zcml 2007-03-05 21:36:39 UTC (rev 72995)
@@ -6,4 +6,10 @@
factory=".attribute.AttributeAnnotations"
/>
+ <adapter
+ for=".interfaces.IAttributeAnnotatable *"
+ provides=".interfaces.IAnnotations"
+ factory=".attribute.AttributeAnnotations"
+ />
+
</configure>
Modified: Zope3/trunk/src/zope/app/preference/README.txt
===================================================================
--- Zope3/trunk/src/zope/app/preference/README.txt 2007-03-05 17:51:17 UTC (rev 72994)
+++ Zope3/trunk/src/zope/app/preference/README.txt 2007-03-05 21:36:39 UTC (rev 72995)
@@ -106,18 +106,19 @@
>>> class PrincipalAnnotations(dict):
... zope.interface.implements(IAnnotations)
... data = {}
- ... def __new__(class_, context):
+ ... def __new__(class_, principal, context):
... try:
- ... annotations = class_.data[context.id]
+ ... annotations = class_.data[principal.id]
... except KeyError:
... annotations = dict.__new__(class_)
- ... class_.data[context.id] = annotations
+ ... class_.data[principal.id] = annotations
... return annotations
- ... def __init__(self, context):
+ ... def __init__(self, principal, context):
... pass
>>> from zope.app.testing import ztapi
- >>> ztapi.provideAdapter(Principal, IAnnotations, PrincipalAnnotations)
+ >>> ztapi.provideAdapter((Principal, zope.interface.Interface), IAnnotations,
+ ... PrincipalAnnotations)
Let's now try to access the settings again:
Modified: Zope3/trunk/src/zope/app/preference/preference.py
===================================================================
--- Zope3/trunk/src/zope/app/preference/preference.py 2007-03-05 17:51:17 UTC (rev 72994)
+++ Zope3/trunk/src/zope/app/preference/preference.py 2007-03-05 21:36:39 UTC (rev 72995)
@@ -19,6 +19,7 @@
from BTrees.OOBTree import OOBTree
import zope.interface
+import zope.component
from zope.schema import getFields
from zope.security.checker import CheckerPublic, Checker, defineChecker
from zope.security.management import getInteraction
@@ -176,7 +177,7 @@
def data(self):
# TODO: what if we have multiple participations?
principal = getInteraction().participations[0].principal
- ann = IAnnotations(principal)
+ ann = zope.component.getMultiAdapter((principal, self), IAnnotations)
# If no preferences exist, create the root preferences object.
if ann.get(pref_key) is None:
Modified: Zope3/trunk/src/zope/app/principalannotation/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/principalannotation/__init__.py 2007-03-05 17:51:17 UTC (rev 72994)
+++ Zope3/trunk/src/zope/app/principalannotation/__init__.py 2007-03-05 21:36:39 UTC (rev 72995)
@@ -107,7 +107,7 @@
def get(self, key, default=None):
return self.data.get(key, default)
-def annotations(principal):
+def annotations(principal, context=None):
"""adapt principal to annotations via principal annotation utility.
To illustrate, we'll register the adapter and a dummy
@@ -136,7 +136,7 @@
>>> annotation is dummy_annotation
True
""" # TODO move this out to a doctest file when we have one...
- utility = component.getUtility(IPrincipalAnnotationUtility)
+ utility = component.getUtility(IPrincipalAnnotationUtility, context=context)
return utility.getAnnotations(principal)
component.adapter(zope.security.interfaces.IPrincipal)(annotations)
interface.implementer(IAnnotations)(annotations)
Modified: Zope3/trunk/src/zope/app/principalannotation/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/principalannotation/configure.zcml 2007-03-05 17:51:17 UTC (rev 72994)
+++ Zope3/trunk/src/zope/app/principalannotation/configure.zcml 2007-03-05 21:36:39 UTC (rev 72995)
@@ -32,4 +32,9 @@
<adapter factory=".annotations" />
+ <adapter
+ for="zope.security.interfaces.IPrincipal *"
+ factory=".annotations"
+ />
+
</configure>
More information about the Zope3-Checkins
mailing list