Commits

holger krekel  committed e7ffdd5

small internal refinement to AliasModules - only useful when directly calling
AliasModule(...) for now.

  • Participants
  • Parent commits 4baca1f

Comments (0)

Files changed (3)

 import sys
 from types import ModuleType
 
-__version__ = '1.2.dev5'
+__version__ = '1.2.dev6'
 
 def initpkg(pkgname, exportdefs, attr=dict()):
     """ initialize given package from the export definitions. """
     __dict__ = property(__dict__)
 
 
-def AliasModule(modname, modpath):
+def AliasModule(modname, modpath, attrname=None):
     mod = []
 
     def getmod():
         if not mod:
-            mod.append(importobj(modpath, None))
+            x = importobj(modpath, None)
+            if attrname is not None:
+                x = getattr(x, attrname)
+            mod.append(x)
         return mod[0]
 
     class AliasModule(ModuleType):
 
         def __repr__(self):
-            return '<AliasModule %r for %r>' % (modname, modpath)
+            x = modpath
+            if attrname:
+                x += "." + attrname
+            return '<AliasModule %r for %r>' % (modname, x)
 
         def __getattribute__(self, name):
             return getattr(getmod(), name)
         description=
         'apipkg: namespace control and lazy-import mechanism',
         long_description = open('README.txt').read(),
-        version='1.2.dev5',
+        version='1.2.dev6',
         url='http://bitbucket.org/hpk42/apipkg',
         license='MIT License',
         platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],

File test_apipkg.py

     import extra_attributes
     assert extra_attributes.foo == 'bar'
 
+def test_aliasmodule_aliases_an_attribute():
+    am = apipkg.AliasModule("mymod", "pprint", 'PrettyPrinter')
+    r = repr(am)
+    assert "<AliasModule 'mymod' for 'pprint.PrettyPrinter'>" == r
+    assert am.format
+
 def test_aliasmodule_repr():
     am = apipkg.AliasModule("mymod", "sys")
     r = repr(am)