RonnyPfannschmidt / anyvc

generic vcs abstraction (used in pida)

Clone this repository (size: 300.7 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/RonnyPfannschmidt/anyvc/

Changed (Δ24 bytes):

raw changeset »

anyvc/apipkg.py (22 lines added, 24 lines removed)

Up to file-list anyvc/apipkg.py:

@@ -5,59 +5,57 @@ see http://pypi.python.org/pypi/apipkg
5
5
6
6
(c) holger krekel, 2009 - MIT license
7
7
"""
8
import os, sys
8
import sys
9
9
from types import ModuleType
10
10
11
__version__ = "1.0b1"
11
__version__ = "1.0b2"
12
12
13
13
def initpkg(pkgname, exportdefs):
14
    """ initialize given package from the export definitions.
15
        replace it in sys.modules
16
    """
17
    mod = ApiModule(pkgname, exportdefs)
18
    mod.__replaced__ = sys.modules.get(pkgname)
19
    if mod.__replaced__ is not None:
20
        mod.__path__ = mod.__replaced__.__path__
14
    """ initialize given package from the export definitions. """
15
    mod = ApiModule(pkgname, exportdefs, implprefix=pkgname)
16
    oldmod = sys.modules[pkgname]
17
    mod.__file__ = getattr(oldmod, '__file__', None)
18
    mod.__version__ = getattr(oldmod, '__version__', None)
19
    mod.__path__ = getattr(oldmod, '__path__', None)
21
20
    sys.modules[pkgname]  = mod
22
21
23
def importobj(importspec):
24
    """ return object specified by importspec."""
25
    modpath, attrname = importspec.split(":")
22
def importobj(modpath, attrname):
26
23
    module = __import__(modpath, None, None, ['__doc__'])
27
24
    return getattr(module, attrname)
28
25
29
26
class ApiModule(ModuleType):
30
    def __init__(self, name, importspec, parent=None):
27
    def __init__(self, name, importspec, implprefix=None):
31
28
        self.__name__ = name
32
29
        self.__all__ = list(importspec)
33
30
        self.__map__ = {}
31
        self.__implprefix__ = implprefix or name
34
32
        for name, importspec in importspec.items():
35
33
            if isinstance(importspec, dict):
36
                package = '%s.%s'%(self.__name__, name)
37
                apimod = ApiModule(package, importspec, parent=self)
38
                sys.modules[package] = apimod
34
                subname = '%s.%s'%(self.__name__, name)
35
                apimod = ApiModule(subname, importspec, implprefix)
36
                sys.modules[subname] = apimod
39
37
                setattr(self, name, apimod)
40
38
            else:
41
                if not importspec.count(":") == 1:
42
                    raise ValueError("invalid importspec %r" % (importspec,))
39
                modpath, attrname = importspec.split(':')
40
                if modpath[0] == '.':
41
                    modpath = implprefix + modpath
43
42
                if name == '__doc__':
44
                    self.__doc__ = importobj(importspec)
43
                    self.__doc__ = importobj(modpath, attrname)
45
44
                else:
46
                    if importspec[0] == '.':
47
                        importspec = self.__name__ + importspec
48
                    self.__map__[name] = importspec
45
                    self.__map__[name] = (modpath, attrname)
49
46
50
47
    def __repr__(self):
51
48
        return '<ApiModule %r>' % (self.__name__,)
52
49
53
50
    def __getattr__(self, name):
54
51
        try:
55
            importspec = self.__map__.pop(name)
52
            modpath, attrname = self.__map__[name]
56
53
        except KeyError:
57
54
            raise AttributeError(name)
58
55
        else:
59
            result = importobj(importspec)
56
            result = importobj(modpath, attrname)
60
57
            setattr(self, name, result)
58
            del self.__map__[name]
61
59
            return result
62
60
63
61
    def __dict__(self):