[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/OFS/Services/ObjectHub - ObjectHub.py:1.6

Guido van Rossum guido@python.org
Fri, 6 Dec 2002 08:23:46 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Services/ObjectHub
In directory cvs.zope.org:/tmp/cvs-serv11056

Modified Files:
	ObjectHub.py 
Log Message:
Changed the name of the argument to register() to be obj_or_loc, like
it is in the interface.

Change the test for whether it is an object or a location to check for
a string or tuple rather than for something wrapped, since some
objects (e.g. the root) may not be wrapped.

Added some XXX comments for SteveA.

Whitespace normalization.


=== Zope3/lib/python/Zope/App/OFS/Services/ObjectHub/ObjectHub.py 1.5 => 1.6 ===
--- Zope3/lib/python/Zope/App/OFS/Services/ObjectHub/ObjectHub.py:1.5	Thu Dec  5 08:54:52 2002
+++ Zope3/lib/python/Zope/App/OFS/Services/ObjectHub/ObjectHub.py	Fri Dec  6 08:23:45 2002
@@ -2,14 +2,14 @@
 #
 # Copyright (c) 2001, 2002 Zope Corporation and Contributors.
 # All Rights Reserved.
-# 
+#
 # This software is subject to the provisions of the Zope Public License,
 # Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
-# 
+#
 ##############################################################################
 """
 
@@ -56,12 +56,12 @@
         return abs
 
 class ObjectHub(ProtoServiceEventChannel):
-    
+
     # this implementation makes the decision to not interact with any
-    # object hubs above it: it is a world unto itself, as far as it is 
+    # object hubs above it: it is a world unto itself, as far as it is
     # concerned, and if it doesn't know how to do something, it won't
     # ask anything else to try.  Everything else is YAGNI for now.
-    
+
     __implements__ = (
         IObjectHub,
         ProtoServiceEventChannel.__implements__)
@@ -72,7 +72,7 @@
         self.__hubid_to_location = IOBTree()
         # tuple of unicodes --> int
         self.__location_to_hubid = OIBTree()
-    
+
     # XXX this is copied because of some context method problems
     # with moving LocalEventChannel.notify to this _notify via a simple
     # assignment, i.e. _notify = LocalEventChannel.notify
@@ -113,7 +113,7 @@
                         canonical_new_location)
                     # send out IObjectMovedHubEvent to plugins
                     event = ObjectMovedHubEvent(
-                        wrapped_self, 
+                        wrapped_self,
                         hubid,
                         canonical_location,
                         canonical_new_location,
@@ -124,14 +124,14 @@
                 # container yet has no location. So, we're not interested in
                 # it.
                 pass
-            else: 
+            else:
                 canonical_location = locationAsTuple(event.location)
                 hubid = clean_self.__location_to_hubid.get(canonical_location)
                 if hubid is not None:
                     if IObjectModifiedEvent.isImplementedBy(event):
                         # send out IObjectModifiedHubEvent to plugins
                         event = ObjectModifiedHubEvent(
-                            wrapped_self, 
+                            wrapped_self,
                             hubid,
                             canonical_location,
                             event.object)
@@ -159,29 +159,34 @@
             raise NotFoundError(locationAsUnicode(location))
         else:
             return hubid
-    
+
     def getLocation(self, hubid):
         '''See interface IObjectHub'''
         try:
             return self.__hubid_to_location[hubid]
         except KeyError:
             raise NotFoundError(hubid)
-    
+
     def getObject(wrapped_self, hubid):
         '''See interface IObjectHub'''
         location = wrapped_self.getLocation(hubid)
         adapter = getAdapter(wrapped_self, ITraverser)
         return adapter.traverse(location)
     getObject = ContextMethod(getObject)
-    
-    def register(wrapped_self, location):
+
+    def register(wrapped_self, obj_or_loc):
         '''See interface ILocalObjectHub'''
         clean_self = removeAllProxies(wrapped_self)
-        if isWrapper(location):
-            obj = location
-            location = getPhysicalPath(location)
-        else:
+        # XXX Need a new unit test for this; previously we tested
+        #     whether it's wrapped, which is wrong because the root
+        #     isn't wrapped (and it certainly makes sense to want to
+        #     register the root).
+        if isinstance(obj_or_loc, (str, unicode, tuple)):
             obj = None
+            location = obj_or_loc
+        else:
+            obj = obj_or_loc
+            location = getPhysicalPath(obj_or_loc)
         canonical_location = locationAsTuple(location)
         if not canonical_location[0] == u'':
             raise ValueError("Location must be absolute")
@@ -195,22 +200,27 @@
 
         location_to_hubid = clean_self.__location_to_hubid
         if location_to_hubid.has_key(canonical_location):
+            # XXX It would be more convenient if register() returned
+            #     a bool indicating whether the object is already
+            #     registered, rather than raising an exception.
+            #     Then a more useful distinction between real errors
+            #     and this (common) condition could be made.
             raise ObjectHubError(
-                'location %s already in object hub' % 
+                'location %s already in object hub' %
                 locationAsUnicode(canonical_location))
         hubid = clean_self._generateHubId(canonical_location)
         location_to_hubid[canonical_location] = hubid
 
         # send out IObjectRegisteredHubEvent to plugins
         event = ObjectRegisteredHubEvent(
-            wrapped_self, 
+            wrapped_self,
             hubid,
             canonical_location,
             obj)
         clean_self._notify(wrapped_self, event)
         return hubid
     register = ContextMethod(register)
-    
+
     def unregister(wrapped_self, location):
         '''See interface ILocalObjectHub'''
         clean_self = removeAllProxies(wrapped_self)
@@ -225,20 +235,20 @@
         try:
             hubid = location_to_hubid[canonical_location]
         except KeyError:
-            raise NotFoundError('location %s is not in object hub' % 
+            raise NotFoundError('location %s is not in object hub' %
                 locationAsUnicode(canonical_location))
         else:
             del hubid_to_location[hubid]
             del location_to_hubid[canonical_location]
-            
+
             # send out IObjectUnregisteredHubEvent to plugins
             event = ObjectUnregisteredHubEvent(
-                wrapped_self, 
+                wrapped_self,
                 hubid,
                 canonical_location)
             clean_self._notify(wrapped_self, event)
     unregister = ContextMethod(unregister)
-   
+
     def numRegistrations(self):
         """See interface IObjectHub"""
         # The hubid<-->location mappings should be the same size.
@@ -256,7 +266,7 @@
             # Optimisation when we're asked for all the registered objects.
             # Returns an IOBTreeItems object.
             return self.__location_to_hubid.items()
-        
+
         # BTrees only support searches including the min and max.
         # So, I need to add to the end of the location a string that will
         # be larger than any other. I could also use a type that
@@ -281,4 +291,3 @@
             index = randid()
         self._v_nextid = index + 1
         return index
-