##// 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 23 from urllib import urlretrieve
24 24 from urlparse import urlparse
25 25
26 from IPython.core.error import UsageError
26 27 from IPython.config.configurable import Configurable
27 28 from IPython.utils.traitlets import Instance
28 29
@@ -44,10 +45,11 b' class ExtensionManager(Configurable):'
44 45 the only argument. You can do anything you want with IPython at
45 46 that point, including defining new magic and aliases, adding new
46 47 components, etc.
47
48 The :func:`load_ipython_extension` will be called again is you
49 load or reload the extension again. It is up to the extension
50 author to add code to manage that.
48
49 You can also optionaly define an :func:`unload_ipython_extension(ipython)`
50 function, which will be called if the user unloads or reloads the extension.
51 The extension manager will only call :func:`load_ipython_extension` again
52 if the extension is reloaded.
51 53
52 54 You can put your extension modules anywhere you want, as long as
53 55 they can be imported by Python's standard import mechanism. However,
@@ -81,9 +83,12 b' class ExtensionManager(Configurable):'
81 83 def load_extension(self, module_str):
82 84 """Load an IPython extension by its module name.
83 85
84 If :func:`load_ipython_extension` returns anything, this function
85 will return that object.
86 Returns the string "already loaded" if the extension is already loaded,
87 otherwise None.
86 88 """
89 if module_str in self.loaded:
90 return "already loaded"
91
87 92 from IPython.utils.syspathcontext import prepended_to_syspath
88 93
89 94 if module_str not in sys.modules:
@@ -98,11 +103,20 b' class ExtensionManager(Configurable):'
98 103
99 104 This function looks up the extension's name in ``sys.modules`` and
100 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 114 if module_str in sys.modules:
103 115 mod = sys.modules[module_str]
104 116 if self._call_unload_ipython_extension(mod):
105 117 self.loaded.discard(module_str)
118 else:
119 return "no unload function"
106 120
107 121 def reload_extension(self, module_str):
108 122 """Reload an IPython extension by calling reload.
@@ -59,14 +59,26 b' class ExtensionMagics(Magics):'
59 59 """Load an IPython extension by its module name."""
60 60 if not module_str:
61 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 66 @line_magic
65 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 73 if not module_str:
68 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 83 @line_magic
72 84 def reload_ext(self, module_str):
General Comments 0
You need to be logged in to leave comments. Login now