##// END OF EJS Templates
Settle on cleaner API for magic registration....
Fernando Perez -
Show More
@@ -1999,9 +1999,10 b' class InteractiveShell(SingletonConfigurable):'
1999 user_magics=mf.UserMagics(self))
1999 user_magics=mf.UserMagics(self))
2000 self.configurables.append(self.magics_manager)
2000 self.configurables.append(self.magics_manager)
2001
2001
2002 # Expose as public API new_magic and registere_magics
2002 # Expose as public API from the magics manager
2003 self.new_magic = self.magics_manager.new_magic
2004 self.register_magics = self.magics_manager.register
2003 self.register_magics = self.magics_manager.register
2004 self.function_as_magic = self.magics_manager.function_as_magic
2005 self.define_magic = self.magics_manager._define_magic
2005
2006
2006 self.register_magics(mf.BasicMagics, mf.CodeMagics,
2007 self.register_magics(mf.BasicMagics, mf.CodeMagics,
2007 mf.ConfigMagics, mf.NamespaceMagics, mf.ExecutionMagics,
2008 mf.ConfigMagics, mf.NamespaceMagics, mf.ExecutionMagics,
@@ -2055,16 +2056,6 b' class InteractiveShell(SingletonConfigurable):'
2055 result = fn(*args)
2056 result = fn(*args)
2056 return result
2057 return result
2057
2058
2058 def define_magic(self, magic_name, func):
2059 """Expose own function as magic function for ipython
2060
2061 Note: this API is now deprecated. Instead, you should use
2062 `get_ipython().new_magic`.
2063 """
2064 warn('Deprecated API, use get_ipython().new_magic: %s\n' %
2065 magic_name)
2066 return self.new_magic(func, magic_name)
2067
2068 def find_line_magic(self, magic_name):
2059 def find_line_magic(self, magic_name):
2069 """Find and return a line magic by name."""
2060 """Find and return a line magic by name."""
2070 return self.magics_manager.magics['line'].get(magic_name)
2061 return self.magics_manager.magics['line'].get(magic_name)
@@ -30,7 +30,7 b' from IPython.external.decorator import decorator'
30 from IPython.utils.ipstruct import Struct
30 from IPython.utils.ipstruct import Struct
31 from IPython.utils.process import arg_split
31 from IPython.utils.process import arg_split
32 from IPython.utils.traitlets import Bool, Dict, Instance
32 from IPython.utils.traitlets import Bool, Dict, Instance
33 from IPython.utils.warn import error
33 from IPython.utils.warn import error, warn
34
34
35 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
36 # Globals
36 # Globals
@@ -190,16 +190,31 b' class MagicsManager(Configurable):'
190 for mtype in magic_types:
190 for mtype in magic_types:
191 self.magics[mtype].update(m.magics[mtype])
191 self.magics[mtype].update(m.magics[mtype])
192
192
193 def new_magic(self, func, magic_name=None, magic_type='line'):
193 def function_as_magic(self, func, magic_type='line', magic_name=None):
194 """Expose a standalone function as magic function for ipython.
194 """Expose a standalone function as magic function for ipython.
195
196 """
195 """
197
196
198 # Create the new method in the user_magics and register it in the
197 # Create the new method in the user_magics and register it in the
199 # global table
198 # global table
199 validate_type(magic_type)
200 magic_name = func.func_name if magic_name is None else magic_name
201 setattr(self.user_magics, magic_name, func)
200 newm, name = self.user_magics.new_magic(func, magic_type, magic_name)
202 newm, name = self.user_magics.new_magic(func, magic_type, magic_name)
201 _record_magic(self.magics, magic_type, name, newm)
203 _record_magic(self.magics, magic_type, name, newm)
202
204
205
206 def _define_magic(self, name, func):
207 """Support for deprecated API.
208
209 This method exists only to support the old-style definition of magics.
210 It will eventually be removed. Deliberately not documented further.
211 """
212 warn('Deprecated API, use function_as_magic or register_magics: %s\n' %
213 name)
214 meth = types.MethodType(func, self.user_magics)
215 setattr(self.user_magics, name, meth)
216 _record_magic(self.magics, 'line', name, meth)
217
203 # Key base class that provides the central functionality for magics.
218 # Key base class that provides the central functionality for magics.
204
219
205 class Magics(object):
220 class Magics(object):
@@ -357,13 +372,3 b' class Magics(object):'
357 if fn not in self.lsmagic():
372 if fn not in self.lsmagic():
358 error("%s is not a magic function" % fn)
373 error("%s is not a magic function" % fn)
359 self.options_table[fn] = optstr
374 self.options_table[fn] = optstr
360
361 def new_magic(self, func, magic_type='line', magic_name=None):
362 """TODO
363 """
364 magic_name = func.func_name if magic_name is None else magic_name
365 validate_type(magic_type)
366 meth = types.MethodType(func, self)
367 setattr(self, magic_name, meth)
368 _record_magic(self.magics, magic_type, magic_name, meth)
369 return meth, magic_name
General Comments 0
You need to be logged in to leave comments. Login now