Show More
@@ -1,134 +1,135 b'' | |||||
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, 2007 Matt Mackall <mpm@selenic.com> |
|
3 | # Copyright 2006, 2007 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 | import __builtin__ | |||
27 | _origimport = __import__ |
|
28 | _origimport = __import__ | |
28 |
|
29 | |||
29 | class _demandmod(object): |
|
30 | class _demandmod(object): | |
30 | """module demand-loader and proxy""" |
|
31 | """module demand-loader and proxy""" | |
31 | def __init__(self, name, globals, locals): |
|
32 | def __init__(self, name, globals, locals): | |
32 | if '.' in name: |
|
33 | if '.' in name: | |
33 | head, rest = name.split('.', 1) |
|
34 | head, rest = name.split('.', 1) | |
34 | after = [rest] |
|
35 | after = [rest] | |
35 | else: |
|
36 | else: | |
36 | head = name |
|
37 | head = name | |
37 | after = [] |
|
38 | after = [] | |
38 | object.__setattr__(self, "_data", (head, globals, locals, after)) |
|
39 | object.__setattr__(self, "_data", (head, globals, locals, after)) | |
39 | object.__setattr__(self, "_module", None) |
|
40 | object.__setattr__(self, "_module", None) | |
40 | def _extend(self, name): |
|
41 | def _extend(self, name): | |
41 | """add to the list of submodules to load""" |
|
42 | """add to the list of submodules to load""" | |
42 | self._data[3].append(name) |
|
43 | self._data[3].append(name) | |
43 | def _load(self): |
|
44 | def _load(self): | |
44 | if not self._module: |
|
45 | if not self._module: | |
45 | head, globals, locals, after = self._data |
|
46 | head, globals, locals, after = self._data | |
46 | mod = _origimport(head, globals, locals) |
|
47 | mod = _origimport(head, globals, locals) | |
47 | # load submodules |
|
48 | # load submodules | |
48 | def subload(mod, p): |
|
49 | def subload(mod, p): | |
49 | h, t = p, None |
|
50 | h, t = p, None | |
50 | if '.' in p: |
|
51 | if '.' in p: | |
51 | h, t = p.split('.', 1) |
|
52 | h, t = p.split('.', 1) | |
52 | if not hasattr(mod, h): |
|
53 | if not hasattr(mod, h): | |
53 | setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__)) |
|
54 | setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__)) | |
54 | elif t: |
|
55 | elif t: | |
55 | subload(getattr(mod, h), t) |
|
56 | subload(getattr(mod, h), t) | |
56 |
|
57 | |||
57 | for x in after: |
|
58 | for x in after: | |
58 | subload(mod, x) |
|
59 | subload(mod, x) | |
59 |
|
60 | |||
60 | # are we in the locals dictionary still? |
|
61 | # are we in the locals dictionary still? | |
61 | if locals and locals.get(head) == self: |
|
62 | if locals and locals.get(head) == self: | |
62 | locals[head] = mod |
|
63 | locals[head] = mod | |
63 | object.__setattr__(self, "_module", mod) |
|
64 | object.__setattr__(self, "_module", mod) | |
64 |
|
65 | |||
65 | def __repr__(self): |
|
66 | def __repr__(self): | |
66 | if self._module: |
|
67 | if self._module: | |
67 | return "<proxied module '%s'>" % self._data[0] |
|
68 | return "<proxied module '%s'>" % self._data[0] | |
68 | return "<unloaded module '%s'>" % self._data[0] |
|
69 | return "<unloaded module '%s'>" % self._data[0] | |
69 | def __call__(self, *args, **kwargs): |
|
70 | def __call__(self, *args, **kwargs): | |
70 | raise TypeError("%s object is not callable" % repr(self)) |
|
71 | raise TypeError("%s object is not callable" % repr(self)) | |
71 | def __getattribute__(self, attr): |
|
72 | def __getattribute__(self, attr): | |
72 | if attr in ('_data', '_extend', '_load', '_module'): |
|
73 | if attr in ('_data', '_extend', '_load', '_module'): | |
73 | return object.__getattribute__(self, attr) |
|
74 | return object.__getattribute__(self, attr) | |
74 | self._load() |
|
75 | self._load() | |
75 | return getattr(self._module, attr) |
|
76 | return getattr(self._module, attr) | |
76 | def __setattr__(self, attr, val): |
|
77 | def __setattr__(self, attr, val): | |
77 | self._load() |
|
78 | self._load() | |
78 | setattr(self._module, attr, val) |
|
79 | setattr(self._module, attr, val) | |
79 |
|
80 | |||
80 | def _demandimport(name, globals=None, locals=None, fromlist=None, level=None): |
|
81 | def _demandimport(name, globals=None, locals=None, fromlist=None, level=None): | |
81 | if not locals or name in ignore or fromlist == ('*',): |
|
82 | if not locals or name in ignore or fromlist == ('*',): | |
82 | # these cases we can't really delay |
|
83 | # these cases we can't really delay | |
83 | return _origimport(name, globals, locals, fromlist) |
|
84 | return _origimport(name, globals, locals, fromlist) | |
84 | elif not fromlist: |
|
85 | elif not fromlist: | |
85 | # import a [as b] |
|
86 | # import a [as b] | |
86 | if '.' in name: # a.b |
|
87 | if '.' in name: # a.b | |
87 | base, rest = name.split('.', 1) |
|
88 | base, rest = name.split('.', 1) | |
88 | # email.__init__ loading email.mime |
|
89 | # email.__init__ loading email.mime | |
89 | if globals and globals.get('__name__', None) == base: |
|
90 | if globals and globals.get('__name__', None) == base: | |
90 | return _origimport(name, globals, locals, fromlist) |
|
91 | return _origimport(name, globals, locals, fromlist) | |
91 | # if a is already demand-loaded, add b to its submodule list |
|
92 | # if a is already demand-loaded, add b to its submodule list | |
92 | if base in locals: |
|
93 | if base in locals: | |
93 | if isinstance(locals[base], _demandmod): |
|
94 | if isinstance(locals[base], _demandmod): | |
94 | locals[base]._extend(rest) |
|
95 | locals[base]._extend(rest) | |
95 | return locals[base] |
|
96 | return locals[base] | |
96 | return _demandmod(name, globals, locals) |
|
97 | return _demandmod(name, globals, locals) | |
97 | else: |
|
98 | else: | |
98 | if level is not None: |
|
99 | if level is not None: | |
99 | # from . import b,c,d or from .a import b,c,d |
|
100 | # from . import b,c,d or from .a import b,c,d | |
100 | return _origimport(name, globals, locals, fromlist, level) |
|
101 | return _origimport(name, globals, locals, fromlist, level) | |
101 | # from a import b,c,d |
|
102 | # from a import b,c,d | |
102 | mod = _origimport(name, globals, locals) |
|
103 | mod = _origimport(name, globals, locals) | |
103 | # recurse down the module chain |
|
104 | # recurse down the module chain | |
104 | for comp in name.split('.')[1:]: |
|
105 | for comp in name.split('.')[1:]: | |
105 | if not hasattr(mod, comp): |
|
106 | if not hasattr(mod, comp): | |
106 | setattr(mod, comp, _demandmod(comp, mod.__dict__, mod.__dict__)) |
|
107 | setattr(mod, comp, _demandmod(comp, mod.__dict__, mod.__dict__)) | |
107 | mod = getattr(mod, comp) |
|
108 | mod = getattr(mod, comp) | |
108 | for x in fromlist: |
|
109 | for x in fromlist: | |
109 | # set requested submodules for demand load |
|
110 | # set requested submodules for demand load | |
110 | if not(hasattr(mod, x)): |
|
111 | if not(hasattr(mod, x)): | |
111 | setattr(mod, x, _demandmod(x, mod.__dict__, locals)) |
|
112 | setattr(mod, x, _demandmod(x, mod.__dict__, locals)) | |
112 | return mod |
|
113 | return mod | |
113 |
|
114 | |||
114 | ignore = [ |
|
115 | ignore = [ | |
115 | '_hashlib', |
|
116 | '_hashlib', | |
116 | '_xmlplus', |
|
117 | '_xmlplus', | |
117 | 'fcntl', |
|
118 | 'fcntl', | |
118 | 'win32com.gen_py', |
|
119 | 'win32com.gen_py', | |
119 | # imported by tarfile, not available under Windows |
|
120 | # imported by tarfile, not available under Windows | |
120 | 'pwd', |
|
121 | 'pwd', | |
121 | 'grp', |
|
122 | 'grp', | |
122 | # imported by profile, itself imported by hotshot.stats, |
|
123 | # imported by profile, itself imported by hotshot.stats, | |
123 | # not available under Windows |
|
124 | # not available under Windows | |
124 | 'resource', |
|
125 | 'resource', | |
125 | ] |
|
126 | ] | |
126 |
|
127 | |||
127 | def enable(): |
|
128 | def enable(): | |
128 | "enable global demand-loading of modules" |
|
129 | "enable global demand-loading of modules" | |
129 |
__builtin |
|
130 | __builtin__.__import__ = _demandimport | |
130 |
|
131 | |||
131 | def disable(): |
|
132 | def disable(): | |
132 | "disable global demand-loading of modules" |
|
133 | "disable global demand-loading of modules" | |
133 |
__builtin |
|
134 | __builtin__.__import__ = _origimport | |
134 |
|
135 |
General Comments 0
You need to be logged in to leave comments.
Login now