##// END OF EJS Templates
lint
Paul Ivanov -
r28598:c55d9f37 remove-more-ipyth...
parent child Browse files
Show More
@@ -1,135 +1,135 b''
1 1 # encoding: utf-8
2 2 """A class for managing IPython extensions."""
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 os
8 8 import os.path
9 9 import sys
10 10 from importlib import import_module, reload
11 11
12 12 from traitlets.config.configurable import Configurable
13 13 from IPython.utils.path import ensure_dir_exists
14 14 from traitlets import Instance
15 15
16 16
17 17 #-----------------------------------------------------------------------------
18 18 # Main class
19 19 #-----------------------------------------------------------------------------
20 20
21 21 BUILTINS_EXTS = {"storemagic": False, "autoreload": False}
22 22
23 23
24 24 class ExtensionManager(Configurable):
25 25 """A class to manage IPython extensions.
26 26
27 27 An IPython extension is an importable Python module that has
28 28 a function with the signature::
29 29
30 30 def load_ipython_extension(ipython):
31 31 # Do things with ipython
32 32
33 33 This function is called after your extension is imported and the
34 34 currently active :class:`InteractiveShell` instance is passed as
35 35 the only argument. You can do anything you want with IPython at
36 36 that point, including defining new magic and aliases, adding new
37 37 components, etc.
38
38
39 39 You can also optionally define an :func:`unload_ipython_extension(ipython)`
40 40 function, which will be called if the user unloads or reloads the extension.
41 41 The extension manager will only call :func:`load_ipython_extension` again
42 42 if the extension is reloaded.
43 43
44 44 You can put your extension modules anywhere you want, as long as
45 45 they can be imported by Python's standard import mechanism.
46 46 """
47 47
48 48 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
49 49
50 50 def __init__(self, shell=None, **kwargs):
51 51 super(ExtensionManager, self).__init__(shell=shell, **kwargs)
52 52 self.loaded = set()
53 53
54 54 def load_extension(self, module_str: str):
55 55 """Load an IPython extension by its module name.
56 56
57 57 Returns the string "already loaded" if the extension is already loaded,
58 58 "no load function" if the module doesn't have a load_ipython_extension
59 59 function, or None if it succeeded.
60 60 """
61 61 try:
62 62 return self._load_extension(module_str)
63 63 except ModuleNotFoundError:
64 64 if module_str in BUILTINS_EXTS:
65 65 BUILTINS_EXTS[module_str] = True
66 66 return self._load_extension("IPython.extensions." + module_str)
67 67 raise
68 68
69 69 def _load_extension(self, module_str: str):
70 70 if module_str in self.loaded:
71 71 return "already loaded"
72 72
73 73 assert self.shell is not None
74 74
75 75 with self.shell.builtin_trap:
76 76 if module_str not in sys.modules:
77 77 mod = import_module(module_str)
78 78 mod = sys.modules[module_str]
79 79 if self._call_load_ipython_extension(mod):
80 80 self.loaded.add(module_str)
81 81 else:
82 82 return "no load function"
83 83
84 84 def unload_extension(self, module_str: str):
85 85 """Unload an IPython extension by its module name.
86 86
87 87 This function looks up the extension's name in ``sys.modules`` and
88 88 simply calls ``mod.unload_ipython_extension(self)``.
89 89
90 90 Returns the string "no unload function" if the extension doesn't define
91 91 a function to unload itself, "not loaded" if the extension isn't loaded,
92 92 otherwise None.
93 93 """
94 94 if BUILTINS_EXTS.get(module_str, False) is True:
95 95 module_str = "IPython.extensions." + module_str
96 96 if module_str not in self.loaded:
97 97 return "not loaded"
98 98
99 99 if module_str in sys.modules:
100 100 mod = sys.modules[module_str]
101 101 if self._call_unload_ipython_extension(mod):
102 102 self.loaded.discard(module_str)
103 103 else:
104 104 return "no unload function"
105 105
106 106 def reload_extension(self, module_str: str):
107 107 """Reload an IPython extension by calling reload.
108 108
109 109 If the module has not been loaded before,
110 110 :meth:`InteractiveShell.load_extension` is called. Otherwise
111 111 :func:`reload` is called and then the :func:`load_ipython_extension`
112 112 function of the module, if it exists is called.
113 113 """
114 114
115 115 if BUILTINS_EXTS.get(module_str, False) is True:
116 116 module_str = "IPython.extensions." + module_str
117 117
118 118 if (module_str in self.loaded) and (module_str in sys.modules):
119 119 self.unload_extension(module_str)
120 120 mod = sys.modules[module_str]
121 121 reload(mod)
122 122 if self._call_load_ipython_extension(mod):
123 123 self.loaded.add(module_str)
124 124 else:
125 125 self.load_extension(module_str)
126 126
127 127 def _call_load_ipython_extension(self, mod):
128 128 if hasattr(mod, 'load_ipython_extension'):
129 129 mod.load_ipython_extension(self.shell)
130 130 return True
131 131
132 132 def _call_unload_ipython_extension(self, mod):
133 133 if hasattr(mod, 'unload_ipython_extension'):
134 134 mod.unload_ipython_extension(self.shell)
135 135 return True
General Comments 0
You need to be logged in to leave comments. Login now