Commit 3d95da90 authored by Stephan Richter's avatar Stephan Richter

Made ``Folder`` class inherit from ``BTreeContainer`` class, so that the

IContainer interface does not need to be re-implemented. Added a ``data``
attribute for BBB.
parent ea915ad3
bin
eggs
develop-eggs
parts
.installed.cfg
build
docs/_build
__pycache__
*.pyc
*.so
.tox
.coverage
nosetests.xml
coverage.xml
*.egg-info
*.egg
......@@ -5,6 +5,10 @@ CHANGES
4.0.0 (unreleased)
-------------------
- Made ``Folder`` class inherit from ``BTreeContainer`` class, so that the
IContainer interface does not need to be re-implemented. Added a ``data``
attribute for BBB.
- Replaced deprecated ``zope.component.adapts`` usage with equivalent
``zope.component.adapter`` decorator.
......
......@@ -14,89 +14,19 @@
"""The standard Zope Folder.
"""
__docformat__ = 'restructuredtext'
from BTrees.OOBTree import OOBTree
from persistent import Persistent
from zope.container.interfaces import IContainer, IContentContainer
from zope.container.contained import Contained, setitem, uncontained
from zope.interface import implementer
from zope.container import btree, interfaces
# XXX This container implementation is really only used by
# zope.site.folder.Folder. Please do not use it.
# XXX Check whether this IContainer implementation cannot really
# be replaced by the BTreeContainer.
@implementer(IContentContainer)
class Folder(Persistent, Contained):
@implementer(interfaces.IContentContainer)
class Folder(btree.BTreeContainer):
"""The standard Zope Folder implementation."""
def __init__(self):
self.data = OOBTree()
def keys(self):
"""Return a sequence-like object containing the names
associated with the objects that appear in the folder
"""
return self.data.keys()
def __iter__(self):
return iter(self.data.keys())
def values(self):
"""Return a sequence-like object containing the objects that
appear in the folder.
"""
return self.data.values()
def items(self):
"""Return a sequence-like object containing tuples of the form
(name, object) for the objects that appear in the folder.
"""
return self.data.items()
def __getitem__(self, name):
"""Return the named object, or raise ``KeyError`` if the object
is not found.
"""
return self.data[name]
def get(self, name, default=None):
"""Return the named object, or the value of the `default`
argument if the object is not found.
"""
return self.data.get(name, default)
def __contains__(self, name):
"""Return true if the named object appears in the folder."""
return self.data.has_key(name)
def __len__(self):
"""Return the number of objects in the folder."""
return len(self.data)
def __setitem__(self, name, object):
"""Add the given object to the folder under the given name."""
if not (isinstance(name, str) or isinstance(name, unicode)):
raise TypeError("Name must be a string rather than a %s" %
name.__class__.__name__)
try:
unicode(name)
except UnicodeError:
raise TypeError("Non-unicode names must be 7-bit-ascii only")
if not name:
raise TypeError("Name must not be empty")
if name in self.data:
raise KeyError("name, %s, is already in use" % name)
setitem(self, self.data.__setitem__, name, object)
def __delitem__(self, name):
"""Delete the named object from the folder. Raises a KeyError
if the object is not found."""
uncontained(self.data[name], self, name)
del self.data[name]
# BBB: The data attribute used to be exposed. This should make it also
# compatible with old pickles.
@apply
def data():
def getter(self):
return self._SampleContainer__data
def setter(self, value):
self._SampleContainer__data = value
return property(getter, setter)
from unittest import TestCase, makeSuite
from zope.container.folder import Folder
from zope.container.tests.test_icontainer import TestSampleContainer
from zope.container.tests.test_icontainer import BaseTestIContainer
from zope.container.tests.test_icontainer import DefaultTestData
class Test(BaseTestIContainer, TestCase):
class Test(TestSampleContainer, TestCase):
def makeTestObject(self):
return Folder()
def makeTestData(self):
return DefaultTestData()
def getUnknownKey(self):
return '10'
def getBadKeyTypes(self):
return [None, ['foo'], 1, '\xf3abc']
def testDataAccess(self):
folder = self.makeTestObject()
self.assertNotEqual(folder.data, None)
folder.data = 'foo'
self.assertEqual(folder.data, 'foo')
def test_suite():
return makeSuite(Test)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment