##// END OF EJS Templates
Extension manager refuses to load extension more than once
Thomas Kluyver -
Show More
@@ -23,6 +23,7 b' import sys'
23 from urllib import urlretrieve
23 from urllib import urlretrieve
24 from urlparse import urlparse
24 from urlparse import urlparse
25
25
26 from IPython.core.error import UsageError
26 from IPython.config.configurable import Configurable
27 from IPython.config.configurable import Configurable
27 from IPython.utils.traitlets import Instance
28 from IPython.utils.traitlets import Instance
28
29
@@ -44,10 +45,11 b' class ExtensionManager(Configurable):'
44 the only argument. You can do anything you want with IPython at
45 the only argument. You can do anything you want with IPython at
45 that point, including defining new magic and aliases, adding new
46 that point, including defining new magic and aliases, adding new
46 components, etc.
47 components, etc.
47
48
48 The :func:`load_ipython_extension` will be called again is you
49 You can also optionaly define an :func:`unload_ipython_extension(ipython)`
49 load or reload the extension again. It is up to the extension
50 function, which will be called if the user unloads or reloads the extension.
50 author to add code to manage that.
51 The extension manager will only call :func:`load_ipython_extension` again
52 if the extension is reloaded.
51
53
52 You can put your extension modules anywhere you want, as long as
54 You can put your extension modules anywhere you want, as long as
53 they can be imported by Python's standard import mechanism. However,
55 they can be imported by Python's standard import mechanism. However,
@@ -81,9 +83,12 b' class ExtensionManager(Configurable):'
81 def load_extension(self, module_str):
83 def load_extension(self, module_str):
82 """Load an IPython extension by its module name.
84 """Load an IPython extension by its module name.
83
85
84 If :func:`load_ipython_extension` returns anything, this function
86 Returns the string "already loaded" if the extension is already loaded,
85 will return that object.
87 otherwise None.
86 """
88 """
89 if module_str in self.loaded:
90 return "already loaded"
91
87 from IPython.utils.syspathcontext import prepended_to_syspath
92 from IPython.utils.syspathcontext import prepended_to_syspath
88
93
89 if module_str not in sys.modules:
94 if module_str not in sys.modules:
@@ -98,11 +103,20 b' class ExtensionManager(Configurable):'
98
103
99 This function looks up the extension's name in ``sys.modules`` and
104 This function looks up the extension's name in ``sys.modules`` and
100 simply calls ``mod.unload_ipython_extension(self)``.
105 simply calls ``mod.unload_ipython_extension(self)``.
106
107 Returns the string "no unload function" if the extension doesn't define
108 a function to unload itself, "not loaded" if the extension isn't loaded,
109 otherwise None.
101 """
110 """
111 if module_str not in self.loaded:
112 return "not loaded"
113
102 if module_str in sys.modules:
114 if module_str in sys.modules:
103 mod = sys.modules[module_str]
115 mod = sys.modules[module_str]
104 if self._call_unload_ipython_extension(mod):
116 if self._call_unload_ipython_extension(mod):
105 self.loaded.discard(module_str)
117 self.loaded.discard(module_str)
118 else:
119 return "no unload function"
106
120
107 def reload_extension(self, module_str):
121 def reload_extension(self, module_str):
108 """Reload an IPython extension by calling reload.
122 """Reload an IPython extension by calling reload.
@@ -59,14 +59,26 b' class ExtensionMagics(Magics):'
59 """Load an IPython extension by its module name."""
59 """Load an IPython extension by its module name."""
60 if not module_str:
60 if not module_str:
61 raise UsageError('Missing module name.')
61 raise UsageError('Missing module name.')
62 self.shell.extension_manager.load_extension(module_str)
62 if self.shell.extension_manager.load_extension(module_str) == 'already loaded':
63 print "The %s extension is already loaded. To reload it, use:" % module_str
64 print " %reload_ext", module_str
63
65
64 @line_magic
66 @line_magic
65 def unload_ext(self, module_str):
67 def unload_ext(self, module_str):
66 """Unload an IPython extension by its module name."""
68 """Unload an IPython extension by its module name.
69
70 Not all extensions can be unloaded, only those which define an
71 ``unload_ipython_extension`` function.
72 """
67 if not module_str:
73 if not module_str:
68 raise UsageError('Missing module name.')
74 raise UsageError('Missing module name.')
69 self.shell.extension_manager.unload_extension(module_str)
75
76 res = self.shell.extension_manager.unload_extension(module_str)
77
78 if res == 'no unload function':
79 print "The %s extension doesn't define how to unload it." % module_str
80 elif res == "not loaded":
81 print "The %s extension is not loaded." % module_str
70
82
71 @line_magic
83 @line_magic
72 def reload_ext(self, module_str):
84 def reload_ext(self, module_str):
General Comments 0
You need to be logged in to leave comments. Login now