[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/ add marker for
transient locations
Dominik Huber
dominik.huber at projekt01.ch
Thu Feb 24 06:07:54 EST 2005
Log message for revision 29280:
add marker for transient locations
exclude transient locations from being registered resp. unregistred within the intid utility.
Changed:
U Zope3/trunk/src/zope/app/intid/__init__.py
U Zope3/trunk/src/zope/app/intid/tests.py
U Zope3/trunk/src/zope/app/location/interfaces.py
-=-
Modified: Zope3/trunk/src/zope/app/intid/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/intid/__init__.py 2005-02-24 11:07:02 UTC (rev 29279)
+++ Zope3/trunk/src/zope/app/intid/__init__.py 2005-02-24 11:07:54 UTC (rev 29280)
@@ -30,15 +30,16 @@
from zope.security.proxy import removeSecurityProxy
from zope.app import zapi
-
from zope.app.container.contained import Contained
from zope.app.keyreference.interfaces import IKeyReference
from zope.app.location.interfaces import ILocation
+from zope.app.location.interfaces import ITransientLocation
from zope.app.intid.interfaces import IIntIds
from zope.app.intid.interfaces import IntIdRemovedEvent
from zope.app.intid.interfaces import IntIdAddedEvent
+
class IntIds(Persistent, Contained):
"""This utility provides a two way mapping between objects and
integer ids.
@@ -72,12 +73,17 @@
return default
def getId(self, ob):
- ref = IKeyReference(ob)
- return self.ids[ref]
+ if not ITransientLocation.providedBy(ob):
+ ref = IKeyReference(ob)
+ return self.ids[ref]
+ else:
+ raise KeyError(ob)
def queryId(self, ob, default=None):
- ref = IKeyReference(ob)
- return self.ids.get(ref, default)
+ try:
+ return self.getId(ob)
+ except KeyError:
+ return default
def _generateId(self):
"""Generate an id which is not yet taken.
@@ -112,6 +118,7 @@
del self.refs[uid]
del self.ids[ref]
+
def removeIntIdSubscriber(ob, event):
"""A subscriber to ObjectRemovedEvent
@@ -119,14 +126,16 @@
id utilities.
"""
- # Notify the catalogs that this object is about to be removed.
- notify(IntIdRemovedEvent(ob, event))
+ if not ITransientLocation.providedBy(ob): # do not unregister transient locations
+ # Notify the catalogs that this object is about to be removed.
+ notify(IntIdRemovedEvent(ob, event))
+
+ for utility in zapi.getAllUtilitiesRegisteredFor(IIntIds, ob):
+ try:
+ utility.unregister(ob)
+ except KeyError:
+ pass
- for utility in zapi.getAllUtilitiesRegisteredFor(IIntIds, ob):
- try:
- utility.unregister(ob)
- except KeyError:
- pass
def addIntIdSubscriber(ob, event):
"""A subscriber to ObjectAddedEvent
@@ -134,10 +143,13 @@
Registers the object added in all unique id utilities and fires
an event for the catalogs.
"""
- for utility in zapi.getAllUtilitiesRegisteredFor(IIntIds, ob):
- utility.register(ob)
+ if not ITransientLocation.providedBy(ob): # do not register transient locations
+ for utility in zapi.getAllUtilitiesRegisteredFor(IIntIds, ob):
+ utility.register(ob)
+
+ # Notify the catalogs that this object was added.
+ notify(IntIdAddedEvent(ob, event))
- notify(IntIdAddedEvent(ob, event))
# BBB
UniqueIdUtility = IntIds
Modified: Zope3/trunk/src/zope/app/intid/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/intid/tests.py 2005-02-24 11:07:02 UTC (rev 29279)
+++ Zope3/trunk/src/zope/app/intid/tests.py 2005-02-24 11:07:54 UTC (rev 29280)
@@ -16,20 +16,28 @@
$Id$
"""
import unittest
-from zope.interface.verify import verifyObject
+
from persistent import Persistent
from persistent.interfaces import IPersistent
+from ZODB.interfaces import IConnection
+
+from zope.interface import implements
+from zope.interface.verify import verifyObject
+
from zope.app.testing import setup, ztapi
from zope.app import zapi
-from zope.interface import implements
-from ZODB.interfaces import IConnection
from zope.app.location.interfaces import ILocation
+from zope.app.location.interfaces import ITransientLocation
from zope.app.component.hooks import setSite
class P(Persistent):
implements(ILocation)
+class T(object):
+ implements(ITransientLocation)
+
+
class ConnectionStub(object):
next = 1
def add(self, ob):
@@ -66,10 +74,18 @@
u = IntIds()
obj = P()
+ t_obj = T()
+
obj._p_jar = ConnectionStub()
+ self.assertRaises(KeyError, u.getId, obj)
+ self.assertRaises(KeyError, u.getId, t_obj)
+ self.assertRaises(TypeError, u.getId, object())
+
self.assert_(u.queryId(obj) is None)
self.assert_(u.queryId(obj, 42) is 42)
+ self.assert_(u.queryId(t_obj) is None)
+ self.assertRaises(TypeError, u.queryId, object())
self.assert_(u.queryObject(42) is None)
self.assert_(u.queryObject(42, obj) is obj)
@@ -176,6 +192,11 @@
events = []
ztapi.handle([IIntIdRemovedEvent], events.append)
+ # Transient locations should not be unregistered.
+ t = T()
+ removeIntIdSubscriber(t, ObjectRemovedEvent(parent_folder))
+ self.assertEquals(len(events), 0)
+
# This should unregister the object in all utilities, not just the
# nearest one.
removeIntIdSubscriber(folder, ObjectRemovedEvent(parent_folder))
@@ -198,7 +219,12 @@
events = []
ztapi.handle([IIntIdAddedEvent], events.append)
- # This should unregister the object in all utilities, not just the
+ # Transient locations should not be registered at all.
+ t = T()
+ addIntIdSubscriber(t, ObjectAddedEvent(parent_folder))
+ self.assertEquals(len(events), 0)
+
+ # This should register the object in all utilities, not just the
# nearest one.
addIntIdSubscriber(folder, ObjectAddedEvent(parent_folder))
Modified: Zope3/trunk/src/zope/app/location/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/location/interfaces.py 2005-02-24 11:07:02 UTC (rev 29279)
+++ Zope3/trunk/src/zope/app/location/interfaces.py 2005-02-24 11:07:54 UTC (rev 29280)
@@ -42,3 +42,10 @@
An iterable of objects whose __parent__ is the object
providing the interface is returned.
"""
+
+
+class ITransientLocation(ILocation):
+ """Mark transient resp. none-persistent location explicitly.
+
+ Do not reference marked objects directly.
+ """
More information about the Zope3-Checkins
mailing list