[Checkins] SVN: zope.generic/trunk/src/zope/generic/directlyprovides/property.py fix:the ProvidesProperty should be also declaration from a class perspective

Dominik Huber dominik.huber at perse.ch
Wed May 10 11:25:38 EDT 2006


Log message for revision 68075:
  fix:the ProvidesProperty should be also declaration from a class perspective

Changed:
  U   zope.generic/trunk/src/zope/generic/directlyprovides/property.py

-=-
Modified: zope.generic/trunk/src/zope/generic/directlyprovides/property.py
===================================================================
--- zope.generic/trunk/src/zope/generic/directlyprovides/property.py	2006-05-10 15:19:22 UTC (rev 68074)
+++ zope.generic/trunk/src/zope/generic/directlyprovides/property.py	2006-05-10 15:25:37 UTC (rev 68075)
@@ -16,21 +16,20 @@
 $Id$
 """
 import sys
+
 from zope.event import notify
+from zope.interface import Declaration
 from zope.interface import directlyProvidedBy
 from zope.interface import directlyProvides
 from zope.interface import implementedBy
-from zope.interface.declarations import Declaration
+from zope.interface.advice import addClassAdvisor
 
 from zope.generic.directlyprovides.event import DirectlyProvidesModifiedEvent
 from zope.generic.directlyprovides.helper import assertListOfInterfaces
 from zope.generic.directlyprovides.helper import updateDirectlyProvided
 
-_marker = object()
 
-
 def hack_checker(value, length, inst):
-    from zope.interface.declarations import Declaration
     flattened = [i for i in value.flattened()]
 
     # precondition for the hack checker
@@ -48,22 +47,37 @@
     return True
 
 
-class ProvidesProperty(object):
+
+def _getclass(cls):
+    return cls
+
+class ProvidesProperty(Declaration):
     """Prepend the values of the declared names to the directly provided interfaces."""
 
     def __init__(self, *names):
         self.__names = names
+        # TODO: This is realy a super hack
+        # From class perspective this property has to behave like a class provides
+        # declaration, from instance perspective this is only a property
+        # This mechanism will breack if somebody uses 
+        # directlyProvides(class, *interfaces). Maybe we have to introduce a 
+        # meta class for that purpose
+        cls = addClassAdvisor(_getclass, depth=3)
+        metacls = type(cls)
+        interfaces = ()
+        self._implements = implementedBy(cls)
+        self.__args = (cls, metacls, ) + interfaces
+        Declaration.__init__(self, *(interfaces + (implementedBy(metacls), )))
 
-    def __get__(self, inst, klass):
+    def __get__(self, inst, cls):
         if inst is None:
             return self
 
-        value = inst.__dict__.get('__provides__', _marker)
-        if value is _marker:
+        try:
+            return inst.__dict__['__provides__']
+        except:
             raise AttributeError('__provides__')
 
-        return value
-
     def __set__(self, inst, value):
         # remove a provides declaration
         if value is None:
@@ -123,6 +137,8 @@
 
         return prependes
 
+    def __reduce__(self):
+        return self.__class__, self.__args
 
 
 def provides(*names):
@@ -189,6 +205,8 @@
 
 
 
+_marker = object()
+
 class UpdateProvides(object):
     """Update the provides attribute after a new value is set.
     
@@ -203,7 +221,7 @@
         self.__after = after
         self.__value_hook = value_hook
 
-    def __get__(self, inst, klass):
+    def __get__(self, inst, cls):
         if inst is None:
             return self
 



More information about the Checkins mailing list