[Zope3-checkins] CVS: Zope3/src/zope/app/services - configuration.py:1.36

Guido van Rossum guido@python.org
Thu, 12 Jun 2003 15:28:39 -0400


Update of /cvs-repository/Zope3/src/zope/app/services
In directory cvs.zope.org:/tmp/cvs-serv16761/services

Modified Files:
	configuration.py 
Log Message:
Add relative/absolute path equivalence to the Dependable class, a la
UseConfiguration, and add unit tests for this.

Refactoring: share more code between Dependable and UseConfiguration.
The common base is in zope/app/dependable.py, for want of a better place.



=== Zope3/src/zope/app/services/configuration.py 1.35 => 1.36 ===
--- Zope3/src/zope/app/services/configuration.py:1.35	Thu Jun 12 14:49:32 2003
+++ Zope3/src/zope/app/services/configuration.py	Thu Jun 12 15:28:08 2003
@@ -50,7 +50,6 @@
 from zope.app.interfaces.services.configuration import Unregistered
 from zope.app.interfaces.services.configuration import Registered, Active
 from zope.app.traversing import getRoot, getPath, traverse
-from zope.app.traversing import getParent, canonicalPath
 from zope.component import getAdapter, queryAdapter
 from zope.component import getServiceManager
 from zope.app.context import ContextWrapper
@@ -500,9 +499,9 @@
     queryActiveComponent = ContextMethod(queryActiveComponent)
 
 
-USE_CONFIG_KEY = 'zope.app.services.configuration.UseConfiguration'
+from zope.app.dependable import PathSetAnnotation
 
-class UseConfiguration:
+class UseConfiguration(PathSetAnnotation):
     """An adapter from IUseConfigurable to IUseConfiguration.
 
     This class is the only place that knows how 'UseConfiguration'
@@ -511,60 +510,12 @@
 
     implements(IUseConfiguration)
 
-    def __init__(self, context):
-        self.context = context
-        try:
-            pp = getPath(getParent(self.context))
-            if not pp.endswith("/"):
-                pp += "/"
-            self.pp = pp # parentpath
-        except TypeError:
-            self.pp = ""
-        self.pplen = len(self.pp)
-
-    def addUsage(self, path):
-        path = self._make_relative(path)
-        annotations = getAdapter(self.context, IAnnotations)
-        old = annotations.get(USE_CONFIG_KEY, ())
-        fixed = map(self._make_relative, old)
-        if path not in fixed:
-            fixed.append(path)
-        new = tuple(fixed)
-        if new != old:
-            annotations[USE_CONFIG_KEY] = new
-
-    def removeUsage(self, path):
-        path = self._make_relative(path)
-        annotations = getAdapter(self.context, IAnnotations)
-        old = annotations.get(USE_CONFIG_KEY, ())
-        if old:
-            fixed = map(self._make_relative, old)
-            fixed = [loc for loc in fixed if loc != path]
-            new = tuple(fixed)
-            if new != old:
-                if new:
-                    annotations[USE_CONFIG_KEY] = new
-                else:
-                    del annotations[USE_CONFIG_KEY]
+    key = "zope.app.services.configuration.UseConfiguration"
+
+    addUsage = PathSetAnnotation.addPath
+    removeUsage = PathSetAnnotation.removePath
+    usages = PathSetAnnotation.getPaths
 
-    def usages(self):
-        annotations = getAdapter(self.context, IAnnotations)
-        locs = annotations.get(USE_CONFIG_KEY, ())
-        return tuple(map(self._make_absolute, locs))
-
-    def _make_relative(self, path):
-        if path.startswith("/") and self.pp:
-            path = canonicalPath(path)
-            if path.startswith(self.pp):
-                path = path[self.pplen:]
-                while path.startswith("/"):
-                    path = path[1:]
-        return path
-
-    def _make_absolute(self, path):
-        if not path.startswith("/") and self.pp:
-            path = self.pp + path
-        return path
 
 class ConfigurationManager(Persistent):
     """Configuration manager