| 1 |
""" |
| 2 |
apipkg: control the exported namespace of a python package. |
| 3 |
|
| 4 |
see http://pypi.python.org/pypi/apipkg |
| 5 |
|
| 6 |
(c) holger krekel, 2009 - MIT license |
| 7 |
""" |
| 8 |
import os |
| 9 |
import sys |
| 10 |
from types import ModuleType |
| 11 |
|
| 12 |
__version__ = '1.3.dev' |
| 13 |
|
| 14 |
def _py_abspath(path): |
| 15 |
""" |
| 16 |
special version of abspath |
| 17 |
that will leave paths from jython jars alone |
| 18 |
""" |
| 19 |
if path.startswith('__pyclasspath__'): |
| 20 |
|
| 21 |
return path |
| 22 |
else: |
| 23 |
return os.path.abspath(path) |
| 24 |
|
| 25 |
def initpkg(pkgname, exportdefs, attr=dict()): |
| 26 |
""" initialize given package from the export definitions. """ |
| 27 |
oldmod = sys.modules.get(pkgname) |
| 28 |
d = {} |
| 29 |
f = getattr(oldmod, '__file__', None) |
| 30 |
if f: |
| 31 |
f = _py_abspath(f) |
| 32 |
d['__file__'] = f |
| 33 |
if hasattr(oldmod, '__version__'): |
| 34 |
d['__version__'] = oldmod.__version__ |
| 35 |
if hasattr(oldmod, '__loader__'): |
| 36 |
d['__loader__'] = oldmod.__loader__ |
| 37 |
if hasattr(oldmod, '__path__'): |
| 38 |
d['__path__'] = [_py_abspath(p) for p in oldmod.__path__] |
| 39 |
if '__doc__' not in exportdefs and getattr(oldmod, '__doc__', None): |
| 40 |
d['__doc__'] = oldmod.__doc__ |
| 41 |
d["__spec__"] = getattr(oldmod, "__spec__", None) |
| 42 |
d.update(attr) |
| 43 |
if hasattr(oldmod, "__dict__"): |
| 44 |
oldmod.__dict__.update(d) |
| 45 |
mod = ApiModule(pkgname, exportdefs, implprefix=pkgname, attr=d) |
| 46 |
sys.modules[pkgname] = mod |
| 47 |
|
| 48 |
def importobj(modpath, attrname): |
| 49 |
module = __import__(modpath, None, None, ['__doc__']) |
| 50 |
if not attrname: |
| 51 |
return module |
| 52 |
|
| 53 |
retval = module |
| 54 |
names = attrname.split(".") |
| 55 |
for x in names: |
| 56 |
retval = getattr(retval, x) |
| 57 |
return retval |
| 58 |
|
| 59 |
class ApiModule(ModuleType): |
| 60 |
def __docget(self): |
| 61 |
try: |
| 62 |
return self.__doc |
| 63 |
except AttributeError: |
| 64 |
if '__doc__' in self.__map__: |
| 65 |
return self.__makeattr('__doc__') |
| 66 |
def __docset(self, value): |
| 67 |
self.__doc = value |
| 68 |
__doc__ = property(__docget, __docset) |
| 69 |
|
| 70 |
def __init__(self, name, importspec, implprefix=None, attr=None): |
| 71 |
self.__name__ = name |
| 72 |
self.__all__ = [x for x in importspec if x != '__onfirstaccess__'] |
| 73 |
self.__map__ = {} |
| 74 |
self.__implprefix__ = implprefix or name |
| 75 |
if attr: |
| 76 |
for name, val in attr.items(): |
| 77 |
|
| 78 |
setattr(self, name, val) |
| 79 |
for name, importspec in importspec.items(): |
| 80 |
if isinstance(importspec, dict): |
| 81 |
subname = '%s.%s' % (self.__name__, name) |
| 82 |
apimod = ApiModule(subname, importspec, implprefix) |
| 83 |
sys.modules[subname] = apimod |
| 84 |
setattr(self, name, apimod) |
| 85 |
else: |
| 86 |
parts = importspec.split(':') |
| 87 |
modpath = parts.pop(0) |
| 88 |
attrname = parts and parts[0] or "" |
| 89 |
if modpath[0] == '.': |
| 90 |
modpath = implprefix + modpath |
| 91 |
|
| 92 |
if not attrname: |
| 93 |
subname = '%s.%s' % (self.__name__, name) |
| 94 |
apimod = AliasModule(subname, modpath) |
| 95 |
sys.modules[subname] = apimod |
| 96 |
if '.' not in name: |
| 97 |
setattr(self, name, apimod) |
| 98 |
else: |
| 99 |
self.__map__[name] = (modpath, attrname) |
| 100 |
|
| 101 |
def __repr__(self): |
| 102 |
l = [] |
| 103 |
if hasattr(self, '__version__'): |
| 104 |
l.append("version=" + repr(self.__version__)) |
| 105 |
if hasattr(self, '__file__'): |
| 106 |
l.append('from ' + repr(self.__file__)) |
| 107 |
if l: |
| 108 |
return '<ApiModule %r %s>' % (self.__name__, " ".join(l)) |
| 109 |
return '<ApiModule %r>' % (self.__name__,) |
| 110 |
|
| 111 |
def __makeattr(self, name): |
| 112 |
"""lazily compute value for name or raise AttributeError if unknown.""" |
| 113 |
|
| 114 |
target = None |
| 115 |
if '__onfirstaccess__' in self.__map__: |
| 116 |
target = self.__map__.pop('__onfirstaccess__') |
| 117 |
importobj(*target)() |
| 118 |
try: |
| 119 |
modpath, attrname = self.__map__[name] |
| 120 |
except KeyError: |
| 121 |
if target is not None and name != '__onfirstaccess__': |
| 122 |
|
| 123 |
return getattr(self, name) |
| 124 |
raise AttributeError(name) |
| 125 |
else: |
| 126 |
result = importobj(modpath, attrname) |
| 127 |
setattr(self, name, result) |
| 128 |
try: |
| 129 |
del self.__map__[name] |
| 130 |
except KeyError: |
| 131 |
pass |
| 132 |
return result |
| 133 |
|
| 134 |
__getattr__ = __makeattr |
| 135 |
|
| 136 |
def __dict__(self): |
| 137 |
|
| 138 |
dictdescr = ModuleType.__dict__['__dict__'] |
| 139 |
dict = dictdescr.__get__(self) |
| 140 |
if dict is not None: |
| 141 |
hasattr(self, 'some') |
| 142 |
for name in self.__all__: |
| 143 |
try: |
| 144 |
self.__makeattr(name) |
| 145 |
except AttributeError: |
| 146 |
pass |
| 147 |
return dict |
| 148 |
__dict__ = property(__dict__) |
| 149 |
|
| 150 |
|
| 151 |
def AliasModule(modname, modpath, attrname=None): |
| 152 |
mod = [] |
| 153 |
|
| 154 |
def getmod(): |
| 155 |
if not mod: |
| 156 |
x = importobj(modpath, None) |
| 157 |
if attrname is not None: |
| 158 |
x = getattr(x, attrname) |
| 159 |
mod.append(x) |
| 160 |
return mod[0] |
| 161 |
|
| 162 |
class AliasModule(ModuleType): |
| 163 |
|
| 164 |
def __repr__(self): |
| 165 |
x = modpath |
| 166 |
if attrname: |
| 167 |
x += "." + attrname |
| 168 |
return '<AliasModule %r for %r>' % (modname, x) |
| 169 |
|
| 170 |
def __getattribute__(self, name): |
| 171 |
try: |
| 172 |
return getattr(getmod(), name) |
| 173 |
except ImportError: |
| 174 |
return None |
| 175 |
|
| 176 |
def __setattr__(self, name, value): |
| 177 |
setattr(getmod(), name, value) |
| 178 |
|
| 179 |
def __delattr__(self, name): |
| 180 |
delattr(getmod(), name) |
| 181 |
|
| 182 |
return AliasModule(str(modname)) |