From 455b0afdfe685624fd80ec8a9e5f3708093d66a1 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Thu, 29 Oct 2009 11:47:12 +0100 Subject: [PATCH] use new apipkg version --HG-- branch : trunk --- _py/apipkg.py | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/_py/apipkg.py b/_py/apipkg.py index 668b48726..c34658dd0 100644 --- a/_py/apipkg.py +++ b/_py/apipkg.py @@ -11,54 +11,51 @@ from types import ModuleType __version__ = "1.0b2" def initpkg(pkgname, exportdefs): - """ initialize given package from the export definitions. - replace it in sys.modules - """ - mod = ApiModule(pkgname, exportdefs) + """ initialize given package from the export definitions. """ + mod = ApiModule(pkgname, exportdefs, implprefix=pkgname) oldmod = sys.modules[pkgname] mod.__file__ = getattr(oldmod, '__file__', None) mod.__version__ = getattr(oldmod, '__version__', None) mod.__path__ = getattr(oldmod, '__path__', None) sys.modules[pkgname] = mod -def importobj(importspec): - """ return object specified by importspec.""" - modpath, attrname = importspec.split(":") +def importobj(modpath, attrname): module = __import__(modpath, None, None, ['__doc__']) return getattr(module, attrname) class ApiModule(ModuleType): - def __init__(self, name, importspec, parent=None): + def __init__(self, name, importspec, implprefix=None): self.__name__ = name self.__all__ = list(importspec) self.__map__ = {} + self.__implprefix__ = implprefix or name for name, importspec in importspec.items(): if isinstance(importspec, dict): - package = '%s.%s'%(self.__name__, name) - apimod = ApiModule(package, importspec, parent=self) - sys.modules[package] = apimod + subname = '%s.%s'%(self.__name__, name) + apimod = ApiModule(subname, importspec, implprefix) + sys.modules[subname] = apimod setattr(self, name, apimod) else: - if not importspec.count(":") == 1: - raise ValueError("invalid importspec %r" % (importspec,)) + modpath, attrname = importspec.split(':') + if modpath[0] == '.': + modpath = implprefix + modpath if name == '__doc__': - self.__doc__ = importobj(importspec) + self.__doc__ = importobj(modpath, attrname) else: - if importspec[0] == '.': - importspec = self.__name__ + importspec - self.__map__[name] = importspec + self.__map__[name] = (modpath, attrname) def __repr__(self): return '' % (self.__name__,) def __getattr__(self, name): try: - importspec = self.__map__.pop(name) + modpath, attrname = self.__map__[name] except KeyError: raise AttributeError(name) else: - result = importobj(importspec) + result = importobj(modpath, attrname) setattr(self, name, result) + del self.__map__[name] return result def __dict__(self):