From 7e14db60d893d9a9420be4062998b28289238808 2012-05-26 02:13:01 From: Fernando Perez Date: 2012-05-26 02:13:01 Subject: [PATCH] First full decoupling of magics into a standalone object. --- diff --git a/IPython/core/completer.py b/IPython/core/completer.py index dd23661..02f646f 100644 --- a/IPython/core/completer.py +++ b/IPython/core/completer.py @@ -472,7 +472,7 @@ class IPCompleter(Completer): # List where completion matches will be stored self.matches = [] - self.shell = shell.shell + self.shell = shell if alias_table is None: alias_table = {} self.alias_table = alias_table @@ -602,7 +602,7 @@ class IPCompleter(Completer): #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg # Get all shell magics now rather than statically, so magics loaded at # runtime show up too - magics = self.shell.lsmagic() + magics = self.shell._magic.lsmagic() pre = self.magic_escape baretext = text.lstrip(pre) return [ pre+m for m in magics if m.startswith(baretext)] diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index a2ccbe8..9c59da8 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -187,7 +187,7 @@ class ReadlineNoRecord(object): # Main IPython class #----------------------------------------------------------------------------- -class InteractiveShell(SingletonConfigurable, Magic): +class InteractiveShell(SingletonConfigurable): """An enhanced, interactive shell for Python.""" _instance = None @@ -430,7 +430,7 @@ class InteractiveShell(SingletonConfigurable, Magic): self.init_encoding() self.init_prefilter() - Magic.__init__(self, self) + self._magic = Magic(self) self.init_syntax_highlighting() self.init_hooks() @@ -588,11 +588,11 @@ class InteractiveShell(SingletonConfigurable, Magic): """Initialize logging in case it was requested at the command line. """ if self.logappend: - self.magic_logstart(self.logappend + ' append') + self.magic('logstart %s append' % self.logappend) elif self.logfile: - self.magic_logstart(self.logfile) + self.magic('logstart %' % self.logfile) elif self.logstart: - self.magic_logstart() + self.magic('logstart') def init_builtins(self): # A single, static flag that we set to True. Its presence indicates @@ -1397,7 +1397,7 @@ class InteractiveShell(SingletonConfigurable, Magic): if not found: if oname.startswith(ESC_MAGIC): oname = oname[1:] - obj = getattr(self,'magic_'+oname,None) + obj = self.find_magic(oname) if obj is not None: found = True ospace = 'IPython internal' @@ -1996,7 +1996,7 @@ class InteractiveShell(SingletonConfigurable, Magic): # FIXME: Move the color initialization to the DisplayHook, which # should be split into a prompt manager and displayhook. We probably # even need a centralize colors management object. - self.magic_colors(self.colors) + self.magic('colors %s' % self.colors) # History was moved to a separate module from IPython.core import history history.init_ipython(self) @@ -2026,11 +2026,11 @@ class InteractiveShell(SingletonConfigurable, Magic): magic_name, _, magic_args = arg_s.partition(' ') magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) - fn = getattr(self,'magic_'+magic_name,None) + fn = self.find_magic(magic_name) if fn is None: error("Magic function `%s` not found." % magic_name) else: - magic_args = self.var_expand(magic_args,1) + magic_args = self.var_expand(magic_args, 1) # Grab local namespace if we need it: if getattr(fn, "needs_local_scope", False): self._magic_locals = sys._getframe(1).f_locals @@ -2040,7 +2040,7 @@ class InteractiveShell(SingletonConfigurable, Magic): self._magic_locals = {} return result - def define_magic(self, magicname, func): + def define_magic(self, magic_name, func): """Expose own function as magic function for ipython Example:: @@ -2054,10 +2054,15 @@ class InteractiveShell(SingletonConfigurable, Magic): ip.define_magic('foo',foo_impl) """ im = types.MethodType(func,self) - old = getattr(self, "magic_" + magicname, None) - setattr(self, "magic_" + magicname, im) + old = self.find_magic(magic_name) + setattr(self._magic, 'magic_' + magic_name, im) return old + def find_magic(self, magic_name): + """Find and return a magic function by name. + """ + return getattr(self._magic, 'magic_' + magic_name, None) + #------------------------------------------------------------------------- # Things related to macros #------------------------------------------------------------------------- @@ -2685,7 +2690,7 @@ class InteractiveShell(SingletonConfigurable, Magic): # Now we must activate the gui pylab wants to use, and fix %run to take # plot updates into account self.enable_gui(gui) - self.magic_run = self._pylab_magic_run + self._magic.magic_run = self._pylab_magic_run #------------------------------------------------------------------------- # Utilities diff --git a/IPython/core/magic.py b/IPython/core/magic.py index 6980f0d..d15047e 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -107,7 +107,7 @@ class MacroToEdit(ValueError): pass # Configurable. This messes up the MRO in some way. The fix is that we need to # make Magic a configurable that InteractiveShell does not subclass. -class Magic: +class Magic(object): """Magic functions for InteractiveShell. Shell functions which can be reached as %function_name. All magic @@ -127,7 +127,7 @@ class Magic: #...................................................................... # some utility functions - def __init__(self,shell): + def __init__(self, shell): self.options_table = {} if profile is None: @@ -3012,7 +3012,7 @@ Defaulting color scheme to 'NoColor'""" # jump to bookmark if needed else: if not os.path.isdir(ps) or opts.has_key('b'): - bkms = self.db.get('bookmarks', {}) + bkms = self.shell.db.get('bookmarks', {}) if bkms.has_key(ps): target = bkms[ps] @@ -3049,7 +3049,7 @@ Defaulting color scheme to 'NoColor'""" if oldcwd != cwd: dhist.append(cwd) - self.db['dhist'] = compress_dhist(dhist)[-100:] + self.shell.db['dhist'] = compress_dhist(dhist)[-100:] if not 'q' in opts and self.shell.user_ns['_dh']: print self.shell.user_ns['_dh'][-1] diff --git a/IPython/core/splitinput.py b/IPython/core/splitinput.py index ab9063b..9cf060c 100644 --- a/IPython/core/splitinput.py +++ b/IPython/core/splitinput.py @@ -133,7 +133,7 @@ class LineInfo(object): """ if not self._oinfo: # ip.shell._ofind is actually on the Magic class! - self._oinfo = ip.shell._ofind(self.ifun) + self._oinfo = ip._ofind(self.ifun) return self._oinfo def __str__(self):