[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/OFS/Services/PrincipalAnnotationService - configure.zcml:1.1 __init__.py:1.1 PrincipalAnnotationService.py:1.1 IPrincipalAnnotationService.py:1.1

Itamar Shtull-Trauring zope@itamarst.org
Wed, 4 Dec 2002 05:41:33 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Services/PrincipalAnnotationService
In directory cvs.zope.org:/tmp/cvs-serv20946/PrincipalAnnotationService

Added Files:
	configure.zcml __init__.py PrincipalAnnotationService.py 
	IPrincipalAnnotationService.py 
Log Message:
service for storing annotations for principals


=== Added File Zope3/lib/python/Zope/App/OFS/Services/PrincipalAnnotationService/configure.zcml ===
<zopeConfigure
   xmlns='http://namespaces.zope.org/zope'
   xmlns:browser='http://namespaces.zope.org/browser'
   xmlns:service='http://namespaces.zope.org/service'
>
  <serviceType
      id="PrincipalAnnotation" 
      interface=".IPrincipalAnnotationService." />

  <content class=".PrincipalAnnotationService.">
    <require
        permission="Zope.Public"
        interface=".IPrincipalAnnotationService." />
    <factory
        id="IPrincipalAnnotationService"
        permission="Zope.ManageServices" />
  <implements interface="Zope.App.OFS.Annotation.IAttributeAnnotatable." /> 
  </content>

  <browser:menuItem menu="add_component" for="Zope.App.OFS.Container.IAdding." 
     action="IPrincipalAnnotationService"  title='Principal Annotation Service'
     description='Stores Annotations for Principals' />

</zopeConfigure>


=== Added File Zope3/lib/python/Zope/App/OFS/Services/PrincipalAnnotationService/__init__.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.
# 
##############################################################################


=== Added File Zope3/lib/python/Zope/App/OFS/Services/PrincipalAnnotationService/PrincipalAnnotationService.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.
# 
##############################################################################

"""Implementation of IPrincipalAnnotationService."""

# TODO: register service as adapter for IAnnotations on service activation
# this depends on existence of LocalAdapterService, so once that's done
# implement this.

# Zope3 imports
from Persistence import Persistent
from Persistence.BTrees.OOBTree import OOBTree
from Zope.App.ComponentArchitecture.NextService import getNextService
from Zope.ContextWrapper import ContextMethod
from Zope.Proxy.ContextWrapper import ContextWrapper
from Zope.App.OFS.Annotation.IAnnotations import IAnnotations

# Sibling imports
from IPrincipalAnnotationService import IPrincipalAnnotationService


class PrincipalAnnotationService(Persistent):
    """Stores IAnnotations for IPrinicipals.

    The service ID is 'PrincipalAnnotation'.
    """

    __implements__ = IPrincipalAnnotationService, Persistent.__implements__

    def __init__(self):
        self.annotations = OOBTree()


    # implementation of IPrincipalAnnotationService
    
    def getAnnotation(self, principalId):
        """Return object implementing IAnnotations for the givin principal.

        If there is no IAnnotations it will be created and then returned.
        """
        if not self.annotations.has_key(principalId):
            self.annotations[principalId] = Annotations(principalId)
        return ContextWrapper(self.annotations[principalId], self, name=principalId)

    getAnnotation = ContextMethod(getAnnotation)
    
    def hasAnnotation(self, principalId):
        """Return boolean indicating if given principal has IAnnotations."""
        return self.annotations.has_key(principalId)


class Annotations(Persistent):
    """Stores annotations."""

    __implements__ = IAnnotations, Persistent.__implements__

    def __init__(self, principalId):
        self.principalId = principalId
        self.data = OOBTree()

    def __getitem__(wrapped_self, key):
        try:
            return wrapped_self.data[key]
        except KeyError:
            # We failed locally: delegate to a higher-level service.
            service = getNextService(wrapped_self, 'PrincipalAnnotation')
            if service:
                return service.getAnnotation(wrapped_self.principalId)[key]
            raise

    __getitem__ = ContextMethod(__getitem__)
    
    def __setitem__(self, key, value):
        self.data[key] = value

    def __delitem__(self, key):
        del self.data[key]

    def get(self, key, default=None):
        try:
            return self.data[key]
        except KeyError:
            return default


class AnnotationsForPrincipal(object):
    """Adapter from IPrincipal to IAnnotations for a PrincipalAnnotationService.

    Register an *instance* of this class as an adapter.
    """

    def __init__(self, service):
        self.service = service

    def __call__(self, principal):
        return self.service.getAnnotation(principal.getId())


=== Added File Zope3/lib/python/Zope/App/OFS/Services/PrincipalAnnotationService/IPrincipalAnnotationService.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.
# 
##############################################################################

"""Service for storing IAnnotations for principals."""

from Interface import Interface


class IPrincipalAnnotationService(Interface):
    """Stores IAnnotations for IPrinicipals."""

    def getAnnotation(principal):
        """Return object implementing IAnnotations for the givin IPrinicipal.

        If there is no IAnnotations it will be created and then returned.
        """

    def hasAnnotation(principal):
        """Return boolean indicating if given IPrincipal has IAnnotations."""