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