##// END OF EJS Templates
use import_item in ShimModule...
Min RK -
Show More
@@ -6,6 +6,8 b''
6 import sys
6 import sys
7 import types
7 import types
8
8
9 from .importstring import import_item
10
9
11
10 class ShimImporter(object):
12 class ShimImporter(object):
11 """Import hook for a shim.
13 """Import hook for a shim.
@@ -36,10 +38,7 b' class ShimImporter(object):'
36 def load_module(self, fullname):
38 def load_module(self, fullname):
37 """Import the mirrored module, and insert it into sys.modules"""
39 """Import the mirrored module, and insert it into sys.modules"""
38 mirror_name = self._mirror_name(fullname)
40 mirror_name = self._mirror_name(fullname)
39 mod = __import__(mirror_name)
41 mod = import_item(mirror_name)
40 if '.' in mirror_name:
41 name = mirror_name.rsplit('.', 1)[-1]
42 mod = getattr(mod, name)
43 sys.modules[fullname] = mod
42 sys.modules[fullname] = mod
44 return mod
43 return mod
45
44
@@ -70,26 +69,7 b' class ShimModule(types.ModuleType):'
70 def __getattr__(self, key):
69 def __getattr__(self, key):
71 # Use the equivalent of import_item(name), see below
70 # Use the equivalent of import_item(name), see below
72 name = "%s.%s" % (self._mirror, key)
71 name = "%s.%s" % (self._mirror, key)
73
72 try:
74 # NOTE: the code below was copied *verbatim* from
73 return import_item(name)
75 # importstring.import_item. For some very strange reason that makes no
74 except ImportError:
76 # sense to me, if we call it *as a function*, it doesn't work. This
75 raise AttributeError(key)
77 # has something to do with the deep bowels of the import machinery and
78 # I couldn't find a way to make the code work as a standard function
79 # call. But at least since it's an unmodified copy of import_item,
80 # which is used extensively and has a test suite, we can be reasonably
81 # confident this is OK. If anyone finds how to call the function, all
82 # the below could be replaced simply with:
83 #
84 # from IPython.utils.importstring import import_item
85 # return import_item('MIRROR.' + key)
86
87 parts = name.rsplit('.', 1)
88 if len(parts) == 2:
89 # called with 'foo.bar....'
90 package, obj = parts
91 module = __import__(package, fromlist=[obj])
92 return getattr(module, obj)
93 else:
94 # called with un-dotted string
95 return __import__(parts[0])
General Comments 0
You need to be logged in to leave comments. Login now