Commits

Ralf Schmitt committed afac3bb

make importing from an alias module work.

Comments (0)

Files changed (1)

                 attrname = parts and parts[0] or ""
                 if modpath[0] == '.':
                     modpath = implprefix + modpath
-                if name == '__doc__':
-                    self.__doc__ = importobj(modpath, attrname)
+
+                if not attrname:
+                    subname = '%s.%s'%(self.__name__, name)
+                    apimod = AliasModule(subname, modpath)
+                    sys.modules[subname] = apimod
+                    setattr(self, name, apimod)
                 else:
-                    self.__map__[name] = (modpath, attrname)
+                    if name == '__doc__':
+                        self.__doc__ = importobj(modpath, attrname)
+                    else:
+                        self.__map__[name] = (modpath, attrname)
 
     def __repr__(self):
         l = []
                     pass
         return dict
     __dict__ = property(__dict__)
+
+class AliasModule(ModuleType):
+    def __init__(self, name, modpath):
+        self.__name__ = name
+        self.__modpath = modpath
+
+    def __repr__(self):
+        l = []
+        if hasattr(self, '__version__'):
+            l.append("version=" + repr(self.__version__))
+        if hasattr(self, '__file__'):
+            l.append('from ' + repr(self.__file__))
+        if l:
+            return '<AliasModule %r %s>' % (self.__name__, " ".join(l))
+        return '<AliasModule %r>' % (self.__name__,)
+
+    def __getattr__(self, name):
+        mod = importobj(self.__modpath, None)
+        result = getattr(mod, name)
+        setattr(self, name, result)
+        for k, v in mod.__dict__.items():
+            setattr(self, k, v)
+        return result
+
+    def __dict__(self):
+        # force all the content of the module to be loaded when __dict__ is read
+        dictdescr = ModuleType.__dict__['__dict__']
+        dict = dictdescr.__get__(self)
+        if dict is not None:
+            hasattr(self, 'some')
+        return dict
+    __dict__ = property(__dict__)
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.