Show More
@@ -1,109 +1,115 | |||||
1 | # demandimport.py - global demand-loading of modules for Mercurial |
|
1 | # demandimport.py - global demand-loading of modules for Mercurial | |
2 | # |
|
2 | # | |
3 | # Copyright 2006 Matt Mackall <mpm@selenic.com> |
|
3 | # Copyright 2006 Matt Mackall <mpm@selenic.com> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms |
|
5 | # This software may be used and distributed according to the terms | |
6 | # of the GNU General Public License, incorporated herein by reference. |
|
6 | # of the GNU General Public License, incorporated herein by reference. | |
7 |
|
7 | |||
8 | ''' |
|
8 | ''' | |
9 | demandimport - automatic demandloading of modules |
|
9 | demandimport - automatic demandloading of modules | |
10 |
|
10 | |||
11 | To enable this module, do: |
|
11 | To enable this module, do: | |
12 |
|
12 | |||
13 | import demandimport; demandimport.enable() |
|
13 | import demandimport; demandimport.enable() | |
14 |
|
14 | |||
15 | Imports of the following forms will be demand-loaded: |
|
15 | Imports of the following forms will be demand-loaded: | |
16 |
|
16 | |||
17 | import a, b.c |
|
17 | import a, b.c | |
18 | import a.b as c |
|
18 | import a.b as c | |
19 | from a import b,c # a will be loaded immediately |
|
19 | from a import b,c # a will be loaded immediately | |
20 |
|
20 | |||
21 | These imports will not be delayed: |
|
21 | These imports will not be delayed: | |
22 |
|
22 | |||
23 | from a import * |
|
23 | from a import * | |
24 | b = __import__(a) |
|
24 | b = __import__(a) | |
25 | ''' |
|
25 | ''' | |
26 |
|
26 | |||
27 | _origimport = __import__ |
|
27 | _origimport = __import__ | |
28 |
|
28 | |||
29 | class _demandmod(object): |
|
29 | class _demandmod(object): | |
30 | """module demand-loader and proxy""" |
|
30 | """module demand-loader and proxy""" | |
31 | def __init__(self, name, globals, locals): |
|
31 | def __init__(self, name, globals, locals): | |
32 | if '.' in name: |
|
32 | if '.' in name: | |
33 | head, rest = name.split('.', 1) |
|
33 | head, rest = name.split('.', 1) | |
34 | after = [rest] |
|
34 | after = [rest] | |
35 | else: |
|
35 | else: | |
36 | head = name |
|
36 | head = name | |
37 | after = [] |
|
37 | after = [] | |
38 | object.__setattr__(self, "_data", (head, globals, locals, after)) |
|
38 | object.__setattr__(self, "_data", (head, globals, locals, after)) | |
39 | object.__setattr__(self, "_module", None) |
|
39 | object.__setattr__(self, "_module", None) | |
40 | def _extend(self, name): |
|
40 | def _extend(self, name): | |
41 | """add to the list of submodules to load""" |
|
41 | """add to the list of submodules to load""" | |
42 | self._data[3].append(name) |
|
42 | self._data[3].append(name) | |
43 | def _load(self): |
|
43 | def _load(self): | |
44 | if not self._module: |
|
44 | if not self._module: | |
45 | head, globals, locals, after = self._data |
|
45 | head, globals, locals, after = self._data | |
46 | mod = _origimport(head, globals, locals) |
|
46 | mod = _origimport(head, globals, locals) | |
47 | # load submodules |
|
47 | # load submodules | |
|
48 | def subload(mod, p): | |||
|
49 | h, t = p, None | |||
|
50 | if '.' in p: | |||
|
51 | h, t = p.split('.', 1) | |||
|
52 | if not hasattr(mod, h): | |||
|
53 | setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__)) | |||
|
54 | else: | |||
|
55 | subload(getattr(mod, h), t) | |||
|
56 | ||||
48 | for x in after: |
|
57 | for x in after: | |
49 |
|
|
58 | subload(mod, x) | |
50 | if '.' in x: |
|
59 | ||
51 | hx = x.split('.')[0] |
|
|||
52 | if not hasattr(mod, hx): |
|
|||
53 | setattr(mod, hx, _demandmod(x, mod.__dict__, mod.__dict__)) |
|
|||
54 | # are we in the locals dictionary still? |
|
60 | # are we in the locals dictionary still? | |
55 | if locals and locals.get(head) == self: |
|
61 | if locals and locals.get(head) == self: | |
56 | locals[head] = mod |
|
62 | locals[head] = mod | |
57 | object.__setattr__(self, "_module", mod) |
|
63 | object.__setattr__(self, "_module", mod) | |
58 | def __repr__(self): |
|
64 | def __repr__(self): | |
59 | return "<unloaded module '%s'>" % self._data[0] |
|
65 | return "<unloaded module '%s'>" % self._data[0] | |
60 | def __call__(self, *args, **kwargs): |
|
66 | def __call__(self, *args, **kwargs): | |
61 | raise TypeError("'unloaded module' object is not callable") |
|
67 | raise TypeError("'unloaded module' object is not callable") | |
62 | def __getattribute__(self, attr): |
|
68 | def __getattribute__(self, attr): | |
63 | if attr in ('_data', '_extend', '_load', '_module'): |
|
69 | if attr in ('_data', '_extend', '_load', '_module'): | |
64 | return object.__getattribute__(self, attr) |
|
70 | return object.__getattribute__(self, attr) | |
65 | self._load() |
|
71 | self._load() | |
66 | return getattr(self._module, attr) |
|
72 | return getattr(self._module, attr) | |
67 | def __setattr__(self, attr, val): |
|
73 | def __setattr__(self, attr, val): | |
68 | self._load() |
|
74 | self._load() | |
69 | setattr(self._module, attr, val) |
|
75 | setattr(self._module, attr, val) | |
70 |
|
76 | |||
71 | def _demandimport(name, globals=None, locals=None, fromlist=None): |
|
77 | def _demandimport(name, globals=None, locals=None, fromlist=None): | |
72 | if not locals or name in ignore or fromlist == ('*',): |
|
78 | if not locals or name in ignore or fromlist == ('*',): | |
73 | # these cases we can't really delay |
|
79 | # these cases we can't really delay | |
74 | return _origimport(name, globals, locals, fromlist) |
|
80 | return _origimport(name, globals, locals, fromlist) | |
75 | elif not fromlist: |
|
81 | elif not fromlist: | |
76 | # import a [as b] |
|
82 | # import a [as b] | |
77 | if '.' in name: # a.b |
|
83 | if '.' in name: # a.b | |
78 | base, rest = name.split('.', 1) |
|
84 | base, rest = name.split('.', 1) | |
79 | # email.__init__ loading email.mime |
|
85 | # email.__init__ loading email.mime | |
80 | if globals and globals.get('__name__', None) == base: |
|
86 | if globals and globals.get('__name__', None) == base: | |
81 | return _origimport(name, globals, locals, fromlist) |
|
87 | return _origimport(name, globals, locals, fromlist) | |
82 | # if a is already demand-loaded, add b to its submodule list |
|
88 | # if a is already demand-loaded, add b to its submodule list | |
83 | if base in locals: |
|
89 | if base in locals: | |
84 | if isinstance(locals[base], _demandmod): |
|
90 | if isinstance(locals[base], _demandmod): | |
85 | locals[base]._extend(rest) |
|
91 | locals[base]._extend(rest) | |
86 | return locals[base] |
|
92 | return locals[base] | |
87 | return _demandmod(name, globals, locals) |
|
93 | return _demandmod(name, globals, locals) | |
88 | else: |
|
94 | else: | |
89 | # from a import b,c,d |
|
95 | # from a import b,c,d | |
90 | mod = _origimport(name, globals, locals) |
|
96 | mod = _origimport(name, globals, locals) | |
91 | # recurse down the module chain |
|
97 | # recurse down the module chain | |
92 | for comp in name.split('.')[1:]: |
|
98 | for comp in name.split('.')[1:]: | |
93 | mod = getattr(mod, comp) |
|
99 | mod = getattr(mod, comp) | |
94 | for x in fromlist: |
|
100 | for x in fromlist: | |
95 | # set requested submodules for demand load |
|
101 | # set requested submodules for demand load | |
96 | if not(hasattr(mod, x)): |
|
102 | if not(hasattr(mod, x)): | |
97 | setattr(mod, x, _demandmod(x, mod.__dict__, mod.__dict__)) |
|
103 | setattr(mod, x, _demandmod(x, mod.__dict__, mod.__dict__)) | |
98 | return mod |
|
104 | return mod | |
99 |
|
105 | |||
100 | ignore = [] |
|
106 | ignore = [] | |
101 |
|
107 | |||
102 | def enable(): |
|
108 | def enable(): | |
103 | "enable global demand-loading of modules" |
|
109 | "enable global demand-loading of modules" | |
104 | __builtins__["__import__"] = _demandimport |
|
110 | __builtins__["__import__"] = _demandimport | |
105 |
|
111 | |||
106 | def disable(): |
|
112 | def disable(): | |
107 | "disable global demand-loading of modules" |
|
113 | "disable global demand-loading of modules" | |
108 | __builtins__["__import__"] = _origimport |
|
114 | __builtins__["__import__"] = _origimport | |
109 |
|
115 |
General Comments 0
You need to be logged in to leave comments.
Login now