[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/OFS/Annotation - AttributeAnnotations.py:1.2 IAnnotatable.py:1.2 IAnnotations.py:1.2 IAttributeAnnotatable.py:1.2 __init__.py:1.2 annotation.zcml:1.2

Jim Fulton jim@zope.com
Mon, 10 Jun 2002 19:28:21 -0400


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

Added Files:
	AttributeAnnotations.py IAnnotatable.py IAnnotations.py 
	IAttributeAnnotatable.py __init__.py annotation.zcml 
Log Message:
Merged Zope-3x-branch into newly forked Zope3 CVS Tree.


=== Zope3/lib/python/Zope/App/OFS/Annotation/AttributeAnnotations.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+# 
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+# 
+##############################################################################
+"""
+
+$Id$
+"""
+
+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.
+    """
+    
+    __implements__ = IAnnotations
+
+    def __init__(self, obj):
+        # We could remove all proxies from obj at this point, but
+        # 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.wrapped_obj = obj
+        self.unwrapped_obj = removeAllProxies(obj)
+        
+    def __getitem__(self, key):
+        annotations = getattr(self.unwrapped_obj, '__annotations__', {})
+        return ContextWrapper(annotations[key], self.wrapped_obj)
+        
+    def get(self, key, default=None):
+        try:
+            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, name):
+        # this method is for getting methods and attributes of the
+        # mapping object used to store annotations.
+        try:
+            attr = getattr(self.unwrapped_obj.__annotations__, name)
+        except AttributeError:
+            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)


=== Zope3/lib/python/Zope/App/OFS/Annotation/IAnnotatable.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE
+#
+##############################################################################
+"""
+
+$Id$
+"""
+from Interface import Interface
+from Interface.Attribute import Attribute
+
+class IAnnotatable(Interface):
+    """
+    Marker interface for objects that support storing annotations.
+    
+    This interface says "There exists an adapter to an IAnnotations
+    for an object that implements IAnnotatable".
+    
+    Classes should not directly declare that they implement this interface.
+    Instead they should implement an interface derived from this one, which
+    details how the annotations are to be stored, such as
+    IAttributeAnnotatable.
+    """


=== Zope3/lib/python/Zope/App/OFS/Annotation/IAnnotations.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+# 
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+# 
+##############################################################################
+"""
+
+$Id$
+"""
+from IAnnotatable import IAnnotatable
+from Interface import Interface
+
+class IAnnotations(IAnnotatable):
+    """
+    Annotations store arbitrary application data under package unique keys
+    """
+
+    def __getitem__(key):
+        """
+        Return the annotation stored under key.
+
+        Raises KeyError if key not found.
+        """
+
+    def get(key, default=None):
+        """
+        Return the annotation stored under key, returning default if not found.
+        """
+
+    def __setitem__(key, memento):
+        """
+        Store annotation under key.
+
+        In order to avoid key collisions, users of this interface must
+        use their dotted package name as part of the key name.
+        """
+
+    def __delitem__(key):
+        """
+        Removes the annotation stored under key.
+
+        Raises a KeyError if the key is not found.
+        """


=== Zope3/lib/python/Zope/App/OFS/Annotation/IAttributeAnnotatable.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+# 
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+# 
+##############################################################################
+"""
+
+$Id$
+"""
+from IAnnotatable import IAnnotatable
+from Interface.Attribute import Attribute
+
+class IAttributeAnnotatable(IAnnotatable):
+    """
+    Marker interface giving permission for an IAnnotations adapter to store
+    data in an an attribute named __annotations__.
+    """
+
+    __annotations__ = Attribute(
+        """
+        This attribute may be used by an IAnnotations adapter to
+        store pickleable data in.  The object implementing this
+        interface promises not to touch the attribute other than
+        to persist it.
+        """)


=== Zope3/lib/python/Zope/App/OFS/Annotation/__init__.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+# 
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+# 
+##############################################################################
+ 


=== Zope3/lib/python/Zope/App/OFS/Annotation/annotation.zcml 1.1 => 1.2 ===
+    xmlns='http://namespaces.zope.org/zope'
+>
+
+  <adapter
+      factory=".AttributeAnnotations."
+      provides=".IAnnotations."
+      for=".IAttributeAnnotatable." />
+
+</zopeConfigure>