Show More
@@ -12,11 +12,7 b' working, though a warning will be printed.' | |||
|
12 | 12 | # Copyright (c) IPython Development Team. |
|
13 | 13 | # Distributed under the terms of the Modified BSD License. |
|
14 | 14 | |
|
15 | from __future__ import print_function | |
|
16 | ||
|
17 | # Stdlib | |
|
18 | 15 | import sys |
|
19 | import types | |
|
20 | 16 | from warnings import warn |
|
21 | 17 | |
|
22 | 18 | warn("The top-level `frontend` package has been deprecated. " |
@@ -27,5 +23,7 b' from IPython.utils.shimmodule import ShimModule' | |||
|
27 | 23 | # Unconditionally insert the shim into sys.modules so that further import calls |
|
28 | 24 | # trigger the custom attribute access above |
|
29 | 25 | |
|
30 |
sys.modules['IPython.frontend.html.notebook'] = ShimModule( |
|
|
31 |
|
|
|
26 | sys.modules['IPython.frontend.html.notebook'] = ShimModule( | |
|
27 | src='IPython.frontend.html.notebook', mirror='IPython.html') | |
|
28 | sys.modules['IPython.frontend'] = ShimModule( | |
|
29 | src='IPython.frontend', mirror='IPython') |
@@ -14,14 +14,18 b' warn("The `IPython.kernel` package has been deprecated. "' | |||
|
14 | 14 | from IPython.utils.shimmodule import ShimModule |
|
15 | 15 | |
|
16 | 16 | # zmq subdir is gone |
|
17 |
sys.modules['IPython.kernel.zmq.session'] = ShimModule( |
|
|
18 | sys.modules['IPython.kernel.zmq'] = ShimModule('zmq', mirror='ipython_kernel') | |
|
17 | sys.modules['IPython.kernel.zmq.session'] = ShimModule( | |
|
18 | src='IPython.kernel.zmq.session', mirror='jupyter_client.session') | |
|
19 | sys.modules['IPython.kernel.zmq'] = ShimModule( | |
|
20 | src='IPython.kernel.zmq', mirror='ipython_kernel') | |
|
19 | 21 | |
|
20 | 22 | for pkg in ('comm', 'inprocess'): |
|
21 | sys.modules['IPython.kernel.%s' % pkg] = ShimModule(pkg, mirror='ipython_kernel.%s' % pkg) | |
|
23 | src = 'IPython.kernel.%s' % pkg | |
|
24 | sys.modules[src] = ShimModule(src=src, mirror='ipython_kernel.%s' % pkg) | |
|
22 | 25 | |
|
23 | 26 | for pkg in ('ioloop', 'blocking'): |
|
24 | sys.modules['IPython.kernel.%s' % pkg] = ShimModule(pkg, mirror='jupyter_client.%s' % pkg) | |
|
27 | src = 'IPython.kernel.%s' % pkg | |
|
28 | sys.modules[src] = ShimModule(src=src, mirror='jupyter_client.%s' % pkg) | |
|
25 | 29 | |
|
26 | 30 | # required for `from IPython.kernel import PKG` |
|
27 | 31 | from ipython_kernel import comm, inprocess |
@@ -4,8 +4,6 b' Shim to maintain backwards compatibility with old IPython.nbconvert imports.' | |||
|
4 | 4 | # Copyright (c) IPython Development Team. |
|
5 | 5 | # Distributed under the terms of the Modified BSD License. |
|
6 | 6 | |
|
7 | from __future__ import print_function | |
|
8 | ||
|
9 | 7 | import sys |
|
10 | 8 | from warnings import warn |
|
11 | 9 | |
@@ -17,4 +15,5 b' from IPython.utils.shimmodule import ShimModule' | |||
|
17 | 15 | # Unconditionally insert the shim into sys.modules so that further import calls |
|
18 | 16 | # trigger the custom attribute access above |
|
19 | 17 | |
|
20 |
sys.modules['IPython.nbconvert'] = ShimModule( |
|
|
18 | sys.modules['IPython.nbconvert'] = ShimModule( | |
|
19 | src='IPython.nbconvert', mirror='jupyter_nbconvert') |
@@ -4,11 +4,7 b' Shim to maintain backwards compatibility with old IPython.nbformat imports.' | |||
|
4 | 4 | # Copyright (c) IPython Development Team. |
|
5 | 5 | # Distributed under the terms of the Modified BSD License. |
|
6 | 6 | |
|
7 | from __future__ import print_function | |
|
8 | ||
|
9 | # Stdlib | |
|
10 | 7 | import sys |
|
11 | import types | |
|
12 | 8 | from warnings import warn |
|
13 | 9 | |
|
14 | 10 | warn("The `IPython.nbformat` package has been deprecated. " |
@@ -19,4 +15,5 b' from IPython.utils.shimmodule import ShimModule' | |||
|
19 | 15 | # Unconditionally insert the shim into sys.modules so that further import calls |
|
20 | 16 | # trigger the custom attribute access above |
|
21 | 17 | |
|
22 |
sys.modules['IPython.nbformat'] = ShimModule( |
|
|
18 | sys.modules['IPython.nbformat'] = ShimModule( | |
|
19 | src='IPython.nbformat', mirror='jupyter_nbformat') |
@@ -4,8 +4,6 b' Shim to maintain backwards compatibility with old IPython.parallel imports.' | |||
|
4 | 4 | # Copyright (c) IPython Development Team. |
|
5 | 5 | # Distributed under the terms of the Modified BSD License. |
|
6 | 6 | |
|
7 | from __future__ import print_function | |
|
8 | ||
|
9 | 7 | import sys |
|
10 | 8 | from warnings import warn |
|
11 | 9 | |
@@ -17,4 +15,6 b' from IPython.utils.shimmodule import ShimModule' | |||
|
17 | 15 | # Unconditionally insert the shim into sys.modules so that further import calls |
|
18 | 16 | # trigger the custom attribute access above |
|
19 | 17 | |
|
20 |
sys.modules['IPython.parallel'] = ShimModule( |
|
|
18 | sys.modules['IPython.parallel'] = ShimModule( | |
|
19 | src='IPython.parallel', mirror='ipython_parallel') | |
|
20 |
@@ -4,11 +4,7 b' Shim to maintain backwards compatibility with old IPython.qt imports.' | |||
|
4 | 4 | # Copyright (c) IPython Development Team. |
|
5 | 5 | # Distributed under the terms of the Modified BSD License. |
|
6 | 6 | |
|
7 | from __future__ import print_function | |
|
8 | ||
|
9 | # Stdlib | |
|
10 | 7 | import sys |
|
11 | import types | |
|
12 | 8 | from warnings import warn |
|
13 | 9 | |
|
14 | 10 | warn("The `IPython.qt` package has been deprecated. " |
@@ -19,4 +15,4 b' from IPython.utils.shimmodule import ShimModule' | |||
|
19 | 15 | # Unconditionally insert the shim into sys.modules so that further import calls |
|
20 | 16 | # trigger the custom attribute access above |
|
21 | 17 | |
|
22 | sys.modules['IPython.qt'] = ShimModule('qt', mirror='jupyter_qtconsole') | |
|
18 | sys.modules['IPython.qt'] = ShimModule(src='IPython.qt', mirror='jupyter_qtconsole') |
@@ -17,4 +17,5 b' from IPython.utils.shimmodule import ShimModule' | |||
|
17 | 17 | # Unconditionally insert the shim into sys.modules so that further import calls |
|
18 | 18 | # trigger the custom attribute access above |
|
19 | 19 | |
|
20 |
sys.modules['IPython.terminal.console'] = ShimModule( |
|
|
20 | sys.modules['IPython.terminal.console'] = ShimModule( | |
|
21 | src='IPython.terminal.console', mirror='jupyter_console') |
@@ -3,13 +3,66 b'' | |||
|
3 | 3 | # Copyright (c) IPython Development Team. |
|
4 | 4 | # Distributed under the terms of the Modified BSD License. |
|
5 | 5 | |
|
6 | import sys | |
|
6 | 7 | import types |
|
7 | 8 | |
|
9 | from .importstring import import_item | |
|
10 | ||
|
11 | ||
|
12 | class ShimImporter(object): | |
|
13 | """Import hook for a shim. | |
|
14 | ||
|
15 | This ensures that submodule imports return the real target module, | |
|
16 | not a clone that will confuse `is` and `isinstance` checks. | |
|
17 | """ | |
|
18 | def __init__(self, src, mirror): | |
|
19 | self.src = src | |
|
20 | self.mirror = mirror | |
|
21 | ||
|
22 | def _mirror_name(self, fullname): | |
|
23 | """get the name of the mirrored module""" | |
|
24 | ||
|
25 | return self.mirror + fullname[len(self.src):] | |
|
26 | ||
|
27 | def find_module(self, fullname, path=None): | |
|
28 | """Return self if we should be used to import the module.""" | |
|
29 | if fullname.startswith(self.src + '.'): | |
|
30 | mirror_name = self._mirror_name(fullname) | |
|
31 | try: | |
|
32 | mod = import_item(mirror_name) | |
|
33 | except ImportError: | |
|
34 | return | |
|
35 | else: | |
|
36 | if not isinstance(mod, types.ModuleType): | |
|
37 | # not a module | |
|
38 | return None | |
|
39 | return self | |
|
40 | ||
|
41 | def load_module(self, fullname): | |
|
42 | """Import the mirrored module, and insert it into sys.modules""" | |
|
43 | mirror_name = self._mirror_name(fullname) | |
|
44 | mod = import_item(mirror_name) | |
|
45 | sys.modules[fullname] = mod | |
|
46 | return mod | |
|
47 | ||
|
48 | ||
|
8 | 49 | class ShimModule(types.ModuleType): |
|
9 | 50 | |
|
10 | 51 | def __init__(self, *args, **kwargs): |
|
11 | 52 | self._mirror = kwargs.pop("mirror") |
|
53 | src = kwargs.pop("src", None) | |
|
54 | if src: | |
|
55 | kwargs['name'] = src.rsplit('.', 1)[-1] | |
|
12 | 56 | super(ShimModule, self).__init__(*args, **kwargs) |
|
57 | # add import hook for descendent modules | |
|
58 | if src: | |
|
59 | sys.meta_path.append( | |
|
60 | ShimImporter(src=src, mirror=self._mirror) | |
|
61 | ) | |
|
62 | ||
|
63 | @property | |
|
64 | def __path__(self): | |
|
65 | return [] | |
|
13 | 66 | |
|
14 | 67 | @property |
|
15 | 68 | def __spec__(self): |
@@ -19,26 +72,7 b' class ShimModule(types.ModuleType):' | |||
|
19 | 72 | def __getattr__(self, key): |
|
20 | 73 | # Use the equivalent of import_item(name), see below |
|
21 | 74 | name = "%s.%s" % (self._mirror, key) |
|
22 | ||
|
23 | # NOTE: the code below was copied *verbatim* from | |
|
24 | # importstring.import_item. For some very strange reason that makes no | |
|
25 | # sense to me, if we call it *as a function*, it doesn't work. This | |
|
26 | # has something to do with the deep bowels of the import machinery and | |
|
27 | # I couldn't find a way to make the code work as a standard function | |
|
28 | # call. But at least since it's an unmodified copy of import_item, | |
|
29 | # which is used extensively and has a test suite, we can be reasonably | |
|
30 | # confident this is OK. If anyone finds how to call the function, all | |
|
31 | # the below could be replaced simply with: | |
|
32 | # | |
|
33 | # from IPython.utils.importstring import import_item | |
|
34 | # return import_item('MIRROR.' + key) | |
|
35 | ||
|
36 | parts = name.rsplit('.', 1) | |
|
37 | if len(parts) == 2: | |
|
38 | # called with 'foo.bar....' | |
|
39 | package, obj = parts | |
|
40 | module = __import__(package, fromlist=[obj]) | |
|
41 | return getattr(module, obj) | |
|
42 | else: | |
|
43 | # called with un-dotted string | |
|
44 | return __import__(parts[0]) | |
|
75 | try: | |
|
76 | return import_item(name) | |
|
77 | except ImportError: | |
|
78 | raise AttributeError(key) |
General Comments 0
You need to be logged in to leave comments.
Login now