[Zope3-checkins] CVS: Zope3/src/zope/context - __init__.py:1.24 interfaces.py:1.16
Steve Alexander
steve@cat-box.net
Sat, 14 Jun 2003 08:33:09 -0400
Update of /cvs-repository/Zope3/src/zope/context
In directory cvs.zope.org:/tmp/cvs-serv13348/src/zope/context
Modified Files:
__init__.py interfaces.py
Log Message:
Removed the ContextAware base class. Replaced it with
the ContextAwareDescriptors() class advice.
=== Zope3/src/zope/context/__init__.py 1.23 => 1.24 ===
--- Zope3/src/zope/context/__init__.py:1.23 Thu Jun 12 05:28:33 2003
+++ Zope3/src/zope/context/__init__.py Sat Jun 14 08:32:38 2003
@@ -22,7 +22,9 @@
__metaclass__ = type
+import sys
from zope.interface import moduleProvides
+from zope.interface.advice import addClassAdvisor
from zope.context.wrapper import getdict, getdictcreate
from zope.context.wrapper import getcontext, getinnercontext
from zope.context.wrapper import getinnerwrapper, getbaseobject
@@ -122,23 +124,24 @@
def __delete__(self, inst):
self.descriptor.__delete__(inst)
-ContextAware = None # this will be replaced a few lines down
-class ContextAwareMetaClass(type):
-
- def __init__(self, name, bases, namespace):
- if ContextAware in bases:
- for name, obj in namespace.items():
- if not isinstance(obj, ContextDescriptor):
- if getattr(obj, '__set__', None) is not None:
- d = ContextAwareDataDescriptor(obj)
- setattr(self, name, d)
- namespace[name] = d
- elif getattr(obj, '__get__', None) is not None:
- m = ContextAwareDescriptor(obj)
- setattr(self, name, m)
- namespace[name] = m
- super(ContextAwareMetaClass, self).__init__(name, bases, namespace)
-
-class ContextAware:
- __metaclass__ = ContextAwareMetaClass
+def _context_aware_advice(cls):
+ for name, obj in cls.__dict__.items():
+ if not isinstance(obj, ContextDescriptor):
+ if getattr(obj, '__set__', None) is not None:
+ d = ContextAwareDataDescriptor(obj)
+ setattr(cls, name, d)
+ elif getattr(obj, '__get__', None) is not None:
+ m = ContextAwareDescriptor(obj)
+ setattr(cls, name, m)
+ return cls
+
+def ContextAwareDescriptors():
+ frame = sys._getframe(1)
+ locals = frame.f_locals
+
+ # Try to make sure we were called from a class def
+ if (locals is frame.f_globals) or ('__module__' not in locals):
+ raise TypeError("ContextAwareDescriptors() can be used only from a"
+ " class definition.")
+ addClassAdvisor(_context_aware_advice, depth=2)
=== Zope3/src/zope/context/interfaces.py 1.15 => 1.16 ===
--- Zope3/src/zope/context/interfaces.py:1.15 Thu Jun 12 05:29:00 2003
+++ Zope3/src/zope/context/interfaces.py Sat Jun 14 08:32:38 2003
@@ -176,7 +176,6 @@
The first argument passed to methods will be context wrapped
if the method is called on a context-wrapped instance.
-
"""
def ContextProperty(fget, fset=None, fdel=None):
@@ -184,10 +183,16 @@
Create a property with functions to be passed context-wrapped instances
This function works like Python's property function, except
- that the access functions are passed context-warpped instances.
+ that the access functions are passed context-wrapped instances.
"""
def ContextSuper(cls, wrapped_instance):
"""Call an inherited method on a wrapped instances
"""
+ def ContextAwareDescriptors():
+ """Function used in a class suite to advise use of context descriptors
+
+ All descriptors defined in that class suite will be made
+ context-aware.
+ """