##// END OF EJS Templates
raise AttributeError on missing attribute in ShimModule...
MinRK -
Show More
@@ -1,79 +1,79 b''
1 """
1 """
2 Shim to maintain backwards compatibility with old frontend imports.
2 Shim to maintain backwards compatibility with old frontend imports.
3
3
4 We have moved all contents of the old `frontend` subpackage into top-level
4 We have moved all contents of the old `frontend` subpackage into top-level
5 subpackages (`html`, `qt` and `terminal`), and flattened the notebook into
5 subpackages (`html`, `qt` and `terminal`), and flattened the notebook into
6 just `IPython.html`, formerly `IPython.frontend.html.notebook`.
6 just `IPython.html`, formerly `IPython.frontend.html.notebook`.
7
7
8 This will let code that was making `from IPython.frontend...` calls continue
8 This will let code that was making `from IPython.frontend...` calls continue
9 working, though a warning will be printed.
9 working, though a warning will be printed.
10 """
10 """
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Copyright (c) 2013, IPython Development Team.
13 # Copyright (c) 2013, IPython Development Team.
14 #
14 #
15 # Distributed under the terms of the Modified BSD License.
15 # Distributed under the terms of the Modified BSD License.
16 #
16 #
17 # The full license is in the file COPYING.txt, distributed with this software.
17 # The full license is in the file COPYING.txt, distributed with this software.
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19
19
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21 # Imports
21 # Imports
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23 from __future__ import print_function
23 from __future__ import print_function
24
24
25 # Stdlib
25 # Stdlib
26 import sys
26 import sys
27 import types
27 import types
28 from warnings import warn
28 from warnings import warn
29
29
30 warn("The top-level `frontend` package has been deprecated. "
30 warn("The top-level `frontend` package has been deprecated. "
31 "All its subpackages have been moved to the top `IPython` level.")
31 "All its subpackages have been moved to the top `IPython` level.")
32
32
33 #-----------------------------------------------------------------------------
33 #-----------------------------------------------------------------------------
34 # Class declarations
34 # Class declarations
35 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
36
36
37 class ShimModule(types.ModuleType):
37 class ShimModule(types.ModuleType):
38
38
39 def __init__(self, *args, **kwargs):
39 def __init__(self, *args, **kwargs):
40 self._mirror = kwargs.pop("mirror")
40 self._mirror = kwargs.pop("mirror")
41 super(ShimModule, self).__init__(*args, **kwargs)
41 super(ShimModule, self).__init__(*args, **kwargs)
42
42
43 def __getattr__(self, key):
43 def __getattr__(self, key):
44 # Use the equivalent of import_item(name), see below
44 # Use the equivalent of import_item(name), see below
45 name = "%s.%s" % (self._mirror, key)
45 name = "%s.%s" % (self._mirror, key)
46
46
47 # NOTE: the code below is copied *verbatim* from
47 # NOTE: the code below is copied *verbatim* from
48 # importstring.import_item. For some very strange reason that makes no
48 # importstring.import_item. For some very strange reason that makes no
49 # sense to me, if we call it *as a function*, it doesn't work. This
49 # sense to me, if we call it *as a function*, it doesn't work. This
50 # has something to do with the deep bowels of the import machinery and
50 # has something to do with the deep bowels of the import machinery and
51 # I couldn't find a way to make the code work as a standard function
51 # I couldn't find a way to make the code work as a standard function
52 # call. But at least since it's an unmodified copy of import_item,
52 # call. But at least since it's an unmodified copy of import_item,
53 # which is used extensively and has a test suite, we can be reasonably
53 # which is used extensively and has a test suite, we can be reasonably
54 # confident this is OK. If anyone finds how to call the function, all
54 # confident this is OK. If anyone finds how to call the function, all
55 # the below could be replaced simply with:
55 # the below could be replaced simply with:
56 #
56 #
57 # from IPython.utils.importstring import import_item
57 # from IPython.utils.importstring import import_item
58 # return import_item('MIRROR.' + key)
58 # return import_item('MIRROR.' + key)
59
59
60 parts = name.rsplit('.', 1)
60 parts = name.rsplit('.', 1)
61 if len(parts) == 2:
61 if len(parts) == 2:
62 # called with 'foo.bar....'
62 # called with 'foo.bar....'
63 package, obj = parts
63 package, obj = parts
64 module = __import__(package, fromlist=[obj])
64 module = __import__(package, fromlist=[obj])
65 try:
65 try:
66 pak = module.__dict__[obj]
66 pak = module.__dict__[obj]
67 except KeyError:
67 except KeyError:
68 raise ImportError('No module named %s' % obj)
68 raise AttributeError(obj)
69 return pak
69 return pak
70 else:
70 else:
71 # called with un-dotted string
71 # called with un-dotted string
72 return __import__(parts[0])
72 return __import__(parts[0])
73
73
74
74
75 # Unconditionally insert the shim into sys.modules so that further import calls
75 # Unconditionally insert the shim into sys.modules so that further import calls
76 # trigger the custom attribute access above
76 # trigger the custom attribute access above
77
77
78 sys.modules['IPython.frontend.html.notebook'] = ShimModule('notebook', mirror='IPython.html')
78 sys.modules['IPython.frontend.html.notebook'] = ShimModule('notebook', mirror='IPython.html')
79 sys.modules['IPython.frontend'] = ShimModule('frontend', mirror='IPython')
79 sys.modules['IPython.frontend'] = ShimModule('frontend', mirror='IPython')
General Comments 0
You need to be logged in to leave comments. Login now