[Zope3-checkins] SVN: Zope3/trunk/ zope.app.keyreference:
Dominik Huber
dominik.huber at projekt01.ch
Thu Jun 9 13:05:27 EDT 2005
Log message for revision 30711:
zope.app.keyreference:
Made different key reference implementations comparable.
Therefore the IKeyReference interface is
extended by an additional key_type_id attribute.
Key references should sort first on their
key type and second on any type-specific
information.
zope.app.event:
fix broken test
Changed:
U Zope3/trunk/doc/CHANGES.txt
U Zope3/trunk/src/zope/app/event/tests/test_objectevent.py
U Zope3/trunk/src/zope/app/keyreference/interfaces.py
U Zope3/trunk/src/zope/app/keyreference/persistent.py
U Zope3/trunk/src/zope/app/keyreference/persistent.txt
-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt 2005-06-09 15:05:17 UTC (rev 30710)
+++ Zope3/trunk/doc/CHANGES.txt 2005-06-09 17:05:27 UTC (rev 30711)
@@ -603,6 +603,9 @@
Bug Fixes
+ - Made different key reference implementations comparable:IKeyReference
+ interface is extended by an additional key_type_id attribute.
+
- Fixed issue #400: i18n image broken in trunk
- Fix #392 : adapter registry subscribers method can return None
Modified: Zope3/trunk/src/zope/app/event/tests/test_objectevent.py
===================================================================
--- Zope3/trunk/src/zope/app/event/tests/test_objectevent.py 2005-06-09 15:05:17 UTC (rev 30710)
+++ Zope3/trunk/src/zope/app/event/tests/test_objectevent.py 2005-06-09 17:05:27 UTC (rev 30711)
@@ -15,7 +15,8 @@
$Id$
"""
-import unittest, doctest
+import unittest
+from zope.testing import doctest
from zope.app.annotation.interfaces import IAnnotations, IAnnotatable
from zope.app.annotation.interfaces import IAttributeAnnotatable
@@ -112,9 +113,9 @@
def setUpObjectEventDocTest(test) :
setUp()
- ztapi.provideAdapter(IAttributeAnnotatable, \
+ ztapi.provideAdapter(IAttributeAnnotatable,
IAnnotations, AttributeAnnotations)
- ztapi.provideAdapter(IAnnotatable, \
+ ztapi.provideAdapter(IAnnotatable,
IZopeDublinCore, ZDCAnnotatableAdapter)
def tearDownObjectEventDocTest(test) :
@@ -126,9 +127,10 @@
unittest.makeSuite(TestObjectAnnotationsModifiedEvent),
unittest.makeSuite(TestObjectContentModifiedEvent),
unittest.makeSuite(TestObjectEventNotifications),
- doctest.DocTestSuite("zope.app.event.objectevent", \
- setUp=setUpObjectEventDocTest, \
- tearDown=tearDownObjectEventDocTest),
+ doctest.DocTestSuite("zope.app.event.objectevent",
+ setUp=setUpObjectEventDocTest,
+ tearDown=tearDownObjectEventDocTest,
+ optionflags=doctest.NORMALIZE_WHITESPACE),
))
if __name__=='__main__':
Modified: Zope3/trunk/src/zope/app/keyreference/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/keyreference/interfaces.py 2005-06-09 15:05:17 UTC (rev 30710)
+++ Zope3/trunk/src/zope/app/keyreference/interfaces.py 2005-06-09 17:05:27 UTC (rev 30711)
@@ -16,7 +16,11 @@
$Id$
"""
import zope.interface
+from zope.schema import DottedName
+from zope.app.i18n import ZopeMessageIDFactory as _
+
+
class NotYet(Exception):
"""Can't compute a key reference for an object
@@ -30,6 +34,12 @@
The references are compared by their hashes.
"""
+ key_type_id = DottedName(title=_('Key Type Id'),
+ description=_('Key references should sort first '
+ 'on their key type and second on any type-specific '
+ 'information.')
+ )
+
def __call__():
"""Get the object this reference is linking to.
"""
Modified: Zope3/trunk/src/zope/app/keyreference/persistent.py
===================================================================
--- Zope3/trunk/src/zope/app/keyreference/persistent.py 2005-06-09 15:05:17 UTC (rev 30710)
+++ Zope3/trunk/src/zope/app/keyreference/persistent.py 2005-06-09 17:05:27 UTC (rev 30711)
@@ -32,6 +32,8 @@
"""
zope.interface.implements(zope.app.keyreference.interfaces.IKeyReference)
+ key_type_id = 'zope.app.keyreference.persistent'
+
def __init__(self, object):
if not getattr(object, '_p_oid', None):
connection = IConnection(object, None)
@@ -49,11 +51,10 @@
return hash(self.object._p_oid)
def __cmp__(self, other):
- if not isinstance(other, KeyReferenceToPersistent):
- raise TypeError("Cannot compare ReferenceToPersistent with %r" %
- (other,))
- return cmp(self.object._p_oid, other.object._p_oid)
+ if self.key_type_id == other.key_type_id:
+ return cmp(self.object._p_oid, other.object._p_oid)
+ return cmp(self.key_type_id, other.key_type_id)
def connectionOfPersistent(ob):
@@ -70,4 +71,3 @@
if cur is None:
return None
return cur._p_jar
-
Modified: Zope3/trunk/src/zope/app/keyreference/persistent.txt
===================================================================
--- Zope3/trunk/src/zope/app/keyreference/persistent.txt 2005-06-09 15:05:17 UTC (rev 30710)
+++ Zope3/trunk/src/zope/app/keyreference/persistent.txt 2005-06-09 17:05:27 UTC (rev 30711)
@@ -43,6 +43,35 @@
>>> hash(KeyReferenceToPersistent(root['ob1'])) == hash(key1)
True
+Other key reference implementations are differed by their key type id.
+Key references should sort first on their key type and second on any
+type-specific information:
+
+ >>> from zope.interface import implements
+ >>> from zope.app.keyreference.interfaces import IKeyReference
+
+ >>> class DummyKeyReference(object):
+ ... implements(IKeyReference)
+ ... key_type_id = 'zope.app.keyreference.object'
+ ... def __init__(self, obj):
+ ... self.object = obj
+ ... def __cmp__(self, other):
+ ... if self.key_type_id == other.key_type_id:
+ ... return cmp(self.object, other.object)
+ ... return cmp(self.key_type_id, other.key_type_id)
+
+ >>> dummy_key1 = DummyKeyReference(object())
+ >>> dummy_key2 = DummyKeyReference(object())
+ >>> dummy_key3 = DummyKeyReference(object())
+
+ >>> keys = [key1, dummy_key1, dummy_key2, key2, dummy_key3]
+ >>> keys.sort()
+ >>> key_type_ids = [key.key_type_id for key in keys]
+ >>> key_type_ids[0:3].count('zope.app.keyreference.object')
+ 3
+ >>> key_type_ids[3:].count('zope.app.keyreference.persistent')
+ 2
+
We'll store the key references in the database:
>>> root['key1'] = key1
More information about the Zope3-Checkins
mailing list