[Checkins] SVN: z3c.objpath/ Initial import.

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


Log message for revision 82284:
  Initial import.
  

Changed:
  A   z3c.objpath/
  A   z3c.objpath/trunk/
  A   z3c.objpath/trunk/buildout.cfg
  A   z3c.objpath/trunk/setup.py
  A   z3c.objpath/trunk/src/
  A   z3c.objpath/trunk/src/z3c/
  A   z3c.objpath/trunk/src/z3c/__init__.py
  A   z3c.objpath/trunk/src/z3c/objpath/
  A   z3c.objpath/trunk/src/z3c/objpath/README.txt
  A   z3c.objpath/trunk/src/z3c/objpath/__init__.py
  A   z3c.objpath/trunk/src/z3c/objpath/interfaces.py
  A   z3c.objpath/trunk/src/z3c/objpath/path.py
  A   z3c.objpath/trunk/src/z3c/objpath/tests.py

-=-
Added: z3c.objpath/trunk/buildout.cfg
===================================================================
--- z3c.objpath/trunk/buildout.cfg	                        (rev 0)
+++ z3c.objpath/trunk/buildout.cfg	2007-12-14 03:37:49 UTC (rev 82284)
@@ -0,0 +1,14 @@
+[buildout]
+develop = .
+parts = test devpython
+
+[test]
+recipe = zc.recipe.testrunner
+eggs = z3c.objpath
+
+# installs bin/devpython to do simple interpreter tests
+[devpython]
+recipe = zc.recipe.egg
+interpreter = devpython
+eggs = z3c.objpath
+

Added: z3c.objpath/trunk/setup.py
===================================================================
--- z3c.objpath/trunk/setup.py	                        (rev 0)
+++ z3c.objpath/trunk/setup.py	2007-12-14 03:37:49 UTC (rev 82284)
@@ -0,0 +1,24 @@
+from setuptools import setup, find_packages
+import sys, os
+
+setup(
+    name='z3c.objpath',
+    version='0.1dev',
+    description="Paths to to objects.",
+    long_description="""""",
+    classifiers=[],
+    keywords='',
+    author='Martijn Faassen',
+    author_email='faassen at startifact.com',
+    url='http://dev.inghist.nl/eggs/',
+    license='',
+    packages=find_packages('src'),
+    package_dir={'': 'src'},
+    include_package_data=True,
+    zip_safe=False,
+    install_requires=[
+        'setuptools',
+        'zope.interface',
+        ],
+    entry_points={},
+    )

Added: z3c.objpath/trunk/src/z3c/__init__.py
===================================================================
--- z3c.objpath/trunk/src/z3c/__init__.py	                        (rev 0)
+++ z3c.objpath/trunk/src/z3c/__init__.py	2007-12-14 03:37:49 UTC (rev 82284)
@@ -0,0 +1,7 @@
+# this is a namespace package
+try:
+    import pkg_resources
+    pkg_resources.declare_namespace(__name__)
+except ImportError:
+    import pkgutil
+    __path__ = pkgutil.extend_path(__path__, __name__)

