Added "resolve" function (copied from 'fanstatic') ZTK-1.1
authorThierry Florac
Sun, 20 Nov 2011 12:47:42 +0100
branchZTK-1.1
changeset 88 37c6b51cef8b
parent 87 689eef034b00
child 89 165f37f89e5a
Added "resolve" function (copied from 'fanstatic')
ztfy/utils/traversing.py
--- a/ztfy/utils/traversing.py	Sun Nov 20 12:47:11 2011 +0100
+++ b/ztfy/utils/traversing.py	Sun Nov 20 12:47:42 2011 +0100
@@ -37,3 +37,29 @@
             return interface(parent)
         parent = getattr(parent, '__parent__', None)
     return None
+
+
+# copied from fanstatic (which copied it from zope.dottedname !)
+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