From 580a081f89a939d0a03c8e0abfce33711e37c100 2012-05-26 03:19:59 From: Fernando Perez Date: 2012-05-26 03:19:59 Subject: [PATCH] Remove unnecessary metaclass and allow registration of magic classes. This lets us register uninstantiated magic classes as well, making the init logic a bit cleaner in the main shell class. --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 379c8a3..e73c9bb 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -1999,12 +1999,10 @@ class InteractiveShell(SingletonConfigurable): user_magics=mf.UserMagics(self)) self.configurables.append(self.magics_manager) - all_m = [t(self) for t in [mf.BasicMagics, mf.CodeMagics, + self.magics_manager.register(mf.BasicMagics, mf.CodeMagics, mf.ConfigMagics, mf.NamespaceMagics, mf.ExecutionMagics, mf.AutoMagics, mf.OSMagics, mf.LoggingMagics, mf.ExtensionsMagics, - mf.PylabMagics, mf.DeprecatedMagics] ] - self.all_m = all_m - self.magics_manager.register(*all_m) + mf.PylabMagics, mf.DeprecatedMagics) # FIXME: Move the color initialization to the DisplayHook, which # should be split into a prompt manager and displayhook. We probably diff --git a/IPython/core/magic.py b/IPython/core/magic.py index 6a6cbca..1c3d3e8 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -178,6 +178,10 @@ class MagicsManager(Configurable): if not m.registered: raise ValueError("Class of magics %r was constructed without " "the @register_macics class decorator") + if type(m) is type: + # If we're given an uninstantiated class + m = m(self.shell) + for mtype in magic_types: self.magics[mtype].update(m.magics[mtype]) @@ -228,10 +232,7 @@ class Magics(object): # Non-configurable class attributes magics = Dict - class __metaclass__(type): - def __new__(cls, name, bases, dct): - cls.registered = False - return type.__new__(cls, name, bases, dct) + registered = False def __init__(self, shell): if not(self.__class__.registered): @@ -241,7 +242,8 @@ class Magics(object): for mtype in magic_types: tab = mtab[mtype] for magic_name, meth_name in self.magics[mtype].iteritems(): - tab[magic_name] = getattr(self, meth_name) + if isinstance(meth_name, basestring): + tab[magic_name] = getattr(self, meth_name) self.magics.update(mtab) def arg_err(self,func): diff --git a/IPython/core/magic_functions.py b/IPython/core/magic_functions.py index e686646..ac0ee74 100644 --- a/IPython/core/magic_functions.py +++ b/IPython/core/magic_functions.py @@ -143,7 +143,6 @@ class BasicMagics(Magics): magic_docs.append('%s%s:\n\t%s\n' % (escape, fname, fndoc)) - magic_docs = ''.join(magic_docs) if mode == 'rest':