##// END OF EJS Templates
ENH: extensions: port autoreload to current API
Pauli Virtanen -
Show More
@@ -219,39 +219,39 b' def superreload(module, reload=reload, old_objects={}):'
219
219
220 return module
220 return module
221
221
222 reloader = ModuleReloader()
223
224 #------------------------------------------------------------------------------
222 #------------------------------------------------------------------------------
225 # IPython connectivity
223 # IPython connectivity
226 #------------------------------------------------------------------------------
224 #------------------------------------------------------------------------------
227 from IPython.core import ipapi
228 from IPython.core.error import TryNext
229
225
230 ip = ipapi.get()
226 from IPython.core.plugin import Plugin
227 from IPython.core.hooks import TryNext
228
229 class Autoreload(Plugin):
230 def __init__(self, shell=None, config=None):
231 super(Autoreload, self).__init__(shell=shell, config=config)
232
233 self.shell.define_magic('autoreload', self.magic_autoreload)
234 self.shell.define_magic('aimport', self.magic_aimport)
235 self.shell.set_hook('pre_run_code_hook', self.pre_run_code_hook)
231
236
232 autoreload_enabled = False
237 self._enabled = False
238 self._reloader = ModuleReloader()
239 self._reloader.check_all = False
233
240
234 def runcode_hook(self):
241 def pre_run_code_hook(self, ipself):
235 if not autoreload_enabled:
242 if not self._enabled:
236 raise TryNext
243 raise TryNext
237 try:
244 try:
238 reloader.check()
245 self._reloader.check()
239 except:
246 except:
240 pass
247 pass
241
248
242 def enable_autoreload():
249 def magic_autoreload(self, ipself, parameter_s=''):
243 global autoreload_enabled
244 autoreload_enabled = True
245
246 def disable_autoreload():
247 global autoreload_enabled
248 autoreload_enabled = False
249
250 def autoreload_f(self, parameter_s=''):
251 r""" %autoreload => Reload modules automatically
250 r"""%autoreload => Reload modules automatically
252
251
253 %autoreload
252 %autoreload
254 Reload all modules (except those excluded by %aimport) automatically now.
253 Reload all modules (except those excluded by %aimport) automatically
254 now.
255
255
256 %autoreload 0
256 %autoreload 0
257 Disable automatic reloading.
257 Disable automatic reloading.
@@ -291,17 +291,17 b" def autoreload_f(self, parameter_s=''):"
291
291
292 """
292 """
293 if parameter_s == '':
293 if parameter_s == '':
294 reloader.check(True)
294 self._reloader.check(True)
295 elif parameter_s == '0':
295 elif parameter_s == '0':
296 disable_autoreload()
296 self._enabled = False
297 elif parameter_s == '1':
297 elif parameter_s == '1':
298 reloader.check_all = False
298 self._reloader.check_all = False
299 enable_autoreload()
299 self._enabled = True
300 elif parameter_s == '2':
300 elif parameter_s == '2':
301 reloader.check_all = True
301 self._reloader.check_all = True
302 enable_autoreload()
302 self._enabled = True
303
303
304 def aimport_f(self, parameter_s=''):
304 def magic_aimport(self, ipself, parameter_s=''):
305 """%aimport => Import modules for automatic reloading.
305 """%aimport => Import modules for automatic reloading.
306
306
307 %aimport
307 %aimport
@@ -317,34 +317,42 b" def aimport_f(self, parameter_s=''):"
317
317
318 modname = parameter_s
318 modname = parameter_s
319 if not modname:
319 if not modname:
320 to_reload = reloader.modules.keys()
320 to_reload = self._reloader.modules.keys()
321 to_reload.sort()
321 to_reload.sort()
322 to_skip = reloader.skip_modules.keys()
322 to_skip = self._reloader.skip_modules.keys()
323 to_skip.sort()
323 to_skip.sort()
324 if reloader.check_all:
324 if self._reloader.check_all:
325 print "Modules to reload:\nall-expect-skipped"
325 print "Modules to reload:\nall-expect-skipped"
326 else:
326 else:
327 print "Modules to reload:\n%s" % ' '.join(to_reload)
327 print "Modules to reload:\n%s" % ' '.join(to_reload)
328 print "\nModules to skip:\n%s" % ' '.join(to_skip)
328 print "\nModules to skip:\n%s" % ' '.join(to_skip)
329 elif modname.startswith('-'):
329 elif modname.startswith('-'):
330 modname = modname[1:]
330 modname = modname[1:]
331 try: del reloader.modules[modname]
331 try:
332 except KeyError: pass
332 del self._reloader.modules[modname]
333 reloader.skip_modules[modname] = True
333 except KeyError:
334 pass
335 self._reloader.skip_modules[modname] = True
334 else:
336 else:
335 try: del reloader.skip_modules[modname]
337 try:
336 except KeyError: pass
338 del self._reloader.skip_modules[modname]
337 reloader.modules[modname] = True
339 except KeyError:
340 pass
341 self._reloader.modules[modname] = True
338
342
339 # Inject module to user namespace; handle also submodules properly
343 # Inject module to user namespace; handle also submodules properly
340 __import__(modname)
344 __import__(modname)
341 basename = modname.split('.')[0]
345 basename = modname.split('.')[0]
342 mod = sys.modules[basename]
346 mod = sys.modules[basename]
343 ip.push({basename: mod})
347 ipself.push({basename: mod})
348
344
349
345 def init():
350 _loaded = False
346 ip.define_magic('autoreload', autoreload_f)
347 ip.define_magic('aimport', aimport_f)
348 ip.set_hook('pre_runcode_hook', runcode_hook)
349
351
350 init()
352 def load_ipython_extension(ip):
353 """Load the extension in IPython."""
354 global _loaded
355 if not _loaded:
356 plugin = Autoreload(shell=ip, config=ip.config)
357 ip.plugin_manager.register_plugin('autoreload', plugin)
358 _loaded = True
General Comments 0
You need to be logged in to leave comments. Login now