[Zope3-checkins] SVN: Zope3/trunk/src/zope/ Added a get method to
security declarations for looking up attribute
Jim Fulton
jim at zope.com
Tue Jun 15 05:43:28 EDT 2004
Log message for revision 25413:
Added a get method to security declarations for looking up attribute
specifications using a cache. Modified container constraint machinery
to use this. This sped up rendering of a sample content page about 15%.
-=-
Modified: Zope3/trunk/src/zope/app/container/constraints.py
===================================================================
--- Zope3/trunk/src/zope/app/container/constraints.py 2004-06-14 16:57:09 UTC (rev 25412)
+++ Zope3/trunk/src/zope/app/container/constraints.py 2004-06-15 09:43:27 UTC (rev 25413)
@@ -151,6 +151,7 @@
"""
import zope.interface
+from zope.interface import providedBy
from zope.app.container.interfaces import InvalidItemType, InvalidContainerType
from zope.app.i18n import ZopeMessageIDFactory as _
from zope.app.container.interfaces import IContainer
@@ -159,66 +160,59 @@
"""Check containement constraints for an object and container
"""
-
# check __setitem__ precondition
- for iface in zope.interface.providedBy(container):
- __setitem__ = iface.get('__setitem__')
- if __setitem__ is not None:
- precondition = __setitem__.queryTaggedValue('precondition')
- if precondition is not None:
- precondition(container, name, object)
- break
+ containerProvided = providedBy(container)
+ __setitem__ = containerProvided.get('__setitem__')
+ if __setitem__ is not None:
+ precondition = __setitem__.queryTaggedValue('precondition')
+ if precondition is not None:
+ precondition(container, name, object)
# check the constraint on __parent__
- for iface in zope.interface.providedBy(object):
- __parent__ = iface.get('__parent__')
- if __parent__ is not None:
- try:
- validate = __parent__.validate
- except AttributeError:
- pass
- else:
- validate(container)
- break
+ __parent__ = providedBy(object).get('__parent__')
+ if __parent__ is not None:
+ try:
+ validate = __parent__.validate
+ except AttributeError:
+ pass
+ else:
+ validate(container)
- if not IContainer.providedBy(container):
+
+ if not containerProvided.extends(IContainer):
# If it doesn't implement IContainer, it can't contain stuff.
raise TypeError(
_('Container is not a valid Zope container.')
)
def checkFactory(container, name, factory):
- for iface in zope.interface.providedBy(container):
- __setitem__ = iface.get('__setitem__')
- if __setitem__ is not None:
- precondition = __setitem__.queryTaggedValue('precondition')
- if precondition is not None:
- try:
- precondition = precondition.factory
- except AttributeError:
- pass
- else:
- try:
- precondition(container, name, factory)
- except zope.interface.Invalid:
- return False
- break
-
- # check the constraint on __parent__
- for iface in factory.getInterfaces():
- __parent__ = iface.get('__parent__')
- if __parent__ is not None:
+ __setitem__ = providedBy(container).get('__setitem__')
+ if __setitem__ is not None:
+ precondition = __setitem__.queryTaggedValue('precondition')
+ if precondition is not None:
try:
- validate = __parent__.validate
+ precondition = precondition.factory
except AttributeError:
pass
else:
try:
- validate(container)
+ precondition(container, name, factory)
except zope.interface.Invalid:
return False
- break
+ # check the constraint on __parent__
+ __parent__ = factory.getInterfaces().get('__parent__')
+ if __parent__ is not None:
+ try:
+ validate = __parent__.validate
+ except AttributeError:
+ pass
+ else:
+ try:
+ validate(container)
+ except zope.interface.Invalid:
+ return False
+
return True
Modified: Zope3/trunk/src/zope/interface/declarations.py
===================================================================
--- Zope3/trunk/src/zope/interface/declarations.py 2004-06-14 16:57:09 UTC (rev 25412)
+++ Zope3/trunk/src/zope/interface/declarations.py 2004-06-15 09:43:27 UTC (rev 25413)
@@ -49,6 +49,79 @@
def __init__(self, *interfaces):
Specification.__init__(self, _normalizeargs(interfaces))
+ def changed(self):
+ Specification.changed(self)
+ try:
+ del self._v_attrs
+ except AttributeError:
+ pass
+
+ def get():
+ marker1 = object()
+ marker2 = object()
+ def get(self, name, default=None):
+ """Query for an attribute description
+
+ >>> import zope.interface
+ >>> class I1(zope.interface.Interface):
+ ... a11 = zope.interface.Attribute('a11')
+ ... a12 = zope.interface.Attribute('a12')
+ >>> class I2(zope.interface.Interface):
+ ... a21 = zope.interface.Attribute('a21')
+ ... a22 = zope.interface.Attribute('a22')
+ ... a12 = zope.interface.Attribute('a212')
+ >>> class I11(I1):
+ ... a11 = zope.interface.Attribute('a111')
+
+ >>> decl = Declaration(I11, I2)
+ >>> decl.get('a11') is I11.get('a11')
+ True
+ >>> decl.get('a12') is I1.get('a12')
+ True
+ >>> decl.get('a21') is I2.get('a21')
+ True
+ >>> decl.get('a22') is I2.get('a22')
+ True
+ >>> decl.get('a')
+ >>> decl.get('a', 42)
+ 42
+
+ We get None even with no interfaces:
+
+ >>> decl = Declaration()
+ >>> decl.get('a11')
+ >>> decl.get('a11', 42)
+ 42
+
+ We get new data if e change interface bases:
+
+ >>> decl.__bases__ = I11, I2
+ >>> decl.get('a11') is I11.get('a11')
+ True
+ """
+ try:
+ attrs = self._v_attrs
+ except AttributeError:
+ attrs = self._v_attrs = {}
+ attr = attrs.get(name, marker1)
+ if attr is marker1:
+ for iface in self:
+ attr = iface.get(name, marker2)
+ if attr is not marker2:
+ break
+ else:
+ attr = marker2
+
+ attrs[name] = attr
+
+ if attr is marker2:
+ return default
+ else:
+ return attr
+
+ return get
+ get = get()
+
def __contains__(self, interface):
"""Test whether an interface is in the specification
Modified: Zope3/trunk/src/zope/interface/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/interface/interfaces.py 2004-06-14 16:57:09 UTC (rev 25412)
+++ Zope3/trunk/src/zope/interface/interfaces.py 2004-06-15 09:43:27 UTC (rev 25413)
@@ -94,6 +94,13 @@
""")
+ def get(name, default=None):
+ """Look up the description for a name
+
+ If the named attribute is not defined, the default is
+ returned.
+ """
+
class IInterface(ISpecification, IElement):
"""Interface objects
@@ -240,13 +247,6 @@
to list, then raises Invalid with the errors as the first element
of the "args" tuple."""
- def get(name, default=None):
- """Look up the description for a name
-
- If the named attribute is not defined, the default is
- returned.
- """
-
def __contains__(name):
"""Test whether the name is defined by the interface"""
More information about the Zope3-Checkins
mailing list