[Zope3-checkins] CVS: Zope3/src/zope/app/annotation - __init__.py:1.1 attribute.py:1.1 configure.zcml:1.1 interfaces.py:1.1

Stephan Richter srichter at cosmos.phy.tufts.edu
Sat Mar 13 18:00:40 EST 2004


Update of /cvs-repository/Zope3/src/zope/app/annotation
In directory cvs.zope.org:/tmp/cvs-serv24548/src/zope/app/annotation

Added Files:
	__init__.py attribute.py configure.zcml interfaces.py 
Log Message:


Created annotation package in zope.app.



Moved annotation interfaces to zope.app.annotation.interfaces.



Moved attributeannotations to zope.app.annotation.attribute.


=== Added File Zope3/src/zope/app/annotation/__init__.py ===
# Import this.


=== Added File Zope3/src/zope/app/annotation/attribute.py ===
##############################################################################
#
# 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.
#
##############################################################################
"""Attribute Annotations implementation 

$Id: attribute.py,v 1.1 2004/03/13 23:00:39 srichter Exp $
"""
from BTrees.OOBTree import OOBTree
from interfaces import IAnnotations, IAttributeAnnotatable
from zope.proxy import removeAllProxies
from zope.interface import implements
from zope.app.location.interfaces import ILocation

class AttributeAnnotations:
    """Store annotations in the __annotations__ attribute on a
       'IAttributeAnnotatable' object.
    """
    implements(IAnnotations)
    __used_for__ = IAttributeAnnotatable

    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):
        """See zope.app.annotation.interfaces.IAnnotations"""
        annotations = getattr(self.unwrapped_obj, '__annotations__', None)
        if annotations is None:
            raise KeyError, key
        return annotations[key]

    def __setitem__(self, key, value):
        """See zope.app.annotation.interfaces.IAnnotations"""
        if ILocation.providedBy(value):
            value.__parent__ = self.unwrapped_obj

        try:
            annotations = self.unwrapped_obj.__annotations__
        except AttributeError:
            annotations = self.unwrapped_obj.__annotations__ = OOBTree()

        annotations[key] = value

    def __delitem__(self, key):
        """See zope.app.interfaces.annotation.IAnnotations"""
        try:
            del self.unwrapped_obj.__annotations__[key]
        except AttributeError:
            raise KeyError, key

    def get(self, key, default=None):
        """See zope.app.annotation.interfaces.IAnnotations"""
        try:
            return self.unwrapped_obj.__annotations__.get(key, default)
        except AttributeError:
            # I guess default shouldn't be wrapped.
            return default

    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 attr


=== Added File Zope3/src/zope/app/annotation/configure.zcml ===
<configure 
    xmlns="http://namespaces.zope.org/zope">


  <adapter
      for=".interfaces.IAttributeAnnotatable"
      provides=".interfaces.IAnnotations"
      factory=".attribute.AttributeAnnotations"
      />

</configure>


=== Added File Zope3/src/zope/app/annotation/interfaces.py ===
##############################################################################
#
# 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.
#
##############################################################################
"""Annotations store arbitrary application data under package-unique keys.

$Id: interfaces.py,v 1.1 2004/03/13 23:00:39 srichter Exp $
"""
from zope.interface import Interface

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.
    """

class IAnnotations(IAnnotatable):
    """Stores arbitrary application data under package-unique keys.

    By "package-unique keys", we mean keys that are are unique by
    virtua of including the dotted name of a package as a prefex.  A
    package name is used to limit the authority for picking names for
    a package to the people using that package.

    For example, when implementing annotations for storing Zope
    Dublin-Core meta-data, we use the key::

      "zope.app.dublincore.ZopeDublinCore"

    """

    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, or default if not found.
        """

    def __setitem__(key, value):
        """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.
        """

class IAttributeAnnotatable(IAnnotatable):
    """Marker indicating that annotations can be stored on an attribute.
    
    This is a marker interface giving permission for an IAnnotations
    adapter to store data in an attribute named __annotations__.

    """




More information about the Zope3-Checkins mailing list