Added: z3c.objpath/trunk/src/z3c/objpath/README.txt
===================================================================
--- z3c.objpath/trunk/src/z3c/objpath/README.txt	                        (rev 0)
+++ z3c.objpath/trunk/src/z3c/objpath/README.txt	2007-12-14 03:37:49 UTC (rev 82284)
@@ -0,0 +1,88 @@
+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):
+  ...   __name__ = None
+  ...   __parent__ = None
+  ...   def __repr__(self):
+  ...     return '<Item %s>' % self.__name__
+
+Let's create a simple container-like object::
+
+  >>> class Container(Item):
+  ...   def __init__(self):
+  ...     self._d = {}
+  ...   def __setitem__(self, name, obj):
+  ...     self._d[name] = obj
+  ...     obj.__name__ = name
+  ...     obj.__parent__ = self
+  ...   def __getitem__(self, name):
+  ...     return self._d[name]
+  ...   def __repr__(self):
+  ...     return '<Container %s>' % self.__name__
+
+Now let's create a structure::
+
+  >>> root = Container()
+  >>> root.__name__ = 'root'
+  >>> data = root['data'] = Container()
+  >>> a = data['a'] = Container()
+  >>> b = data['b'] = Container()
+  >>> c = data['c'] = Item()
+  >>> d = a['d'] = Item()
+  >>> e = a['e'] = Container()
+  >>> f = e['f'] = Item()
+  >>> g = b['g'] = Item()
+
+We can create a path to ``a`` from ``root``::
+
+  >>> objpath.path(root, a)
+  '/root/data/a'
+
+We can also resolve it again::
+
+  >>> objpath.resolve(root, '/root/data/a')
+  <Container a>
+
+We can also create a path to ``a`` from ``data``::
+
+  >>> objpath.path(data, a)
+  '/data/a'
+
+And resolve it again::
+
+  >>> objpath.resolve(data, '/data/a')
+  <Container a>
+
+We can make a deeper path::
+
+  >>> objpath.path(root, f)
+  '/root/data/a/e/f'
+
+And resolve it::
+
+  >>> objpath.resolve(root, '/root/data/a/e/f')
+  <Item f>
+
+We get an error if we cannot construct a path::
+
+  >>> objpath.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')
+  Traceback (most recent call last):
+   ...
+  ValueError: Cannot resolve path /root/data/a/f/e

Added: z3c.objpath/trunk/src/z3c/objpath/__init__.py
===================================================================
--- z3c.objpath/trunk/src/z3c/objpath/__init__.py	                        (rev 0)
+++ z3c.objpath/trunk/src/z3c/objpath/__init__.py	2007-12-14 03:37:49 UTC (rev 82284)
@@ -0,0 +1 @@
+#

Added: z3c.objpath/trunk/src/z3c/objpath/interfaces.py
===================================================================
--- z3c.objpath/trunk/src/z3c/objpath/interfaces.py	                        (rev 0)
+++ z3c.objpath/trunk/src/z3c/objpath/interfaces.py	2007-12-14 03:37:49 UTC (rev 82284)
@@ -0,0 +1,30 @@
+from zope.interface import Interface
+
+class IObjectPath(Interface):
+    """Path representation to objects.
+    """
+
+    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.
+
+        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.
+
+        root - should be the root that the object is contained in.
+        path - a path to an object relative to the root.
+
+        Returns the object that the path referred to.
+
+        If the path cannot be resolved to an object, raise a ValueError.
+        """
+

Added: z3c.objpath/trunk/src/z3c/objpath/path.py
===================================================================
--- z3c.objpath/trunk/src/z3c/objpath/path.py	                        (rev 0)
+++ z3c.objpath/trunk/src/z3c/objpath/path.py	2007-12-14 03:37:49 UTC (rev 82284)
@@ -0,0 +1,39 @@
+from zope.interface import implements
+from z3c.objpath.interfaces import IObjectPath
+
+class ObjectPath(object):
+    """
+    Implementation of IObjectPath.
+
+    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 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
+
+        

Added: z3c.objpath/trunk/src/z3c/objpath/tests.py
===================================================================
--- z3c.objpath/trunk/src/z3c/objpath/tests.py	                        (rev 0)
+++ z3c.objpath/trunk/src/z3c/objpath/tests.py	2007-12-14 03:37:49 UTC (rev 82284)
@@ -0,0 +1,15 @@
+import unittest
+
+from zope.testing import doctest
+
+def test_suite():
+    optionflags = (
+        doctest.ELLIPSIS
+        | doctest.REPORT_NDIFF
+        | doctest.NORMALIZE_WHITESPACE
+        )
+
+    return unittest.TestSuite([
+        doctest.DocFileSuite(
+            'README.txt', optionflags=optionflags)
+        ])



More information about the Checkins mailing list