[Zope-Checkins] CVS: Zope3/lib/python/Zope/ObjectHub - IObjectHub.py:1.3 ObjectHub.py:1.4

Holger Krekel hpk@devel.trillke.net
Tue, 25 Jun 2002 06:45:46 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/ObjectHub
In directory cvs.zope.org:/tmp/cvs-serv23194

Modified Files:
	IObjectHub.py ObjectHub.py 
Log Message:
- added register/unregister methods to IObjectHub and its default
  implementation. 

- testObjectHub checks for correct Register/Unregister Events
  and errors

Godefroid and Holger



=== Zope3/lib/python/Zope/ObjectHub/IObjectHub.py 1.2 => 1.3 ===
         
         If there is no location, raise Zope.Exceptions.NotFoundError.
-        
         """
         
     def getObject(ruid):
@@ -132,3 +131,19 @@
         If there is no such object, passes through whatever error
         the traversal service raises.
         """
+
+    def register(location):
+        """Returns a new ruid for the given location if it is not 
+        already registered. 
+
+        It also emits a RuidObjectRegisteredEvent.  Raises an 
+        ObjectHubError if the location was previously registered. 
+        """
+
+    def unregister(ruid_or_location):
+        """Unregister an object identified either by location or by ruid.
+
+        It also emits a RuidObjectUnregisteredEvent. 
+        If the Ruid or location wasn't registered a 
+        Zope.Exceptions.NotFoundError is raised.
+        """ 


=== Zope3/lib/python/Zope/ObjectHub/ObjectHub.py 1.3 => 1.4 ===
         adapter = getAdapter(object, ITraverser)
         return adapter.traverse(location)
+
+    def register(self, location):
+        '''See interface IObjectHub'''
+        canonical_location=self._canonical(location)
+
+        ruid = self._registerObject(canonical_location)
+
+        # send out to plugins IRuidObjectRegisteredEvent
+        event = RuidObjectRegisteredEvent(
+            self, 
+            ruid,
+            canonical_location)
+        self.__eventchannel.notify(event)
+        return ruid
+
+    def unregister(self, ruid_or_location):
+        '''See interface IObjectHub'''
+        if type(ruid_or_location) is int:
+            canonical_location=self.lookupLocation(ruid_or_location)
+        else:
+            canonical_location=self._canonical(ruid_or_location)
+        ruid = self._unregisterObject(canonical_location)
+        if ruid is None:
+            raise NotFoundError, 'location %s is not in object hub' % \
+                canonical_location
+        event = RuidObjectUnregisteredEvent(
+            self, 
+            ruid,
+            canonical_location)
+        self.__eventchannel.notify(event)
         
     #
     ############################################################
@@ -140,17 +170,20 @@
         
     _canonical=staticmethod(_canonical)
 
-    def _objectAdded(self, location):
-        canonical_location = self._canonical(location)
-        
+    def _registerObject(self, canonical_location):
         location_to_ruid = self.__location_to_ruid
-                
         if location_to_ruid.has_key(canonical_location):
             raise ObjectHubError, 'location %s already in object hub' % \
                 canonical_location
         ruid = self._generateRuid(canonical_location)
         location_to_ruid[canonical_location] = ruid
-        
+        return ruid
+
+    def _objectAdded(self, location):
+        canonical_location = self._canonical(location)
+
+        ruid = self._registerObject(canonical_location)
+
         # send out to plugins IRuidObjectAddedEvent
         event = RuidObjectAddedEvent(
             self, 
@@ -199,20 +232,28 @@
             canonical_new_location)
         self.__eventchannel.notify(event)
 
-            
-    def _objectRemoved(self, location, obj):
+    def _unregisterObject(self, canonical_location):
         location_to_ruid = self.__location_to_ruid
         ruid_to_location = self.__ruid_to_location
-        canonical_location = self._canonical(location)
         try:
             ruid = location_to_ruid[canonical_location]
         except KeyError:
             # we don't know about this location, so we
-            # can ignore this event
-            return
+            # just return None 
+            return 
+        else:
+            del ruid_to_location[ruid]
+            del location_to_ruid[canonical_location]
+            return ruid
+        
             
-        del ruid_to_location[ruid]
-        del location_to_ruid[canonical_location]
+    def _objectRemoved(self, location, obj):
+        canonical_location = self._canonical(location)
+        ruid = self._unregisterObject(canonical_location)
+        if ruid is None:
+            # we don't know about this location, so we
+            # just ignore the Event
+            return
             
         # send out to plugins IRuidObjectRemovedEvent
         event = RuidObjectRemovedEvent(