2009-10-05 08:22:48 +08:00
|
|
|
"""
|
|
|
|
apipkg: control the exported namespace of a python package.
|
|
|
|
|
|
|
|
see http://pypi.python.org/pypi/apipkg
|
|
|
|
|
|
|
|
(c) holger krekel, 2009 - MIT license
|
|
|
|
"""
|
2009-10-28 04:31:42 +08:00
|
|
|
import sys
|
2009-10-05 08:22:48 +08:00
|
|
|
from types import ModuleType
|
|
|
|
|
2009-12-29 23:29:48 +08:00
|
|
|
__version__ = "1.0b3"
|
2009-10-05 08:22:48 +08:00
|
|
|
|
|
|
|
def initpkg(pkgname, exportdefs):
|
2009-10-29 18:47:12 +08:00
|
|
|
""" initialize given package from the export definitions. """
|
|
|
|
mod = ApiModule(pkgname, exportdefs, implprefix=pkgname)
|
2009-10-28 04:31:42 +08:00
|
|
|
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
|
2009-10-05 08:22:48 +08:00
|
|
|
|
2009-10-29 18:47:12 +08:00
|
|
|
def importobj(modpath, attrname):
|
2009-10-05 08:22:48 +08:00
|
|
|
module = __import__(modpath, None, None, ['__doc__'])
|
|
|
|
return getattr(module, attrname)
|
|
|
|
|
|
|
|
class ApiModule(ModuleType):
|
2009-10-29 18:47:12 +08:00
|
|
|
def __init__(self, name, importspec, implprefix=None):
|
2009-10-05 08:22:48 +08:00
|
|
|
self.__name__ = name
|
2009-12-29 23:29:48 +08:00
|
|
|
self.__all__ = [x for x in importspec if x != '__onfirstaccess__']
|
2009-10-05 08:22:48 +08:00
|
|
|
self.__map__ = {}
|
2009-10-29 18:47:12 +08:00
|
|
|
self.__implprefix__ = implprefix or name
|
2009-10-05 08:22:48 +08:00
|
|
|
for name, importspec in importspec.items():
|
|
|
|
if isinstance(importspec, dict):
|
2009-10-29 18:47:12 +08:00
|
|
|
subname = '%s.%s'%(self.__name__, name)
|
|
|
|
apimod = ApiModule(subname, importspec, implprefix)
|
|
|
|
sys.modules[subname] = apimod
|
2009-10-28 04:31:42 +08:00
|
|
|
setattr(self, name, apimod)
|
2009-10-05 08:22:48 +08:00
|
|
|
else:
|
2009-10-29 18:47:12 +08:00
|
|
|
modpath, attrname = importspec.split(':')
|
|
|
|
if modpath[0] == '.':
|
|
|
|
modpath = implprefix + modpath
|
2009-10-05 08:22:48 +08:00
|
|
|
if name == '__doc__':
|
2009-10-29 18:47:12 +08:00
|
|
|
self.__doc__ = importobj(modpath, attrname)
|
2009-10-05 08:22:48 +08:00
|
|
|
else:
|
2009-10-29 18:47:12 +08:00
|
|
|
self.__map__[name] = (modpath, attrname)
|
2009-10-05 08:22:48 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
2009-12-29 23:29:48 +08:00
|
|
|
l = []
|
|
|
|
if hasattr(self, '__version__'):
|
|
|
|
l.append("version=" + repr(self.__version__))
|
|
|
|
if hasattr(self, '__file__'):
|
|
|
|
l.append('from ' + repr(self.__file__))
|
|
|
|
if l:
|
|
|
|
return '<ApiModule %r %s>' % (self.__name__, " ".join(l))
|
2009-10-28 04:31:42 +08:00
|
|
|
return '<ApiModule %r>' % (self.__name__,)
|
2009-10-05 08:22:48 +08:00
|
|
|
|
|
|
|
def __getattr__(self, name):
|
2009-12-29 23:29:48 +08:00
|
|
|
target = None
|
|
|
|
if '__onfirstaccess__' in self.__map__:
|
|
|
|
target = self.__map__.pop('__onfirstaccess__')
|
|
|
|
importobj(*target)()
|
2009-10-05 08:22:48 +08:00
|
|
|
try:
|
2009-10-29 18:47:12 +08:00
|
|
|
modpath, attrname = self.__map__[name]
|
2009-10-05 08:22:48 +08:00
|
|
|
except KeyError:
|
2009-12-29 23:29:48 +08:00
|
|
|
if target is not None and name != '__onfirstaccess__':
|
|
|
|
# retry, onfirstaccess might have set attrs
|
|
|
|
return getattr(self, name)
|
2009-10-05 08:22:48 +08:00
|
|
|
raise AttributeError(name)
|
|
|
|
else:
|
2009-10-29 18:47:12 +08:00
|
|
|
result = importobj(modpath, attrname)
|
2009-10-05 08:22:48 +08:00
|
|
|
setattr(self, name, result)
|
2009-10-29 18:47:12 +08:00
|
|
|
del self.__map__[name]
|
2009-10-05 08:22:48 +08:00
|
|
|
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:
|
2009-12-29 23:29:48 +08:00
|
|
|
hasattr(self, 'some')
|
2009-10-05 08:22:48 +08:00
|
|
|
for name in self.__all__:
|
|
|
|
hasattr(self, name) # force attribute load, ignore errors
|
|
|
|
return dict
|
|
|
|
__dict__ = property(__dict__)
|