From e8c0cba4ba672aea5ab335aeb686abce9c0fde6f 2012-05-26 03:19:59 From: Fernando Perez Date: 2012-05-26 03:19:59 Subject: [PATCH] Clean up magic registration api and make it public. --- diff --git a/IPython/core/history.py b/IPython/core/history.py index 5ca060f..bafcd73 100644 --- a/IPython/core/history.py +++ b/IPython/core/history.py @@ -985,10 +985,7 @@ class HistoryMagics(Magics): def init_ipython(ip): - ip.magics_manager.register(HistoryMagics) - #ip.define_magic('hist', HistoryMagics.history) - #ip.define_magic('recall', HistoryMagics.rep) - + ip.register_magics(HistoryMagics) # XXX - ipy_completers are in quarantine, need to be updated to new apis #import ipy_completers #ipy_completers.quick_completer('%hist' ,'-g -t -r -n') diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index e73c9bb..d9dc730 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -1999,7 +1999,11 @@ class InteractiveShell(SingletonConfigurable): user_magics=mf.UserMagics(self)) self.configurables.append(self.magics_manager) - self.magics_manager.register(mf.BasicMagics, mf.CodeMagics, + # Expose as public API new_magic and registere_magics + self.new_magic = self.magics_manager.new_magic + self.register_magics = self.magics_manager.register + + self.register_magics(mf.BasicMagics, mf.CodeMagics, mf.ConfigMagics, mf.NamespaceMagics, mf.ExecutionMagics, mf.AutoMagics, mf.OSMagics, mf.LoggingMagics, mf.ExtensionsMagics, mf.PylabMagics, mf.DeprecatedMagics) @@ -2053,22 +2057,13 @@ class InteractiveShell(SingletonConfigurable): def define_magic(self, magic_name, func): """Expose own function as magic function for ipython - - Example:: - - def foo_impl(self, parameter_s=''): - 'My very own magic!. (Use docstrings, IPython reads them).' - print 'Magic function. Passed parameter is between < >:' - print '<%s>' % parameter_s - print 'The self object is:', self - ip.define_magic('foo', foo_impl) + Note: this API is now deprecated. Instead, you should use + `get_ipython().new_magic`. """ - return self.magics_manager - im = types.MethodType(func, self._magic) - old = self.find_magic(magic_name) - setattr(self._magic, 'magic_' + magic_name, im) - return old + warn('Deprecated API, use get_ipython().new_magic: %s\n' % + magic_name) + return self.new_magic(func, magic_name) def find_line_magic(self, magic_name): """Find and return a line magic by name.""" diff --git a/IPython/core/magic.py b/IPython/core/magic.py index b9c6022..c5f24d3 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -89,6 +89,11 @@ def register_magics(cls): magics['cell'] = {} return cls +def _record_magic(dct, mtype, mname, func): + if mtype == 'line_cell': + dct['line'][mname] = dct['cell'][mname] = func + else: + dct[mtype][mname] = func def validate_type(magic_type): if magic_type not in magic_types: @@ -185,7 +190,7 @@ class MagicsManager(Configurable): for mtype in magic_types: self.magics[mtype].update(m.magics[mtype]) - def define_magic(self, magic_name, func, magic_type='line'): + def new_magic(self, func, magic_name=None, magic_type='line'): """Expose own function as magic function for ipython Example:: @@ -198,11 +203,11 @@ class MagicsManager(Configurable): ip.define_magic('foo', foo_impl) """ + # Create the new method in the user_magics and register it in the # global table - self.user_magics.new_magic(magic_name, func, magic_type) - self.magics[magic_type][magic_name] = \ - self.user_magics.magics[magic_type][magic_name] + newm, name = self.user_magics.new_magic(func, magic_type, magic_name) + _record_magic(self.magics, magic_type, name, newm) # Key base class that provides the central functionality for magics. @@ -362,10 +367,12 @@ class Magics(object): error("%s is not a magic function" % fn) self.options_table[fn] = optstr - def new_magic(self, magic_name, func, magic_type='line'): + def new_magic(self, func, magic_type='line', magic_name=None): """TODO """ + magic_name = func.func_name if magic_name is None else magic_name validate_type(magic_type) meth = types.MethodType(func, self) setattr(self, magic_name, meth) - self.magics[magic_type][magic_name] = meth + _record_magic(self.magics, magic_type, magic_name, meth) + return meth, magic_name