Show More
@@ -1,160 +1,160 b'' | |||
|
1 | 1 | # demandimport.py - global demand-loading of modules for Mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2006, 2007 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | ''' |
|
9 | 9 | demandimport - automatic demandloading of modules |
|
10 | 10 | |
|
11 | 11 | To enable this module, do: |
|
12 | 12 | |
|
13 | 13 | import demandimport; demandimport.enable() |
|
14 | 14 | |
|
15 | 15 | Imports of the following forms will be demand-loaded: |
|
16 | 16 | |
|
17 | 17 | import a, b.c |
|
18 | 18 | import a.b as c |
|
19 | 19 | from a import b,c # a will be loaded immediately |
|
20 | 20 | |
|
21 | 21 | These imports will not be delayed: |
|
22 | 22 | |
|
23 | 23 | from a import * |
|
24 | 24 | b = __import__(a) |
|
25 | 25 | ''' |
|
26 | 26 | |
|
27 | 27 | import __builtin__ |
|
28 | 28 | _origimport = __import__ |
|
29 | 29 | |
|
30 | 30 | class _demandmod(object): |
|
31 | 31 | """module demand-loader and proxy""" |
|
32 | 32 | def __init__(self, name, globals, locals, level): |
|
33 | 33 | if '.' in name: |
|
34 | 34 | head, rest = name.split('.', 1) |
|
35 | 35 | after = [rest] |
|
36 | 36 | else: |
|
37 | 37 | head = name |
|
38 | 38 | after = [] |
|
39 | 39 | object.__setattr__(self, "_data", (head, globals, locals, after, level)) |
|
40 | 40 | object.__setattr__(self, "_module", None) |
|
41 | 41 | def _extend(self, name): |
|
42 | 42 | """add to the list of submodules to load""" |
|
43 | 43 | self._data[3].append(name) |
|
44 | 44 | def _load(self): |
|
45 | 45 | if not self._module: |
|
46 | 46 | head, globals, locals, after, level = self._data |
|
47 | 47 | if level is not None: |
|
48 | mod = _origimport(head, globals, locals, level) | |
|
48 | mod = _origimport(head, globals, locals, level=level) | |
|
49 | 49 | else: |
|
50 | 50 | mod = _origimport(head, globals, locals) |
|
51 | 51 | # load submodules |
|
52 | 52 | def subload(mod, p): |
|
53 | 53 | h, t = p, None |
|
54 | 54 | if '.' in p: |
|
55 | 55 | h, t = p.split('.', 1) |
|
56 | 56 | if not hasattr(mod, h): |
|
57 | 57 | # TODO: should we adjust the level here? |
|
58 | 58 | submod = _demandmod(p, mod.__dict__, mod.__dict__, |
|
59 | 59 | level=level) |
|
60 | 60 | setattr(mod, h, submod) |
|
61 | 61 | elif t: |
|
62 | 62 | subload(getattr(mod, h), t) |
|
63 | 63 | |
|
64 | 64 | for x in after: |
|
65 | 65 | subload(mod, x) |
|
66 | 66 | |
|
67 | 67 | # are we in the locals dictionary still? |
|
68 | 68 | if locals and locals.get(head) == self: |
|
69 | 69 | locals[head] = mod |
|
70 | 70 | object.__setattr__(self, "_module", mod) |
|
71 | 71 | |
|
72 | 72 | def __repr__(self): |
|
73 | 73 | if self._module: |
|
74 | 74 | return "<proxied module '%s'>" % self._data[0] |
|
75 | 75 | return "<unloaded module '%s'>" % self._data[0] |
|
76 | 76 | def __call__(self, *args, **kwargs): |
|
77 | 77 | raise TypeError("%s object is not callable" % repr(self)) |
|
78 | 78 | def __getattribute__(self, attr): |
|
79 | 79 | if attr in ('_data', '_extend', '_load', '_module'): |
|
80 | 80 | return object.__getattribute__(self, attr) |
|
81 | 81 | self._load() |
|
82 | 82 | return getattr(self._module, attr) |
|
83 | 83 | def __setattr__(self, attr, val): |
|
84 | 84 | self._load() |
|
85 | 85 | setattr(self._module, attr, val) |
|
86 | 86 | |
|
87 | 87 | def _demandimport(name, globals=None, locals=None, fromlist=None, level=None): |
|
88 | 88 | if not locals or name in ignore or fromlist == ('*',): |
|
89 | 89 | # these cases we can't really delay |
|
90 | 90 | if level is None: |
|
91 | 91 | return _origimport(name, globals, locals, fromlist) |
|
92 | 92 | else: |
|
93 | 93 | return _origimport(name, globals, locals, fromlist, level) |
|
94 | 94 | elif not fromlist: |
|
95 | 95 | # import a [as b] |
|
96 | 96 | if '.' in name: # a.b |
|
97 | 97 | base, rest = name.split('.', 1) |
|
98 | 98 | # email.__init__ loading email.mime |
|
99 | 99 | if globals and globals.get('__name__', None) == base: |
|
100 | 100 | if level is not None: |
|
101 | 101 | return _origimport(name, globals, locals, fromlist, level) |
|
102 | 102 | else: |
|
103 | 103 | return _origimport(name, globals, locals, fromlist) |
|
104 | 104 | # if a is already demand-loaded, add b to its submodule list |
|
105 | 105 | if base in locals: |
|
106 | 106 | if isinstance(locals[base], _demandmod): |
|
107 | 107 | locals[base]._extend(rest) |
|
108 | 108 | return locals[base] |
|
109 | 109 | return _demandmod(name, globals, locals, level=level) |
|
110 | 110 | else: |
|
111 | 111 | # from a import b,c,d |
|
112 | 112 | if level is not None: |
|
113 | 113 | mod = _origimport(name, globals, locals, level=level) |
|
114 | 114 | else: |
|
115 | 115 | mod = _origimport(name, globals, locals) |
|
116 | 116 | # recurse down the module chain |
|
117 | 117 | for comp in name.split('.')[1:]: |
|
118 | 118 | if not hasattr(mod, comp): |
|
119 | 119 | # TODO: should we adjust the level here? |
|
120 | 120 | submod = _demandmod(comp, mod.__dict__, mod.__dict__, |
|
121 | 121 | level=level) |
|
122 | 122 | setattr(mod, comp, submod) |
|
123 | 123 | mod = getattr(mod, comp) |
|
124 | 124 | for x in fromlist: |
|
125 | 125 | # set requested submodules for demand load |
|
126 | 126 | if not(hasattr(mod, x)): |
|
127 | 127 | # TODO: should we adjust the level here? |
|
128 | 128 | submod = _demandmod(x, mod.__dict__, locals, level=level) |
|
129 | 129 | setattr(mod, x, submod) |
|
130 | 130 | return mod |
|
131 | 131 | |
|
132 | 132 | ignore = [ |
|
133 | 133 | '_hashlib', |
|
134 | 134 | '_xmlplus', |
|
135 | 135 | 'fcntl', |
|
136 | 136 | 'win32com.gen_py', |
|
137 | 137 | '_winreg', # 2.7 mimetypes needs immediate ImportError |
|
138 | 138 | 'pythoncom', |
|
139 | 139 | # imported by tarfile, not available under Windows |
|
140 | 140 | 'pwd', |
|
141 | 141 | 'grp', |
|
142 | 142 | # imported by profile, itself imported by hotshot.stats, |
|
143 | 143 | # not available under Windows |
|
144 | 144 | 'resource', |
|
145 | 145 | # this trips up many extension authors |
|
146 | 146 | 'gtk', |
|
147 | 147 | # setuptools' pkg_resources.py expects "from __main__ import x" to |
|
148 | 148 | # raise ImportError if x not defined |
|
149 | 149 | '__main__', |
|
150 | 150 | '_ssl', # conditional imports in the stdlib, issue1964 |
|
151 | 151 | ] |
|
152 | 152 | |
|
153 | 153 | def enable(): |
|
154 | 154 | "enable global demand-loading of modules" |
|
155 | 155 | __builtin__.__import__ = _demandimport |
|
156 | 156 | |
|
157 | 157 | def disable(): |
|
158 | 158 | "disable global demand-loading of modules" |
|
159 | 159 | __builtin__.__import__ = _origimport |
|
160 | 160 |
General Comments 0
You need to be logged in to leave comments.
Login now