##// END OF EJS Templates
add import hook for shim packages...
Min RK -
Show More
@@ -12,11 +12,7 b' working, though a warning will be printed.'
12 # Copyright (c) IPython Development Team.
12 # Copyright (c) IPython Development Team.
13 # Distributed under the terms of the Modified BSD License.
13 # Distributed under the terms of the Modified BSD License.
14
14
15 from __future__ import print_function
16
17 # Stdlib
18 import sys
15 import sys
19 import types
20 from warnings import warn
16 from warnings import warn
21
17
22 warn("The top-level `frontend` package has been deprecated. "
18 warn("The top-level `frontend` package has been deprecated. "
@@ -27,5 +23,7 b' from IPython.utils.shimmodule import ShimModule'
27 # Unconditionally insert the shim into sys.modules so that further import calls
23 # Unconditionally insert the shim into sys.modules so that further import calls
28 # trigger the custom attribute access above
24 # trigger the custom attribute access above
29
25
30 sys.modules['IPython.frontend.html.notebook'] = ShimModule('notebook', mirror='IPython.html')
26 sys.modules['IPython.frontend.html.notebook'] = ShimModule(
31 sys.modules['IPython.frontend'] = ShimModule('frontend', mirror='IPython')
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 from IPython.utils.shimmodule import ShimModule
14 from IPython.utils.shimmodule import ShimModule
15
15
16 # zmq subdir is gone
16 # zmq subdir is gone
17 sys.modules['IPython.kernel.zmq.session'] = ShimModule('session', mirror='jupyter_client.session')
17 sys.modules['IPython.kernel.zmq.session'] = ShimModule(
18 sys.modules['IPython.kernel.zmq'] = ShimModule('zmq', mirror='ipython_kernel')
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 for pkg in ('comm', 'inprocess'):
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 for pkg in ('ioloop', 'blocking'):
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 # required for `from IPython.kernel import PKG`
30 # required for `from IPython.kernel import PKG`
27 from ipython_kernel import comm, inprocess
31 from ipython_kernel import comm, inprocess
@@ -4,8 +4,6 b' Shim to maintain backwards compatibility with old IPython.nbconvert imports.'
4 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 from __future__ import print_function
8
9 import sys
7 import sys
10 from warnings import warn
8 from warnings import warn
11
9
@@ -17,4 +15,5 b' from IPython.utils.shimmodule import ShimModule'
17 # Unconditionally insert the shim into sys.modules so that further import calls
15 # Unconditionally insert the shim into sys.modules so that further import calls
18 # trigger the custom attribute access above
16 # trigger the custom attribute access above
19
17
20 sys.modules['IPython.nbconvert'] = ShimModule('nbconvert', mirror='jupyter_nbconvert')
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 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 from __future__ import print_function
8
9 # Stdlib
10 import sys
7 import sys
11 import types
12 from warnings import warn
8 from warnings import warn
13
9
14 warn("The `IPython.nbformat` package has been deprecated. "
10 warn("The `IPython.nbformat` package has been deprecated. "
@@ -19,4 +15,5 b' from IPython.utils.shimmodule import ShimModule'
19 # Unconditionally insert the shim into sys.modules so that further import calls
15 # Unconditionally insert the shim into sys.modules so that further import calls
20 # trigger the custom attribute access above
16 # trigger the custom attribute access above
21
17
22 sys.modules['IPython.nbformat'] = ShimModule('nbformat', mirror='jupyter_nbformat')
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 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 from __future__ import print_function
8
9 import sys
7 import sys
10 from warnings import warn
8 from warnings import warn
11
9
@@ -17,4 +15,6 b' from IPython.utils.shimmodule import ShimModule'
17 # Unconditionally insert the shim into sys.modules so that further import calls
15 # Unconditionally insert the shim into sys.modules so that further import calls
18 # trigger the custom attribute access above
16 # trigger the custom attribute access above
19
17
20 sys.modules['IPython.parallel'] = ShimModule('parallel', mirror='ipython_parallel')
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 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 from __future__ import print_function
8
9 # Stdlib
10 import sys
7 import sys
11 import types
12 from warnings import warn
8 from warnings import warn
13
9
14 warn("The `IPython.qt` package has been deprecated. "
10 warn("The `IPython.qt` package has been deprecated. "
@@ -19,4 +15,4 b' from IPython.utils.shimmodule import ShimModule'
19 # Unconditionally insert the shim into sys.modules so that further import calls
15 # Unconditionally insert the shim into sys.modules so that further import calls
20 # trigger the custom attribute access above
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 # Unconditionally insert the shim into sys.modules so that further import calls
17 # Unconditionally insert the shim into sys.modules so that further import calls
18 # trigger the custom attribute access above
18 # trigger the custom attribute access above
19
19
20 sys.modules['IPython.terminal.console'] = ShimModule('console', mirror='jupyter_console')
20 sys.modules['IPython.terminal.console'] = ShimModule(
21 src='IPython.terminal.console', mirror='jupyter_console')
@@ -3,14 +3,65 b''
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 sys
6 import types
7 import types
7
8
9
10 class ShimImporter(object):
11 """Import hook for a shim.
12
13 This ensures that submodule imports return the real target module,
14 not a clone that will confuse `is` and `isinstance` checks.
15 """
16 def __init__(self, src, mirror):
17 self.src = src
18 self.mirror = mirror
19
20 def _mirror_name(self, fullname):
21 """get the name of the mirrored module"""
22
23 return self.mirror + fullname[len(self.src):]
24
25 def find_module(self, fullname, path=None):
26 """Return self if we should be used to import the module."""
27 if fullname.startswith(self.src + '.'):
28 mirror_name = self._mirror_name(fullname)
29 try:
30 __import__(mirror_name)
31 except ImportError:
32 return
33 else:
34 return self
35
36 def load_module(self, fullname):
37 """Import the mirrored module, and insert it into sys.modules"""
38 mirror_name = self._mirror_name(fullname)
39 mod = __import__(mirror_name)
40 if '.' in mirror_name:
41 name = mirror_name.rsplit('.', 1)[-1]
42 mod = getattr(mod, name)
43 sys.modules[fullname] = mod
44 return mod
45
46
8 class ShimModule(types.ModuleType):
47 class ShimModule(types.ModuleType):
9
48
10 def __init__(self, *args, **kwargs):
49 def __init__(self, *args, **kwargs):
11 self._mirror = kwargs.pop("mirror")
50 self._mirror = kwargs.pop("mirror")
51 src = kwargs.pop("src", None)
52 if src:
53 kwargs['name'] = src.rsplit('.', 1)[-1]
12 super(ShimModule, self).__init__(*args, **kwargs)
54 super(ShimModule, self).__init__(*args, **kwargs)
13
55 # add import hook for descendent modules
56 if src:
57 sys.meta_path.append(
58 ShimImporter(src=src, mirror=self._mirror)
59 )
60
61 @property
62 def __path__(self):
63 return []
64
14 @property
65 @property
15 def __spec__(self):
66 def __spec__(self):
16 """Don't produce __spec__ until requested"""
67 """Don't produce __spec__ until requested"""
General Comments 0
You need to be logged in to leave comments. Login now