[Checkins] SVN: z3c.objpath/trunk/src/z3c/objpath/ Do not maintain 'root' in the public interface.

Martijn Faassen faassen at infrae.com
Thu Dec 13 22:54:50 EST 2007


Log message for revision 82285:
  Do not maintain 'root' in the public interface.
  

Changed:
  U   z3c.objpath/trunk/src/z3c/objpath/README.txt
  U   z3c.objpath/trunk/src/z3c/objpath/interfaces.py
  U   z3c.objpath/trunk/src/z3c/objpath/path.py

-=-
Modified: z3c.objpath/trunk/src/z3c/objpath/README.txt
===================================================================
--- z3c.objpath/trunk/src/z3c/objpath/README.txt	2007-12-14 03:37:49 UTC (rev 82284)
+++ z3c.objpath/trunk/src/z3c/objpath/README.txt	2007-12-14 03:54:49 UTC (rev 82285)
@@ -1,13 +1,6 @@
 ObjectPath
 ==========
 
-Let's get the ObjectPath object from somewhere. We could have it registered
-as a utility and look it up that way, but in this case we'll just import
-it and instantiate it::
-
-  >>> from z3c.objpath.path import ObjectPath
-  >>> objpath = ObjectPath()
-
 We'll have a simple item::
 
   >>> class Item(object):
@@ -43,46 +36,51 @@
   >>> f = e['f'] = Item()
   >>> g = b['g'] = Item()
 
+We will now exercise two functions, ``path`` and ``resolve``, which
+are inverses of each other::
+
+  >>> from z3c.objpath.path import path, resolve
+
 We can create a path to ``a`` from ``root``::
 
-  >>> objpath.path(root, a)
+  >>> path(root, a)
   '/root/data/a'
 
 We can also resolve it again::
 
-  >>> objpath.resolve(root, '/root/data/a')
+  >>> resolve(root, '/root/data/a')
   <Container a>
 
 We can also create a path to ``a`` from ``data``::
 
-  >>> objpath.path(data, a)
+  >>> path(data, a)
   '/data/a'
 
 And resolve it again::
 
-  >>> objpath.resolve(data, '/data/a')
+  >>> resolve(data, '/data/a')
   <Container a>
 
 We can make a deeper path::
 
-  >>> objpath.path(root, f)
+  >>> path(root, f)
   '/root/data/a/e/f'
 
 And resolve it::
 
-  >>> objpath.resolve(root, '/root/data/a/e/f')
+  >>> resolve(root, '/root/data/a/e/f')
   <Item f>
 
 We get an error if we cannot construct a path::
 
-  >>> objpath.path(e, a)
+  >>> path(e, a)
   Traceback (most recent call last):
    ...
   ValueError: Cannot create path for <Container a>
 
 We also get an error if we cannot resolve a path::
 
-  >>> objpath.resolve(root, '/root/data/a/f/e')
+  >>> resolve(root, '/root/data/a/f/e')
   Traceback (most recent call last):
    ...
   ValueError: Cannot resolve path /root/data/a/f/e

Modified: z3c.objpath/trunk/src/z3c/objpath/interfaces.py
===================================================================
--- z3c.objpath/trunk/src/z3c/objpath/interfaces.py	2007-12-14 03:37:49 UTC (rev 82284)
+++ z3c.objpath/trunk/src/z3c/objpath/interfaces.py	2007-12-14 03:54:49 UTC (rev 82285)
@@ -1,30 +1,27 @@
 from zope.interface import Interface
 
 class IObjectPath(Interface):
-    """Path representation to objects.
+    """Path representation for objects.
     """
+    def path(obj):
+        """Give the path representation of obj.
 
-    def path(root, obj):
-        """Give the path representation of obj relative to root.
-
-        root - should be the root that the object is contained in.
         obj - object in a hierarchy of IContainer objects.
 
-        The path is defined relatively to the root.
+        The path is defined by the application and may be relative
+        to the application root.
 
         Returns the path.
         
         If no path to the object can be made, raise a ValueError.
         """
 
-    def resolve(root, path):
-        """Given a path resolve it from root.
+    def resolve(path):
+        """Given a path resolve to an object.
 
-        root - should be the root that the object is contained in.
-        path - a path to an object relative to the root.
+        path - a path as created with path()
 
         Returns the object that the path referred to.
 
         If the path cannot be resolved to an object, raise a ValueError.
         """
-

Modified: z3c.objpath/trunk/src/z3c/objpath/path.py
===================================================================
--- z3c.objpath/trunk/src/z3c/objpath/path.py	2007-12-14 03:37:49 UTC (rev 82284)
+++ z3c.objpath/trunk/src/z3c/objpath/path.py	2007-12-14 03:54:49 UTC (rev 82285)
@@ -1,39 +1,31 @@
-from zope.interface import implements
-from z3c.objpath.interfaces import IObjectPath
+"""This module contains some functions that may be helpful in the
+implementation of IObjectPath interface.
+"""
 
-class ObjectPath(object):
-    """
-    Implementation of IObjectPath.
+def path(root, obj):
+    steps = []
+    orig_obj = obj
+    while obj is not None:
+        steps.append(obj.__name__)
+        if obj is root:
+            break
+        obj = obj.__parent__
+    else:
+        raise ValueError("Cannot create path for %s" % orig_obj)
+    steps.reverse()
+    return '/' + '/'.join(steps)
 
-    Applications can register this as a global (or local) utility. Libraries
-    can then make use of this by looking up the utility.
-    """
-    implements(IObjectPath)
+def resolve(root, path):
+    steps = path.split('/')
+    assert steps[0] == ''
+    obj = root
+    assert steps[1] == root.__name__
+    steps = steps[2:]
+    for step in steps:
+        try:
+            obj = obj[step]
+        except KeyError:
+            raise ValueError("Cannot resolve path %s" % path)
+    return obj
 
-    def path(self, root, obj):
-        steps = []
-        orig_obj = obj
-        while obj is not None:
-            steps.append(obj.__name__)
-            if obj is root:
-                break
-            obj = obj.__parent__
-        else:
-            raise ValueError("Cannot create path for %s" % orig_obj)
-        steps.reverse()
-        return '/' + '/'.join(steps)
-
-    def resolve(self, root, path):
-        steps = path.split('/')
-        assert steps[0] == ''
-        obj = root
-        assert steps[1] == root.__name__
-        steps = steps[2:]
-        for step in steps:
-            try:
-                obj = obj[step]
-            except KeyError:
-                raise ValueError("Cannot resolve path %s" % path)
-        return obj
-
         



More information about the Checkins mailing list