holger krekel avatar holger krekel committed 56f2070

allow aliasing of whole modules

Comments (0)

Files changed (3)

 - make apipkg memorize the absolute path where a package starts
   importing so that subsequent chdir + imports won't break. 
 
+- allow to alias modules
+
 1.0.0b6
 ----------------------------------------
 
 
 def importobj(modpath, attrname):
     module = __import__(modpath, None, None, ['__doc__'])
-    return getattr(module, attrname)
+    if attrname:
+        return getattr(module, attrname)
+    return module
 
 class ApiModule(ModuleType):
     def __init__(self, name, importspec, implprefix=None, attr=None):
                 sys.modules[subname] = apimod
                 setattr(self, name, apimod)
             else:
-                modpath, attrname = importspec.split(':')
+                parts = importspec.split(':')
+                modpath = parts.pop(0)
+                attrname = parts and parts[0] or ""
                 if modpath[0] == '.':
                     modpath = implprefix + modpath
                 if name == '__doc__':
         assert isinstance(recmodule, apipkg.ApiModule)
         assert recmodule.some.__name__ == "someclass"
 
+    def test_module_alias_import(self, monkeypatch, tmpdir):
+        pkgdir = tmpdir.mkdir("aliasimport")
+        pkgdir.join('__init__.py').write(py.code.Source("""
+            import apipkg
+            apipkg.initpkg(__name__, exportdefs={
+                'some': 'os.path',
+            })
+        """))
+        monkeypatch.syspath_prepend(tmpdir)
+        import aliasimport
+        assert aliasimport.some is py.std.os.path
+
 def xtest_nested_absolute_imports():
     import email
     api_email = apipkg.ApiModule('email',{
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.