##// END OF EJS Templates
Merge pull request #8186 from minrk/shim-import-hook...
Thomas Kluyver -
r20994:01e03c55 merge
parent child Browse files
Show More
@@ -1,31 +1,29 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 # 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. "
23 "All its subpackages have been moved to the top `IPython` level.")
19 "All its subpackages have been moved to the top `IPython` level.")
24
20
25 from IPython.utils.shimmodule import ShimModule
21 from IPython.utils.shimmodule import ShimModule
26
22
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')
@@ -1,31 +1,35 b''
1 """
1 """
2 Shim to maintain backwards compatibility with old IPython.kernel imports.
2 Shim to maintain backwards compatibility with old IPython.kernel imports.
3 """
3 """
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 import sys
7 import sys
8 from warnings import warn
8 from warnings import warn
9
9
10 warn("The `IPython.kernel` package has been deprecated. "
10 warn("The `IPython.kernel` package has been deprecated. "
11 "You should import from ipython_kernel or jupyter_client instead.")
11 "You should import from ipython_kernel or jupyter_client instead.")
12
12
13
13
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
28 from jupyter_client import ioloop, blocking
32 from jupyter_client import ioloop, blocking
29 # public API
33 # public API
30 from ipython_kernel.connect import *
34 from ipython_kernel.connect import *
31 from jupyter_client import *
35 from jupyter_client import *
@@ -1,20 +1,19 b''
1 """
1 """
2 Shim to maintain backwards compatibility with old IPython.nbconvert imports.
2 Shim to maintain backwards compatibility with old IPython.nbconvert imports.
3 """
3 """
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
12 warn("The `IPython.nbconvert` package has been deprecated. "
10 warn("The `IPython.nbconvert` package has been deprecated. "
13 "You should import from ipython_nbconvert instead.")
11 "You should import from ipython_nbconvert instead.")
14
12
15 from IPython.utils.shimmodule import ShimModule
13 from IPython.utils.shimmodule import ShimModule
16
14
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')
@@ -1,22 +1,19 b''
1 """
1 """
2 Shim to maintain backwards compatibility with old IPython.nbformat imports.
2 Shim to maintain backwards compatibility with old IPython.nbformat imports.
3 """
3 """
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. "
15 "You should import from jupyter_nbformat instead.")
11 "You should import from jupyter_nbformat instead.")
16
12
17 from IPython.utils.shimmodule import ShimModule
13 from IPython.utils.shimmodule import ShimModule
18
14
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')
@@ -1,20 +1,20 b''
1 """
1 """
2 Shim to maintain backwards compatibility with old IPython.parallel imports.
2 Shim to maintain backwards compatibility with old IPython.parallel imports.
3 """
3 """
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
12 warn("The `IPython.parallel` package has been deprecated. "
10 warn("The `IPython.parallel` package has been deprecated. "
13 "You should import from ipython_parallel instead.")
11 "You should import from ipython_parallel instead.")
14
12
15 from IPython.utils.shimmodule import ShimModule
13 from IPython.utils.shimmodule import ShimModule
16
14
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
@@ -1,22 +1,18 b''
1 """
1 """
2 Shim to maintain backwards compatibility with old IPython.qt imports.
2 Shim to maintain backwards compatibility with old IPython.qt imports.
3 """
3 """
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. "
15 "You should import from jupyter_qtconsole instead.")
11 "You should import from jupyter_qtconsole instead.")
16
12
17 from IPython.utils.shimmodule import ShimModule
13 from IPython.utils.shimmodule import ShimModule
18
14
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')
@@ -1,20 +1,21 b''
1 """
1 """
2 Shim to maintain backwards compatibility with old IPython.terminal.console imports.
2 Shim to maintain backwards compatibility with old IPython.terminal.console imports.
3 """
3 """
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
7 from __future__ import print_function
8
8
9 import sys
9 import sys
10 from warnings import warn
10 from warnings import warn
11
11
12 warn("The `IPython.terminal.console` package has been deprecated. "
12 warn("The `IPython.terminal.console` package has been deprecated. "
13 "You should import from jupyter_console instead.")
13 "You should import from jupyter_console instead.")
14
14
15 from IPython.utils.shimmodule import ShimModule
15 from IPython.utils.shimmodule import ShimModule
16
16
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')
@@ -1,44 +1,78 b''
1 """A shim module for deprecated imports
1 """A shim module for deprecated imports
2 """
2 """
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 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 class ShimModule(types.ModuleType):
49 class ShimModule(types.ModuleType):
9
50
10 def __init__(self, *args, **kwargs):
51 def __init__(self, *args, **kwargs):
11 self._mirror = kwargs.pop("mirror")
52 self._mirror = kwargs.pop("mirror")
53 src = kwargs.pop("src", None)
54 if src:
55 kwargs['name'] = src.rsplit('.', 1)[-1]
12 super(ShimModule, self).__init__(*args, **kwargs)
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 @property
67 @property
15 def __spec__(self):
68 def __spec__(self):
16 """Don't produce __spec__ until requested"""
69 """Don't produce __spec__ until requested"""
17 return __import__(self._mirror).__spec__
70 return __import__(self._mirror).__spec__
18
71
19 def __getattr__(self, key):
72 def __getattr__(self, key):
20 # Use the equivalent of import_item(name), see below
73 # Use the equivalent of import_item(name), see below
21 name = "%s.%s" % (self._mirror, key)
74 name = "%s.%s" % (self._mirror, key)
22
75 try:
23 # NOTE: the code below was copied *verbatim* from
76 return import_item(name)
24 # importstring.import_item. For some very strange reason that makes no
77 except ImportError:
25 # sense to me, if we call it *as a function*, it doesn't work. This
78 raise AttributeError(key)
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])
General Comments 0
You need to be logged in to leave comments. Login now