[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/OFS/Annotation - AttributeAnnotations.py:1.1.2.3

Steve Alexander steve@cat-box.net
Tue, 28 May 2002 10:07:23 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Annotation
In directory cvs.zope.org:/tmp/cvs-serv7818/OFS/Annotation

Modified Files:
      Tag: Zope-3x-branch
	AttributeAnnotations.py 
Log Message:
made attribute annotations wrap what they return in the object that
holds the annotation.


=== Zope3/lib/python/Zope/App/OFS/Annotation/AttributeAnnotations.py 1.1.2.2 => 1.1.2.3 ===
 from Persistence.BTrees.OOBTree import OOBTree
 from IAnnotations import IAnnotations
+from Zope.Proxy.ProxyIntrospection import removeAllProxies
+from Zope.Proxy.ContextWrapper import ContextWrapper
 
 class AttributeAnnotations:
-
     """
     Store annotations in the __annotations__ attribute on a
     IAttributeAnnotatable object.
@@ -33,24 +34,32 @@
         # for now, we'll leave it to users of annotations to do that.
         # Users of annotations will typically need to do their own
         # unwrapping anyway.
-        self.obj = obj
+        
+        self.wrapped_obj = obj
+        self.unwrapped_obj = removeAllProxies(obj)
         
     def __getitem__(self, key):
-        annotations = getattr(self.obj, '__annotations__', {})
-        return annotations[key]
+        annotations = getattr(self.unwrapped_obj, '__annotations__', {})
+        return ContextWrapper(annotations[key], self.wrapped_obj)
         
     def get(self, key, default=None):
         try:
-            return self.obj.__annotations__.get(key, default)
+            value = self.unwrapped_obj.__annotations__.get(key, default)
         except AttributeError:
+            # I guess default shouldn't be wrapped.
             return default
+        else:
+            return ContextWrapper(value, self.wrapped_obj)
 
-    def __getattr__(self, attr):
+    def __getattr__(self, name):
+        # this method is for getting methods and attributes of the
+        # mapping object used to store annotations.
         try:
-            return getattr(self.obj.__annotations__, attr)
+            attr = getattr(self.unwrapped_obj.__annotations__, name)
         except AttributeError:
-            if not hasattr(self.obj, '__annotations__'):
-                annotations = self.obj.__annotations__ = OOBTree()
-                return getattr(annotations, attr)
-            raise
-            
+            if not hasattr(self.unwrapped_obj, '__annotations__'):
+                annotations = self.unwrapped_obj.__annotations__ = OOBTree()
+                attr = getattr(annotations, name)
+            else:
+                raise
+        return ContextWrapper(attr, self.wrapped_obj)