parent
							
								
									690ccaedc1
								
							
						
					
					
						commit
						455b0afdfe
					
				| 
						 | 
					@ -11,54 +11,51 @@ from types import ModuleType
 | 
				
			||||||
__version__ = "1.0b2"
 | 
					__version__ = "1.0b2"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def initpkg(pkgname, exportdefs):
 | 
					def initpkg(pkgname, exportdefs):
 | 
				
			||||||
    """ initialize given package from the export definitions.
 | 
					    """ initialize given package from the export definitions. """
 | 
				
			||||||
        replace it in sys.modules
 | 
					    mod = ApiModule(pkgname, exportdefs, implprefix=pkgname)
 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
    mod = ApiModule(pkgname, exportdefs)
 | 
					 | 
				
			||||||
    oldmod = sys.modules[pkgname]
 | 
					    oldmod = sys.modules[pkgname]
 | 
				
			||||||
    mod.__file__ = getattr(oldmod, '__file__', None)
 | 
					    mod.__file__ = getattr(oldmod, '__file__', None)
 | 
				
			||||||
    mod.__version__ = getattr(oldmod, '__version__', None)
 | 
					    mod.__version__ = getattr(oldmod, '__version__', None)
 | 
				
			||||||
    mod.__path__ = getattr(oldmod, '__path__', None)
 | 
					    mod.__path__ = getattr(oldmod, '__path__', None)
 | 
				
			||||||
    sys.modules[pkgname]  = mod
 | 
					    sys.modules[pkgname]  = mod
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def importobj(importspec):
 | 
					def importobj(modpath, attrname):
 | 
				
			||||||
    """ return object specified by importspec."""
 | 
					 | 
				
			||||||
    modpath, attrname = importspec.split(":")
 | 
					 | 
				
			||||||
    module = __import__(modpath, None, None, ['__doc__'])
 | 
					    module = __import__(modpath, None, None, ['__doc__'])
 | 
				
			||||||
    return getattr(module, attrname)
 | 
					    return getattr(module, attrname)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ApiModule(ModuleType):
 | 
					class ApiModule(ModuleType):
 | 
				
			||||||
    def __init__(self, name, importspec, parent=None):
 | 
					    def __init__(self, name, importspec, implprefix=None):
 | 
				
			||||||
        self.__name__ = name
 | 
					        self.__name__ = name
 | 
				
			||||||
        self.__all__ = list(importspec)
 | 
					        self.__all__ = list(importspec)
 | 
				
			||||||
        self.__map__ = {}
 | 
					        self.__map__ = {}
 | 
				
			||||||
 | 
					        self.__implprefix__ = implprefix or name
 | 
				
			||||||
        for name, importspec in importspec.items():
 | 
					        for name, importspec in importspec.items():
 | 
				
			||||||
            if isinstance(importspec, dict):
 | 
					            if isinstance(importspec, dict):
 | 
				
			||||||
                package = '%s.%s'%(self.__name__, name)
 | 
					                subname = '%s.%s'%(self.__name__, name)
 | 
				
			||||||
                apimod = ApiModule(package, importspec, parent=self)
 | 
					                apimod = ApiModule(subname, importspec, implprefix)
 | 
				
			||||||
                sys.modules[package] = apimod
 | 
					                sys.modules[subname] = apimod
 | 
				
			||||||
                setattr(self, name, apimod)
 | 
					                setattr(self, name, apimod)
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                if not importspec.count(":") == 1:
 | 
					                modpath, attrname = importspec.split(':')
 | 
				
			||||||
                    raise ValueError("invalid importspec %r" % (importspec,))
 | 
					                if modpath[0] == '.':
 | 
				
			||||||
 | 
					                    modpath = implprefix + modpath
 | 
				
			||||||
                if name == '__doc__':
 | 
					                if name == '__doc__':
 | 
				
			||||||
                    self.__doc__ = importobj(importspec)
 | 
					                    self.__doc__ = importobj(modpath, attrname)
 | 
				
			||||||
                else:
 | 
					                else:
 | 
				
			||||||
                    if importspec[0] == '.':
 | 
					                    self.__map__[name] = (modpath, attrname)
 | 
				
			||||||
                        importspec = self.__name__ + importspec
 | 
					 | 
				
			||||||
                    self.__map__[name] = importspec
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __repr__(self):
 | 
					    def __repr__(self):
 | 
				
			||||||
        return '<ApiModule %r>' % (self.__name__,)
 | 
					        return '<ApiModule %r>' % (self.__name__,)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __getattr__(self, name):
 | 
					    def __getattr__(self, name):
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            importspec = self.__map__.pop(name)
 | 
					            modpath, attrname = self.__map__[name]
 | 
				
			||||||
        except KeyError:
 | 
					        except KeyError:
 | 
				
			||||||
            raise AttributeError(name)
 | 
					            raise AttributeError(name)
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            result = importobj(importspec)
 | 
					            result = importobj(modpath, attrname)
 | 
				
			||||||
            setattr(self, name, result)
 | 
					            setattr(self, name, result)
 | 
				
			||||||
 | 
					            del self.__map__[name]
 | 
				
			||||||
            return result
 | 
					            return result
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __dict__(self):
 | 
					    def __dict__(self):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue