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