##// END OF EJS Templates
use getattr in import_item, shim_module...
Min RK -
Show More
@@ -1,50 +1,39 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 A simple utility to import something by its string name.
3 A simple utility to import something by its string name.
4
5 Authors:
6
7 * Brian Granger
8 """
4 """
9
5
10 #-----------------------------------------------------------------------------
6 # Copyright (c) IPython Development Team.
11 # Copyright (C) 2008-2011 The IPython Development Team
7 # Distributed under the terms of the Modified BSD License.
12 #
13 # Distributed under the terms of the BSD License. The full license is in
14 # the file COPYING, distributed as part of this software.
15 #-----------------------------------------------------------------------------
16
8
17 #-----------------------------------------------------------------------------
18 # Functions and classes
19 #-----------------------------------------------------------------------------
20
9
21 def import_item(name):
10 def import_item(name):
22 """Import and return ``bar`` given the string ``foo.bar``.
11 """Import and return ``bar`` given the string ``foo.bar``.
23
12
24 Calling ``bar = import_item("foo.bar")`` is the functional equivalent of
13 Calling ``bar = import_item("foo.bar")`` is the functional equivalent of
25 executing the code ``from foo import bar``.
14 executing the code ``from foo import bar``.
26
15
27 Parameters
16 Parameters
28 ----------
17 ----------
29 name : string
18 name : string
30 The fully qualified name of the module/package being imported.
19 The fully qualified name of the module/package being imported.
31
20
32 Returns
21 Returns
33 -------
22 -------
34 mod : module object
23 mod : module object
35 The module that was imported.
24 The module that was imported.
36 """
25 """
37
26
38 parts = name.rsplit('.', 1)
27 parts = name.rsplit('.', 1)
39 if len(parts) == 2:
28 if len(parts) == 2:
40 # called with 'foo.bar....'
29 # called with 'foo.bar....'
41 package, obj = parts
30 package, obj = parts
42 module = __import__(package, fromlist=[obj])
31 module = __import__(package, fromlist=[obj])
43 try:
32 try:
44 pak = module.__dict__[obj]
33 pak = getattr(module, obj)
45 except KeyError:
34 except AttributeError:
46 raise ImportError('No module named %s' % obj)
35 raise ImportError('No module named %s' % obj)
47 return pak
36 return pak
48 else:
37 else:
49 # called with un-dotted string
38 # called with un-dotted string
50 return __import__(parts[0])
39 return __import__(parts[0])
@@ -1,43 +1,39 b''
1 """A shim module for deprecated imports
1 """A shim module for deprecated imports
2 """
2 """
3 # Copyright (c) IPython Development Team.
3 # Copyright (c) IPython Development Team.
4 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
5
5
6 import types
6 import types
7
7
8 class ShimModule(types.ModuleType):
8 class ShimModule(types.ModuleType):
9
9
10 def __init__(self, *args, **kwargs):
10 def __init__(self, *args, **kwargs):
11 self._mirror = kwargs.pop("mirror")
11 self._mirror = kwargs.pop("mirror")
12 super(ShimModule, self).__init__(*args, **kwargs)
12 super(ShimModule, self).__init__(*args, **kwargs)
13
13
14 def __getattr__(self, key):
14 def __getattr__(self, key):
15 # Use the equivalent of import_item(name), see below
15 # Use the equivalent of import_item(name), see below
16 name = "%s.%s" % (self._mirror, key)
16 name = "%s.%s" % (self._mirror, key)
17
17
18 # NOTE: the code below is copied *verbatim* from
18 # NOTE: the code below was copied *verbatim* from
19 # importstring.import_item. For some very strange reason that makes no
19 # importstring.import_item. For some very strange reason that makes no
20 # sense to me, if we call it *as a function*, it doesn't work. This
20 # sense to me, if we call it *as a function*, it doesn't work. This
21 # has something to do with the deep bowels of the import machinery and
21 # has something to do with the deep bowels of the import machinery and
22 # I couldn't find a way to make the code work as a standard function
22 # I couldn't find a way to make the code work as a standard function
23 # call. But at least since it's an unmodified copy of import_item,
23 # call. But at least since it's an unmodified copy of import_item,
24 # which is used extensively and has a test suite, we can be reasonably
24 # which is used extensively and has a test suite, we can be reasonably
25 # confident this is OK. If anyone finds how to call the function, all
25 # confident this is OK. If anyone finds how to call the function, all
26 # the below could be replaced simply with:
26 # the below could be replaced simply with:
27 #
27 #
28 # from IPython.utils.importstring import import_item
28 # from IPython.utils.importstring import import_item
29 # return import_item('MIRROR.' + key)
29 # return import_item('MIRROR.' + key)
30
30
31 parts = name.rsplit('.', 1)
31 parts = name.rsplit('.', 1)
32 if len(parts) == 2:
32 if len(parts) == 2:
33 # called with 'foo.bar....'
33 # called with 'foo.bar....'
34 package, obj = parts
34 package, obj = parts
35 module = __import__(package, fromlist=[obj])
35 module = __import__(package, fromlist=[obj])
36 try:
36 return getattr(module, obj)
37 pak = module.__dict__[obj]
38 except KeyError:
39 raise AttributeError(obj)
40 return pak
41 else:
37 else:
42 # called with un-dotted string
38 # called with un-dotted string
43 return __import__(parts[0])
39 return __import__(parts[0])
General Comments 0
You need to be logged in to leave comments. Login now