[Zope3-checkins] CVS: Zope3/src/zope/app/utilities - schema.py:1.8

Sidnei da Silva sidnei at x3ng.com.br
Wed Oct 8 09:10:42 EDT 2003


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

Modified Files:
	schema.py 
Log Message:
Merging schema-utility-persistence and schema-utility-struct-failure branches. Still get a failing test, which I commented out for now.

=== Zope3/src/zope/app/utilities/schema.py 1.7 => 1.8 ===
--- Zope3/src/zope/app/utilities/schema.py:1.7	Wed Sep 24 17:18:35 2003
+++ Zope3/src/zope/app/utilities/schema.py	Wed Oct  8 09:10:11 2003
@@ -15,8 +15,15 @@
 
 $Id$
 """
+from types import FunctionType
+
 from persistence.dict import PersistentDict
+from persistence.wrapper import Struct
+
 from zope.security.proxy import trustedRemoveSecurityProxy
+from zope.interface import Interface
+from zope.interface import implements
+from zope.interface import directlyProvides, directlyProvidedBy
 from zope.app import zapi
 from zope.app.introspector import nameToInterface, interfaceToName
 from zope.app.browser.container.adding import Adding
@@ -24,21 +31,25 @@
 from zope.app.interfaces.utilities.schema import \
      ISchemaAdding, IMutableSchema, ISchemaUtility
 from zope.app.services.interface import PersistentInterfaceClass
-from zope.app.services.interface import PersistentInterface
 from zope.app.services.utility import UtilityRegistration
-from zope.interface import implements
-from zope.interface import directlyProvides, directlyProvidedBy
 from zope.schema import getFieldsInOrder, getFieldNamesInOrder
 from zope.app.container.contained import Contained, setitem, uncontained
+from zope.interface.interface import Attribute, Method, fromFunction
+from zope.interface.interface import InterfaceClass
+from zope.interface.exceptions import InvalidInterface
 
 class SchemaUtility(PersistentInterfaceClass, Contained):
 
     implements(IMutableSchema, ISchemaUtility)
 
-    def __init__(self):
-        super(SchemaUtility, self).__init__('', (PersistentInterface,))
+    def __init__(self, name='', bases=(), attrs=None,
+                 __doc__=None, __module__=None):
+        if not bases:
+            bases = (Interface,)
+        super(SchemaUtility, self).__init__(name, bases,
+                                            attrs, __doc__, __module__)
         self.schemaPermissions = PersistentDict()
-        self._InterfaceClass__attrs = PersistentDict()
+        self._attrs = PersistentDict()
 
     def setName(self, name):
         """See zope.app.interfaces.utilities.IMutableSchema"""
@@ -53,14 +64,14 @@
             raise KeyError, "Field %s already exists." % name
         if fields:
             field.order = fields[-1].order + 1
-        self._setField(name, field)
+        self[name] = field
 
     def removeField(self, name):
         """See zope.app.interfaces.utilities.IMutableSchema"""
         fields = getFieldNamesInOrder(self)
         if name not in fields:
             raise KeyError, "Field %s does not exists." % name
-        self._delField(name)
+        del self[name]
 
     def renameField(self, orig_name, target_name):
         """See zope.app.interfaces.utilities.IMutableSchema"""
@@ -69,9 +80,9 @@
             raise KeyError, "Field %s does not exists." % orig_name
         if target_name in fields:
             raise KeyError, "Field %s already exists." % target_name
-        field = self._getField(orig_name)
-        self._delField(orig_name)
-        self._setField(target_name, field)
+        field = self[orig_name]
+        del self[orig_name]
+        self[target_name] = field
 
     def insertField(self, name, field, position):
         """See zope.app.interfaces.utilities.IMutableSchema"""
@@ -86,7 +97,7 @@
             field.order = fields[position-1].order + 1
         else:
             field.order = 1
-        self._setField(name, field)
+        self[name] = field
         for field in fields[position:]:
             field.order += 1
 
@@ -105,20 +116,118 @@
         for field in fields[position:]:
             field.order += 1
 
-    def _getField(self, name):
-        return self._InterfaceClass__attrs[name]
-
-    def _setField(self, name, field):
-        field = trustedRemoveSecurityProxy(field)
-        if not field.__name__:
-            field.__name__ = name
-        setitem(self, self._InterfaceClass__attrs.__setitem__, name, field)
+    def __delitem__(self, name):
+        uncontained(self._attrs[name], self, name)
+        del self._attrs[name]
         self._p_changed = 1
 
-    def _delField(self, name):
-        uncontained(self._InterfaceClass__attrs[name], self, name)
-        del self._InterfaceClass__attrs[name]
+    def __setitem__(self, name, value):
+        value = trustedRemoveSecurityProxy(value)
+        if isinstance(value, Attribute):
+            value.interface = name
+            if not value.__name__:
+                value.__name__ = name
+            elif isinstance(value, FunctionType):
+                attrs[name] = fromFunction(value, name, name=name)
+            else:
+                raise InvalidInterface("Concrete attribute, %s" % name)
+        value = Struct(value)
+        setitem(self, self._attrs.__setitem__, name, value)
         self._p_changed = 1
+
+    # Methods copied from zope.interface.interface.InterfaceClass,
+    # to avoid having to work around name mangling, which happens to be
+    # ugly and undesirable.
+    # Copied some methods, but not all. Only the ones that used __attrs
+    # and __bases__. Changed __attrs to _attrs, which is a PersistentDict,
+    # and __bases__ to getBases(), whic filters instances of InterfaceClass
+    def getBases(self):
+        return [b for b in self.__bases__ if isinstance(b, self.__class__)]
+
+    def extends(self, other, strict=True):
+        """Does an interface extend another?"""
+        if not strict and self == other:
+            return True
+
+        for b in self.getBases():
+            if b == other: return True
+            if b.extends(other): return True
+        return False
+
+    def names(self, all=False):
+        """Return the attribute names defined by the interface."""
+        if not all:
+            return self._attrs.keys()
+
+        r = {}
+        for name in self._attrs.keys():
+            r[name] = 1
+        for base in self.getBases():
+            for name in base.names(all):
+                r[name] = 1
+        return r.keys()
+
+    def namesAndDescriptions(self, all=False):
+        """Return attribute names and descriptions defined by interface."""
+        if not all:
+            return self._attrs.items()
+
+        r = {}
+        for name, d in self._attrs.items():
+            r[name] = d
+
+        for base in self.getBases():
+            for name, d in base.namesAndDescriptions(all):
+                if name not in r:
+                    r[name] = d
+
+        return r.items()
+
+    def getDescriptionFor(self, name):
+        """Return the attribute description for the given name."""
+        r = self.queryDescriptionFor(name)
+        if r is not None:
+            return r
+
+        raise KeyError, name
+
+    __getitem__ = getDescriptionFor
+
+    def queryDescriptionFor(self, name, default=None):
+        """Return the attribute description for the given name."""
+        r = self._attrs.get(name, self)
+        if r is not self:
+            return r
+        for base in self.getBases():
+            r = base.queryDescriptionFor(name, self)
+            if r is not self:
+                return r
+
+        return default
+
+    get = queryDescriptionFor
+
+    def deferred(self):
+        """Return a defered class corresponding to the interface."""
+        if hasattr(self, "_deferred"): return self._deferred
+
+        klass={}
+        exec "class %s: pass" % self.__name__ in klass
+        klass=klass[self.__name__]
+
+        self.__d(klass.__dict__)
+
+        self._deferred=klass
+
+        return klass
+
+    def __d(self, dict):
+
+        for k, v in self._attrs.items():
+            if isinstance(v, Method) and not (k in dict):
+                dict[k]=v
+
+        for b in self.getBases(): b.__d(dict)
 
 class SchemaAdding(Adding):
 




More information about the Zope3-Checkins mailing list