Show More
@@ -29,35 +29,29 b' import __builtin__' | |||||
29 |
|
29 | |||
30 | class _demandmod(object): |
|
30 | class _demandmod(object): | |
31 | """module demand-loader and proxy""" |
|
31 | """module demand-loader and proxy""" | |
32 |
def __init__(self, name, globals, locals |
|
32 | def __init__(self, name, globals, locals): | |
33 | if '.' in name: |
|
33 | if '.' in name: | |
34 | head, rest = name.split('.', 1) |
|
34 | head, rest = name.split('.', 1) | |
35 | after = [rest] |
|
35 | after = [rest] | |
36 | else: |
|
36 | else: | |
37 | head = name |
|
37 | head = name | |
38 | after = [] |
|
38 | after = [] | |
39 |
object.__setattr__(self, "_data", (head, globals, locals, after |
|
39 | object.__setattr__(self, "_data", (head, globals, locals, after)) | |
40 | object.__setattr__(self, "_module", None) |
|
40 | object.__setattr__(self, "_module", None) | |
41 | def _extend(self, name): |
|
41 | def _extend(self, name): | |
42 | """add to the list of submodules to load""" |
|
42 | """add to the list of submodules to load""" | |
43 | self._data[3].append(name) |
|
43 | self._data[3].append(name) | |
44 | def _load(self): |
|
44 | def _load(self): | |
45 | if not self._module: |
|
45 | if not self._module: | |
46 |
head, globals, locals, after |
|
46 | head, globals, locals, after = self._data | |
47 | if level is not None: |
|
47 | mod = _origimport(head, globals, locals) | |
48 | mod = _origimport(head, globals, locals, level) |
|
|||
49 | else: |
|
|||
50 | mod = _origimport(head, globals, locals) |
|
|||
51 | # load submodules |
|
48 | # load submodules | |
52 | def subload(mod, p): |
|
49 | def subload(mod, p): | |
53 | h, t = p, None |
|
50 | h, t = p, None | |
54 | if '.' in p: |
|
51 | if '.' in p: | |
55 | h, t = p.split('.', 1) |
|
52 | h, t = p.split('.', 1) | |
56 | if not hasattr(mod, h): |
|
53 | if not hasattr(mod, h): | |
57 | # TODO: should we adjust the level here? |
|
54 | setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__)) | |
58 | submod = _demandmod(p, mod.__dict__, mod.__dict__, |
|
|||
59 | level=level) |
|
|||
60 | setattr(mod, h, submod) |
|
|||
61 | elif t: |
|
55 | elif t: | |
62 | subload(getattr(mod, h), t) |
|
56 | subload(getattr(mod, h), t) | |
63 |
|
57 | |||
@@ -97,36 +91,28 b' def _demandimport(name, globals=None, lo' | |||||
97 | base, rest = name.split('.', 1) |
|
91 | base, rest = name.split('.', 1) | |
98 | # email.__init__ loading email.mime |
|
92 | # email.__init__ loading email.mime | |
99 | if globals and globals.get('__name__', None) == base: |
|
93 | if globals and globals.get('__name__', None) == base: | |
100 | if level is not None: |
|
94 | return _origimport(name, globals, locals, fromlist) | |
101 | return _origimport(name, globals, locals, fromlist, level) |
|
|||
102 | else: |
|
|||
103 | return _origimport(name, globals, locals, fromlist) |
|
|||
104 | # if a is already demand-loaded, add b to its submodule list |
|
95 | # if a is already demand-loaded, add b to its submodule list | |
105 | if base in locals: |
|
96 | if base in locals: | |
106 | if isinstance(locals[base], _demandmod): |
|
97 | if isinstance(locals[base], _demandmod): | |
107 | locals[base]._extend(rest) |
|
98 | locals[base]._extend(rest) | |
108 | return locals[base] |
|
99 | return locals[base] | |
109 |
return _demandmod(name, globals, locals |
|
100 | return _demandmod(name, globals, locals) | |
110 | else: |
|
101 | else: | |
111 | # from a import b,c,d |
|
|||
112 | if level is not None: |
|
102 | if level is not None: | |
113 | mod = _origimport(name, globals, locals, level=level) |
|
103 | # from . import b,c,d or from .a import b,c,d | |
114 | else: |
|
104 | return _origimport(name, globals, locals, fromlist, level) | |
115 | mod = _origimport(name, globals, locals) |
|
105 | # from a import b,c,d | |
|
106 | mod = _origimport(name, globals, locals) | |||
116 | # recurse down the module chain |
|
107 | # recurse down the module chain | |
117 | for comp in name.split('.')[1:]: |
|
108 | for comp in name.split('.')[1:]: | |
118 | if not hasattr(mod, comp): |
|
109 | if not hasattr(mod, comp): | |
119 | # TODO: should we adjust the level here? |
|
110 | setattr(mod, comp, _demandmod(comp, mod.__dict__, mod.__dict__)) | |
120 | submod = _demandmod(comp, mod.__dict__, mod.__dict__, |
|
|||
121 | level=level) |
|
|||
122 | setattr(mod, comp, submod) |
|
|||
123 | mod = getattr(mod, comp) |
|
111 | mod = getattr(mod, comp) | |
124 | for x in fromlist: |
|
112 | for x in fromlist: | |
125 | # set requested submodules for demand load |
|
113 | # set requested submodules for demand load | |
126 | if not(hasattr(mod, x)): |
|
114 | if not(hasattr(mod, x)): | |
127 | # TODO: should we adjust the level here? |
|
115 | setattr(mod, x, _demandmod(x, mod.__dict__, locals)) | |
128 | submod = _demandmod(x, mod.__dict__, locals, level=level) |
|
|||
129 | setattr(mod, x, submod) |
|
|||
130 | return mod |
|
116 | return mod | |
131 |
|
117 | |||
132 | ignore = [ |
|
118 | ignore = [ |
General Comments 0
You need to be logged in to leave comments.
Login now