blob: 3b3076c01bb60b691d39fd0b57f29fa51fabf496 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
import sys
_VENDORED_NAMES = 'packaging',
_SEARCH_PATH = 'pkg_resources._vendor.', ''
class VendorImporter:
"""
A PEP 302 meta path importer for finding optionally-vendored
or otherwise naturally-installed packages from __name__.
"""
def find_module(self, fullname, path=None):
root, base, target = fullname.partition(__name__ + '.')
if root:
return
if not any(map(target.startswith, _VENDORED_NAMES)):
return
return self
def load_module(self, fullname):
root, base, target = fullname.partition(__name__ + '.')
for prefix in _SEARCH_PATH:
try:
__import__(prefix + target)
mod = sys.modules[prefix + target]
sys.modules[fullname] = mod
return mod
except ImportError:
pass
else:
raise ImportError(
"The '{target}' package is required; "
"normally this is bundled with this package so if you get "
"this warning, consult the packager of your "
"distribution.".format(**locals())
)
@classmethod
def install(cls):
if not any(isinstance(imp, cls) for imp in sys.meta_path):
sys.meta_path.append(cls())
VendorImporter.install()
|