##// END OF EJS Templates
add ShimWarning for shimmed imports...
Min RK -
Show More
@@ -0,0 +1,15 b''
1 import sys
2 import warnings
3
4 import nose.tools as nt
5
6 from IPython.utils.capture import capture_output
7 from IPython.utils.shimmodule import ShimWarning
8
9 def test_shim_warning():
10 sys.modules.pop('IPython.config', None)
11 with warnings.catch_warnings(record=True) as w:
12 warnings.simplefilter("always")
13 import IPython.config
14 assert len(w) == 1
15 assert issubclass(w[-1].category, ShimWarning)
@@ -1,18 +1,19 b''
1 1 """
2 2 Shim to maintain backwards compatibility with old IPython.config imports.
3 3 """
4 4 # Copyright (c) IPython Development Team.
5 5 # Distributed under the terms of the Modified BSD License.
6 6
7 7 import sys
8 8 from warnings import warn
9 9
10 from IPython.utils.shimmodule import ShimModule, ShimWarning
11
10 12 warn("The `IPython.config` package has been deprecated. "
11 "You should import from traitlets.config instead.")
13 "You should import from traitlets.config instead.", ShimWarning)
12 14
13 from IPython.utils.shimmodule import ShimModule
14 15
15 16 # Unconditionally insert the shim into sys.modules so that further import calls
16 17 # trigger the custom attribute access above
17 18
18 19 sys.modules['IPython.config'] = ShimModule(src='IPython.config', mirror='traitlets.config')
@@ -1,29 +1,29 b''
1 1 """
2 2 Shim to maintain backwards compatibility with old frontend imports.
3 3
4 4 We have moved all contents of the old `frontend` subpackage into top-level
5 5 subpackages (`html`, `qt` and `terminal`), and flattened the notebook into
6 6 just `IPython.html`, formerly `IPython.frontend.html.notebook`.
7 7
8 8 This will let code that was making `from IPython.frontend...` calls continue
9 9 working, though a warning will be printed.
10 10 """
11 11
12 12 # Copyright (c) IPython Development Team.
13 13 # Distributed under the terms of the Modified BSD License.
14 14
15 15 import sys
16 16 from warnings import warn
17 17
18 warn("The top-level `frontend` package has been deprecated. "
19 "All its subpackages have been moved to the top `IPython` level.")
18 from IPython.utils.shimmodule import ShimModule, ShimWarning
20 19
21 from IPython.utils.shimmodule import ShimModule
20 warn("The top-level `frontend` package has been deprecated. "
21 "All its subpackages have been moved to the top `IPython` level.", ShimWarning)
22 22
23 23 # Unconditionally insert the shim into sys.modules so that further import calls
24 24 # trigger the custom attribute access above
25 25
26 26 sys.modules['IPython.frontend.html.notebook'] = ShimModule(
27 27 src='IPython.frontend.html.notebook', mirror='IPython.html')
28 28 sys.modules['IPython.frontend'] = ShimModule(
29 29 src='IPython.frontend', mirror='IPython')
@@ -1,28 +1,28 b''
1 1 """
2 2 Shim to maintain backwards compatibility with old IPython.html imports.
3 3 """
4 4 # Copyright (c) IPython Development Team.
5 5 # Distributed under the terms of the Modified BSD License.
6 6
7 7 import sys
8 8 from warnings import warn
9 9
10 from IPython.utils.shimmodule import ShimModule, ShimWarning
11
10 12 warn("The `IPython.html` package has been deprecated. "
11 13 "You should import from `notebook` instead. "
12 "`IPython.html.widgets` has moved to `ipywidgets`.")
13
14 from IPython.utils.shimmodule import ShimModule
14 "`IPython.html.widgets` has moved to `ipywidgets`.", ShimWarning)
15 15
16 16 _widgets = sys.modules['IPython.html.widgets'] = ShimModule(
17 17 src='IPython.html.widgets', mirror='ipywidgets')
18 18
19 19 _html = ShimModule(
20 20 src='IPython.html', mirror='notebook')
21 21
22 22 # hook up widgets
23 23 _html.widgets = _widgets
24 24 sys.modules['IPython.html'] = _html
25 25
26 26 if __name__ == '__main__':
27 27 from notebook import notebookapp as app
28 28 app.launch_new_instance()
@@ -1,35 +1,35 b''
1 1 """
2 2 Shim to maintain backwards compatibility with old IPython.kernel imports.
3 3 """
4 4 # Copyright (c) IPython Development Team.
5 5 # Distributed under the terms of the Modified BSD License.
6 6
7 7 import sys
8 8 from warnings import warn
9 9
10 warn("The `IPython.kernel` package has been deprecated. "
11 "You should import from ipykernel or jupyter_client instead.")
10 from IPython.utils.shimmodule import ShimModule, ShimWarning
12 11
12 warn("The `IPython.kernel` package has been deprecated. "
13 "You should import from ipykernel or jupyter_client instead.", ShimWarning)
13 14
14 from IPython.utils.shimmodule import ShimModule
15 15
16 16 # zmq subdir is gone
17 17 sys.modules['IPython.kernel.zmq.session'] = ShimModule(
18 18 src='IPython.kernel.zmq.session', mirror='jupyter_client.session')
19 19 sys.modules['IPython.kernel.zmq'] = ShimModule(
20 20 src='IPython.kernel.zmq', mirror='ipykernel')
21 21
22 22 for pkg in ('comm', 'inprocess'):
23 23 src = 'IPython.kernel.%s' % pkg
24 24 sys.modules[src] = ShimModule(src=src, mirror='ipykernel.%s' % pkg)
25 25
26 26 for pkg in ('ioloop', 'blocking'):
27 27 src = 'IPython.kernel.%s' % pkg
28 28 sys.modules[src] = ShimModule(src=src, mirror='jupyter_client.%s' % pkg)
29 29
30 30 # required for `from IPython.kernel import PKG`
31 31 from ipykernel import comm, inprocess
32 32 from jupyter_client import ioloop, blocking
33 33 # public API
34 34 from ipykernel.connect import *
35 35 from jupyter_client import *
@@ -1,19 +1,19 b''
1 1 """
2 2 Shim to maintain backwards compatibility with old IPython.nbconvert imports.
3 3 """
4 4 # Copyright (c) IPython Development Team.
5 5 # Distributed under the terms of the Modified BSD License.
6 6
7 7 import sys
8 8 from warnings import warn
9 9
10 warn("The `IPython.nbconvert` package has been deprecated. "
11 "You should import from ipython_nbconvert instead.")
10 from IPython.utils.shimmodule import ShimModule, ShimWarning
12 11
13 from IPython.utils.shimmodule import ShimModule
12 warn("The `IPython.nbconvert` package has been deprecated. "
13 "You should import from ipython_nbconvert instead.", ShimWarning)
14 14
15 15 # Unconditionally insert the shim into sys.modules so that further import calls
16 16 # trigger the custom attribute access above
17 17
18 18 sys.modules['IPython.nbconvert'] = ShimModule(
19 19 src='IPython.nbconvert', mirror='nbconvert')
@@ -1,19 +1,19 b''
1 1 """
2 2 Shim to maintain backwards compatibility with old IPython.nbformat imports.
3 3 """
4 4 # Copyright (c) IPython Development Team.
5 5 # Distributed under the terms of the Modified BSD License.
6 6
7 7 import sys
8 8 from warnings import warn
9 9
10 warn("The `IPython.nbformat` package has been deprecated. "
11 "You should import from nbformat instead.")
10 from IPython.utils.shimmodule import ShimModule, ShimWarning
12 11
13 from IPython.utils.shimmodule import ShimModule
12 warn("The `IPython.nbformat` package has been deprecated. "
13 "You should import from nbformat instead.", ShimWarning)
14 14
15 15 # Unconditionally insert the shim into sys.modules so that further import calls
16 16 # trigger the custom attribute access above
17 17
18 18 sys.modules['IPython.nbformat'] = ShimModule(
19 19 src='IPython.nbformat', mirror='nbformat')
@@ -1,20 +1,20 b''
1 1 """
2 2 Shim to maintain backwards compatibility with old IPython.parallel imports.
3 3 """
4 4 # Copyright (c) IPython Development Team.
5 5 # Distributed under the terms of the Modified BSD License.
6 6
7 7 import sys
8 8 from warnings import warn
9 9
10 warn("The `IPython.parallel` package has been deprecated. "
11 "You should import from ipyparallel instead.")
10 from IPython.utils.shimmodule import ShimModule, ShimWarning
12 11
13 from IPython.utils.shimmodule import ShimModule
12 warn("The `IPython.parallel` package has been deprecated. "
13 "You should import from ipyparallel instead.", ShimWarning)
14 14
15 15 # Unconditionally insert the shim into sys.modules so that further import calls
16 16 # trigger the custom attribute access above
17 17
18 18 sys.modules['IPython.parallel'] = ShimModule(
19 19 src='IPython.parallel', mirror='ipyparallel')
20 20
@@ -1,24 +1,24 b''
1 1 """
2 2 Shim to maintain backwards compatibility with old IPython.qt imports.
3 3 """
4 4 # Copyright (c) IPython Development Team.
5 5 # Distributed under the terms of the Modified BSD License.
6 6
7 7 import sys
8 8 from warnings import warn
9 9
10 warn("The `IPython.qt` package has been deprecated. "
11 "You should import from qtconsole instead.")
10 from IPython.utils.shimmodule import ShimModule, ShimWarning
12 11
13 from IPython.utils.shimmodule import ShimModule
12 warn("The `IPython.qt` package has been deprecated. "
13 "You should import from qtconsole instead.", ShimWarning)
14 14
15 15 # Unconditionally insert the shim into sys.modules so that further import calls
16 16 # trigger the custom attribute access above
17 17
18 18 _console = sys.modules['IPython.qt.console'] = ShimModule(
19 19 src='IPython.qt.console', mirror='qtconsole')
20 20
21 21 _qt = ShimModule(src='IPython.qt', mirror='qtconsole')
22 22
23 23 _qt.console = _console
24 24 sys.modules['IPython.qt'] = _qt
@@ -1,21 +1,19 b''
1 1 """
2 2 Shim to maintain backwards compatibility with old IPython.terminal.console imports.
3 3 """
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
12 warn("The `IPython.terminal.console` package has been deprecated. "
13 "You should import from jupyter_console instead.")
10 from IPython.utils.shimmodule import ShimModule, ShimWarning
14 11
15 from IPython.utils.shimmodule import ShimModule
12 warn("The `IPython.terminal.console` package has been deprecated. "
13 "You should import from jupyter_console instead.", ShimWarning)
16 14
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 18 sys.modules['IPython.terminal.console'] = ShimModule(
21 19 src='IPython.terminal.console', mirror='jupyter_console')
@@ -1,90 +1,92 b''
1 1 """A shim module for deprecated imports
2 2 """
3 3 # Copyright (c) IPython Development Team.
4 4 # Distributed under the terms of the Modified BSD License.
5 5
6 6 import sys
7 7 import types
8 8
9 9 from .importstring import import_item
10 10
11 class ShimWarning(Warning):
12 """A warning to show when a module has moved, and a shim is in its place."""
11 13
12 14 class ShimImporter(object):
13 15 """Import hook for a shim.
14 16
15 17 This ensures that submodule imports return the real target module,
16 18 not a clone that will confuse `is` and `isinstance` checks.
17 19 """
18 20 def __init__(self, src, mirror):
19 21 self.src = src
20 22 self.mirror = mirror
21 23
22 24 def _mirror_name(self, fullname):
23 25 """get the name of the mirrored module"""
24 26
25 27 return self.mirror + fullname[len(self.src):]
26 28
27 29 def find_module(self, fullname, path=None):
28 30 """Return self if we should be used to import the module."""
29 31 if fullname.startswith(self.src + '.'):
30 32 mirror_name = self._mirror_name(fullname)
31 33 try:
32 34 mod = import_item(mirror_name)
33 35 except ImportError:
34 36 return
35 37 else:
36 38 if not isinstance(mod, types.ModuleType):
37 39 # not a module
38 40 return None
39 41 return self
40 42
41 43 def load_module(self, fullname):
42 44 """Import the mirrored module, and insert it into sys.modules"""
43 45 mirror_name = self._mirror_name(fullname)
44 46 mod = import_item(mirror_name)
45 47 sys.modules[fullname] = mod
46 48 return mod
47 49
48 50
49 51 class ShimModule(types.ModuleType):
50 52
51 53 def __init__(self, *args, **kwargs):
52 54 self._mirror = kwargs.pop("mirror")
53 55 src = kwargs.pop("src", None)
54 56 if src:
55 57 kwargs['name'] = src.rsplit('.', 1)[-1]
56 58 super(ShimModule, self).__init__(*args, **kwargs)
57 59 # add import hook for descendent modules
58 60 if src:
59 61 sys.meta_path.append(
60 62 ShimImporter(src=src, mirror=self._mirror)
61 63 )
62 64
63 65 @property
64 66 def __path__(self):
65 67 return []
66 68
67 69 @property
68 70 def __spec__(self):
69 71 """Don't produce __spec__ until requested"""
70 72 return __import__(self._mirror).__spec__
71 73
72 74 def __dir__(self):
73 75 return dir(__import__(self._mirror))
74 76
75 77 @property
76 78 def __all__(self):
77 79 """Ensure __all__ is always defined"""
78 80 mod = __import__(self._mirror)
79 81 try:
80 82 return mod.__all__
81 83 except AttributeError:
82 84 return [name for name in dir(mod) if not name.startswith('_')]
83 85
84 86 def __getattr__(self, key):
85 87 # Use the equivalent of import_item(name), see below
86 88 name = "%s.%s" % (self._mirror, key)
87 89 try:
88 90 return import_item(name)
89 91 except ImportError:
90 92 raise AttributeError(key)
General Comments 0
You need to be logged in to leave comments. Login now