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