# HG changeset patch -- Bitbucket.org
# Project apipkg
# URL http://bitbucket.org/hpk42/apipkg/overview
# User Ralf Schmitt <r...@systemexit.de>
# Date 1287009631 -7200
# Node ID 144664a4ea184197e981873b961130751cc1e3b5
# Parent  56f2070fcd8e0192bad2eb35c66e6cd0ad858209
allow dotted name lookups, e.g. "fromkeys = 'UserDict:UserDict.fromkeys'.

--- a/test_apipkg.py
+++ b/test_apipkg.py
@@ -346,3 +346,15 @@ def test_chdir_with_relative_imports_sho
     )
     assert res == 0
 
+
+def test_dotted_name_lookup(tmpdir, monkeypatch):
+    pkgdir = tmpdir.mkdir("dotted_name_lookup")
+    pkgdir.join('__init__.py').write(py.code.Source("""
+        import apipkg
+        apipkg.initpkg(__name__, dict(fromkeys='UserDict:UserDict.fromkeys'))
+    """))
+    monkeypatch.syspath_prepend(tmpdir)
+    import dotted_name_lookup
+    d = dotted_name_lookup.fromkeys(["a", "b"])
+    print d
+    assert d.keys() == ["a", "b"]

--- a/apipkg.py
+++ b/apipkg.py
@@ -30,9 +30,14 @@ def initpkg(pkgname, exportdefs):
 
 def importobj(modpath, attrname):
     module = __import__(modpath, None, None, ['__doc__'])
-    if attrname:
-        return getattr(module, attrname)
-    return module
+    if not attrname:
+        return module
+
+    retval = module
+    names = attrname.split(".")
+    for x in names:
+        retval = getattr(retval, x)
+    return retval
 
 class ApiModule(ModuleType):
     def __init__(self, name, importspec, implprefix=None, attr=None):
_______________________________________________
py-svn mailing list
py-svn@codespeak.net
http://codespeak.net/mailman/listinfo/py-svn

Reply via email to