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