[Zope3-checkins] CVS: Zope3/src/zope/app/container -
constraints.py:1.4
Kiran Jonnalagadda
jace at pobox.com
Wed Dec 10 08:04:54 EST 2003
Update of /cvs-repository/Zope3/src/zope/app/container
In directory cvs.zope.org:/tmp/cvs-serv29585/src/zope/app/container
Modified Files:
constraints.py
Log Message:
checkObject now verifies that the container is an IContainer.
=== Zope3/src/zope/app/container/constraints.py 1.3 => 1.4 ===
--- Zope3/src/zope/app/container/constraints.py:1.3 Thu Dec 4 08:48:01 2003
+++ Zope3/src/zope/app/container/constraints.py Wed Dec 10 08:04:53 2003
@@ -33,8 +33,9 @@
... "Add an item"
... __setitem__.precondition = preNoZ
+ >>> from zope.app.interfaces.container import IContainer
>>> class C1:
- ... zope.interface.implements(I1)
+ ... zope.interface.implements(I1, IContainer)
... def __repr__(self):
... return 'C1'
@@ -119,7 +120,7 @@
>>> del c1.x
>>> checkFactory(c1, "bob", factory)
False
-
+
Unlike checkObject, checkFactory:
- Returns a boolean value
@@ -128,7 +129,7 @@
The container constraint we defined for C1 isn't actually used to
check the factory:
-
+
>>> c1.x = 1
>>> checkFactory(c1, "Zbob", factory)
True
@@ -142,7 +143,7 @@
We can do this (silly thing) because preNoZ doesn't use the object
argument.
-
+
>>> checkFactory(c1, "Zbob", factory)
False
@@ -151,12 +152,14 @@
import zope.interface
from zope.app.interfaces.container import InvalidItemType, InvalidContainerType
+from zope.app.i18n import ZopeMessageIDFactory as _
+from zope.app.interfaces.container import IContainer
def checkObject(container, name, object):
"""Check containement constraints for an object and container
"""
-
+
# check __setitem__ precondition
for iface in zope.interface.providedBy(container):
__setitem__ = iface.get('__setitem__')
@@ -178,6 +181,12 @@
validate(container)
break
+ if not IContainer.isImplementedBy(container):
+ # 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__')
@@ -212,7 +221,7 @@
return True
-
+
class IItemTypePrecondition(zope.interface.Interface):
def __call__(container, name, object):
@@ -251,7 +260,7 @@
... return zope.interface.implementedBy(Ob)
>>> factory = Factory()
-
+
>>> try:
... precondition(None, 'foo', ob)
... except InvalidItemType, v:
@@ -259,7 +268,7 @@
... else:
... print 'Should have failed'
None True True
-
+
>>> try:
... precondition.factory(None, 'foo', factory)
... except InvalidItemType, v:
@@ -272,7 +281,7 @@
>>> precondition(None, 'foo', ob)
>>> precondition.factory(None, 'foo', factory)
- """
+ """
zope.interface.implements(IItemTypePrecondition)
@@ -287,12 +296,12 @@
def factory(self, container, name, factory):
implemented = factory.getInterfaces()
-
+
for iface in self.types:
if implemented.isOrExtends(iface):
return
raise InvalidItemType(container, factory, self.types)
-
+
class ContainerTypesConstraint:
"""Constrain a container to be one of a number of types
@@ -316,7 +325,7 @@
>>> zope.interface.classImplements(Ob, I2)
>>> constraint(Ob())
True
-
+
"""
def __init__(self, *types):
More information about the Zope3-Checkins
mailing list