From 3ca71fa97cb8c8f762508995b14dabf03c05399f 2006-11-04 00:34:34 From: fptest Date: 2006-11-04 00:34:34 Subject: [PATCH] - Add a new ipconfig() public function for manipulating the internal rc configuration structure. - Add a new system_header variable to allow customizations of the system header printed when making system calls. --- diff --git a/IPython/Magic.py b/IPython/Magic.py index 6887eba..33bba52 100644 --- a/IPython/Magic.py +++ b/IPython/Magic.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Magic functions for InteractiveShell. -$Id: Magic.py 1846 2006-10-28 07:51:56Z vivainio $""" +$Id: Magic.py 1879 2006-11-04 00:34:34Z fptest $""" #***************************************************************************** # Copyright (C) 2001 Janko Hauser and @@ -502,16 +502,23 @@ Currently the magic system has the following functions:\n""" print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent] def magic_system_verbose(self, parameter_s = ''): - """Toggle verbose printing of system calls on/off.""" + """Set verbose printing of system calls. - self.shell.rc_set_toggle('system_verbose') + If called without an argument, act as a toggle""" + + if parameter_s: + val = bool(eval(parameter_s)) + else: + val = None + + self.shell.rc_set_toggle('system_verbose',val) print "System verbose printing is:",\ ['OFF','ON'][self.shell.rc.system_verbose] def magic_history(self, parameter_s = ''): """Print input history (_i variables), with most recent last. - %history -> print at most 40 inputs (some may be multi-line)\\ + %history -> print at most 40 inputs (some may be multi-line)\\ %history n -> print at most n inputs\\ %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\ @@ -1005,10 +1012,52 @@ Currently the magic system has the following functions:\n""" del(user_ns[i]) def magic_config(self,parameter_s=''): - """Show IPython's internal configuration.""" - - page('Current configuration structure:\n'+ - pformat(self.shell.rc.dict())) + """Handle IPython's internal configuration. + + If called without arguments, it will print IPython's complete internal + configuration. + + If called with one argument, it will print the value of that key in + the configuration. + + If called with more than one argument, the first is interpreted as a + key and the rest as a Python expression which gets eval()'d. + + Examples: + + In [1]: s='A Python string' + + In [2]: !echo $s + A Python string + + In [3]: config system_verbose True + + In [4]: !echo $s + IPython system call: echo A Python string + A Python string + + In [5]: %config system_header 'sys> ' + + In [6]: !echo $s + sys> echo A Python string + A Python string + + # Notice the extra quotes to protect the string after interpolation: + In [7]: header = "'sys2> '" + + In [8]: %config system_header $header + + In [9]: !echo $s + sys2> echo A Python string + A Python string + """ + + args = parameter_s.split(None,1) + key = args[0] + if len(args)==1: + self.shell.ipconfig(key) + else: + self.shell.ipconfig(key,eval(args[1])) def magic_logstart(self,parameter_s=''): """Start logging anywhere in a session. diff --git a/IPython/UserConfig/ipythonrc b/IPython/UserConfig/ipythonrc index 33ebc28..36b8cc6 100644 --- a/IPython/UserConfig/ipythonrc +++ b/IPython/UserConfig/ipythonrc @@ -1,5 +1,5 @@ # -*- Mode: Shell-Script -*- Not really, but shows comments correctly -# $Id: ipythonrc 1329 2006-05-26 07:52:45Z fperez $ +# $Id: ipythonrc 1879 2006-11-04 00:34:34Z fptest $ #*************************************************************************** # @@ -340,6 +340,15 @@ xmode Context multi_line_specials 1 + +# System calls: When IPython makes system calls (e.g. via special syntax like +# !cmd or !!cmd, or magics like %sc or %sx), it can print the command it is +# executing to standard output, prefixed by a header string. + +system_header "IPython system call: " + +system_verbose 1 + # wxversion: request a specific wxPython version (used for -wthread) # Set this to the value of wxPython you want to use, but note that this diff --git a/IPython/iplib.py b/IPython/iplib.py index 4a7fc86..ca749b0 100644 --- a/IPython/iplib.py +++ b/IPython/iplib.py @@ -6,7 +6,7 @@ Requires Python 2.3 or newer. This file contains all the classes and helper functions specific to IPython. -$Id: iplib.py 1878 2006-11-03 23:00:22Z fptest $ +$Id: iplib.py 1879 2006-11-04 00:34:34Z fptest $ """ #***************************************************************************** @@ -464,16 +464,18 @@ class InteractiveShell(object,Magic): # and it allows interpolation of variables in the user's namespace. self.system = lambda cmd: \ shell(self.var_expand(cmd,depth=2), - header='IPython system call: ', + header=self.rc.system_header, verbose=self.rc.system_verbose) + # These are for getoutput and getoutputerror: self.getoutput = lambda cmd: \ getoutput(self.var_expand(cmd,depth=2), - header='IPython system call: ', + header=self.rc.system_header, verbose=self.rc.system_verbose) + self.getoutputerror = lambda cmd: \ getoutputerror(self.var_expand(cmd,depth=2), - header='IPython system call: ', + header=self.rc.system_header, verbose=self.rc.system_verbose) # RegExp for splitting line contents into pre-char//first @@ -726,6 +728,7 @@ class InteractiveShell(object,Magic): ipmagic = self.ipmagic, ipalias = self.ipalias, ipsystem = self.ipsystem, + ipconfig = self.ipconfig, _ip = self.api ) for biname,bival in builtins_new.items(): @@ -961,6 +964,29 @@ class InteractiveShell(object,Magic): else: error("Alias `%s` not found." % alias_name) + def ipconfig(self,key=None,value=None): + """Manipulate the IPython config. + + This provides a python interface to + If called with no arguments, it prints the internal IPython config + + Optional arguments: + + - key(None): if given, what key of the rc structure to return. + + - value(None): if given, set the key to this value.""" + + if key is None: + page('Current configuration structure:\n'+ + pformat(self.rc.dict())) + else: + if value is None: + print '%s -> %s' % (key,self.rc[key]) + else: + if key not in self.rc: + raise KeyError(str(key)) + self.rc[key] = value + def ipsystem(self,arg_s): """Make a system call, using IPython.""" diff --git a/IPython/ipmaker.py b/IPython/ipmaker.py index 7973de3..364268c 100644 --- a/IPython/ipmaker.py +++ b/IPython/ipmaker.py @@ -6,7 +6,7 @@ Requires Python 2.1 or better. This file contains the main make_IPython() starter function. -$Id: ipmaker.py 1384 2006-06-29 20:04:37Z vivainio $""" +$Id: ipmaker.py 1879 2006-11-04 00:34:34Z fptest $""" #***************************************************************************** # Copyright (C) 2001-2006 Fernando Perez. @@ -218,6 +218,7 @@ object? -> Details about 'object'. ?object also works, ?? prints more. separate_in = '\n', separate_out = '\n', separate_out2 = '', + system_header = 'IPython system call: ', system_verbose = 0, gthread = 0, qthread = 0, diff --git a/IPython/irunner.py b/IPython/irunner.py index 8f1b050..e75da7d 100755 --- a/IPython/irunner.py +++ b/IPython/irunner.py @@ -299,7 +299,16 @@ def main(): runners = dict(ipython=IPythonRunner, python=PythonRunner, sage=SAGERunner) - runners[opts.mode]().main(args) + + try: + ext = os.path.splitext(args[0]) + except IndexError: + ext = '' + modes = {'.ipy':'ipython', + '.py':'python', + '.sage':'sage'} + mode = modes.get(ext,opts.mode) + runners[mode]().main(args) if __name__ == '__main__': main() diff --git a/doc/ChangeLog b/doc/ChangeLog index bab8cc0..ab91bf5 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,16 @@ +2006-11-03 Fernando Perez + + * IPython/iplib.py (InteractiveShell.ipconfig): new builtin, also + visible in ipapi as ip.config(), to programatically control the + internal rc object. There's an accompanying %config magic for + interactive use, which has been enhanced to match the + funtionality in ipconfig. + + * IPython/Magic.py (magic_system_verbose): Change %system_verbose + so it's not just a toggle, it now takes an argument. Add support + for a customizable header when making system calls, as the new + system_header variable in the ipythonrc file. + 2006-11-03 Walter Doerwald * IPython/Extensions/ipipe.py: xrepr(), xiter() and xattrs() are now