[Zope3-checkins] SVN: Zope3/trunk/src/zope/dottedname/ Added a
simple general facility for resolving dotted names.
Jim Fulton
jim at zope.com
Thu Sep 29 05:12:46 EDT 2005
Log message for revision 38682:
Added a simple general facility for resolving dotted names.
Changed:
A Zope3/trunk/src/zope/dottedname/
A Zope3/trunk/src/zope/dottedname/__init__.py
A Zope3/trunk/src/zope/dottedname/resolve.py
A Zope3/trunk/src/zope/dottedname/resolve.txt
A Zope3/trunk/src/zope/dottedname/tests.py
-=-
Added: Zope3/trunk/src/zope/dottedname/__init__.py
===================================================================
--- Zope3/trunk/src/zope/dottedname/__init__.py 2005-09-29 09:12:41 UTC (rev 38681)
+++ Zope3/trunk/src/zope/dottedname/__init__.py 2005-09-29 09:12:45 UTC (rev 38682)
@@ -0,0 +1 @@
+#
Property changes on: Zope3/trunk/src/zope/dottedname/__init__.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/dottedname/resolve.py
===================================================================
--- Zope3/trunk/src/zope/dottedname/resolve.py 2005-09-29 09:12:41 UTC (rev 38681)
+++ Zope3/trunk/src/zope/dottedname/resolve.py 2005-09-29 09:12:45 UTC (rev 38682)
@@ -0,0 +1,44 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (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.
+#
+##############################################################################
+"""Dotted name support
+
+$Id$
+"""
+
+def resolve(name, module=None):
+ name = name.split('.')
+ if not name[0]:
+ if module is None:
+ raise ValueError("relative name without base module")
+ module = module.split('.')
+ name.pop(0)
+ while not name[0]:
+ module.pop()
+ name.pop(0)
+ name = module + name
+
+ used = name.pop(0)
+ found = __import__(used)
+ for n in name:
+ used += '.' + n
+ try:
+ found = getattr(found, n)
+ except AttributeError:
+ __import__(used)
+ found = getattr(found, n)
+
+ return found
+
+
+
Property changes on: Zope3/trunk/src/zope/dottedname/resolve.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/dottedname/resolve.txt
===================================================================
--- Zope3/trunk/src/zope/dottedname/resolve.txt 2005-09-29 09:12:41 UTC (rev 38681)
+++ Zope3/trunk/src/zope/dottedname/resolve.txt 2005-09-29 09:12:45 UTC (rev 38682)
@@ -0,0 +1,32 @@
+Resolution of dotted names
+==========================
+
+The zope.dottedname.resolve module provides a function for resolving
+dotted names. Dotted names are resolved by importing modules and by
+getting attributes from imported modules. Names with leading dots are
+relative.
+
+To illustrate, we'll use the dotted name resolver to access objects in
+the os module:
+
+ >>> from zope.dottedname.resolve import resolve
+ >>> resolve('os.path.split').__name__
+ 'split'
+
+Here, we used an absolute name. We can also using a relative name:
+
+ >>> resolve('.split').__name__
+ Traceback (most recent call last):
+ ...
+ ValueError: relative name without base module
+
+But we need to provide the module the name is relative to:
+
+ >>> resolve('.split', 'os.path').__name__
+ 'split'
+
+ >>> resolve('..system', 'os.path').__name__
+ 'system'
+
+ >>> resolve('...datetime', 'os.path').__name__
+ 'datetime'
Property changes on: Zope3/trunk/src/zope/dottedname/resolve.txt
___________________________________________________________________
Name: svn:eol-style
+ native
Added: Zope3/trunk/src/zope/dottedname/tests.py
===================================================================
--- Zope3/trunk/src/zope/dottedname/tests.py 2005-09-29 09:12:41 UTC (rev 38681)
+++ Zope3/trunk/src/zope/dottedname/tests.py 2005-09-29 09:12:45 UTC (rev 38682)
@@ -0,0 +1,28 @@
+##############################################################################
+#
+# Copyright (c) 2004 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.
+#
+##############################################################################
+"""XXX short summary goes here.
+
+$Id$
+"""
+import unittest
+from zope.testing import doctest
+
+def test_suite():
+ return unittest.TestSuite((
+ doctest.DocFileSuite('resolve.txt'),
+ ))
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
+
Property changes on: Zope3/trunk/src/zope/dottedname/tests.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
More information about the Zope3-Checkins
mailing list