diff --git a/IPython/genutils.py b/IPython/genutils.py index e4a38cc..b4ffd2b 100644 --- a/IPython/genutils.py +++ b/IPython/genutils.py @@ -5,7 +5,7 @@ General purpose utilities. This is a grab-bag of stuff I find useful in most programs I write. Some of these things are also convenient when working at the command line. -$Id: genutils.py 960 2005-12-28 06:51:01Z fperez $""" +$Id: genutils.py 967 2005-12-29 09:02:13Z fperez $""" #***************************************************************************** # Copyright (C) 2001-2004 Fernando Perez. @@ -860,6 +860,7 @@ class LSString(str): n = nlstr = property(get_nlstr) +#---------------------------------------------------------------------------- class SList(list): """List derivative with a special access attributes. @@ -895,6 +896,19 @@ class SList(list): n = nlstr = property(get_nlstr) +#---------------------------------------------------------------------------- +# This can be replaced with an isspace() call once we drop 2.2 compatibility +_isspace_match = re.compile(r'^\s+$').match +def isspace(s): + return bool(_isspace_match(s)) + +#---------------------------------------------------------------------------- +def esc_quotes(strng): + """Return the input string with single and double quotes escaped out""" + + return strng.replace('"','\\"').replace("'","\\'") + +#---------------------------------------------------------------------------- def raw_input_multi(header='', ps1='==> ', ps2='..> ',terminate_str = '.'): """Take multiple lines of input. @@ -1026,6 +1040,19 @@ def qwflat(words,sep=None,maxsplit=-1): """Calls qw(words) in flat mode. It's just a convenient shorthand.""" return qw(words,1,sep,maxsplit) +#---------------------------------------------------------------------------- +def qw_lol(indata): + """qw_lol('a b') -> [['a','b']], + otherwise it's just a call to qw(). + + We need this to make sure the modules_some keys *always* end up as a + list of lists.""" + + if type(indata) in StringTypes: + return [qw(indata)] + else: + return qw(indata) + #----------------------------------------------------------------------------- def list_strings(arg): """Always return a list of strings, given a string or list of strings @@ -1585,6 +1612,15 @@ def map_method(method,object_list,*argseq,**kw): return out_list #---------------------------------------------------------------------------- +def import_fail_info(mod_name,fns=None): + """Inform load failure for a module.""" + + if fns == None: + warn("Loading of %s failed.\n" % (mod_name,)) + else: + warn("Loading of %s from %s failed.\n" % (fns,mod_name)) + +#---------------------------------------------------------------------------- # Proposed popitem() extension, written as a method class NotGiven: pass diff --git a/IPython/iplib.py b/IPython/iplib.py index f494950..8cbf24f 100644 --- a/IPython/iplib.py +++ b/IPython/iplib.py @@ -6,7 +6,7 @@ Requires Python 2.1 or newer. This file contains all the classes and helper functions specific to IPython. -$Id: iplib.py 966 2005-12-29 08:34:07Z fperez $ +$Id: iplib.py 967 2005-12-29 09:02:13Z fperez $ """ #***************************************************************************** @@ -81,35 +81,24 @@ raw_input_original = raw_input #**************************************************************************** # Some utility function definitions -# This can be replaced with an isspace() call once we drop 2.2 compatibility -_isspace_match = re.compile(r'^\s+$').match -def isspace(s): - return bool(_isspace_match(s)) - -def esc_quotes(strng): - """Return the input string with single and double quotes escaped out""" - - return strng.replace('"','\\"').replace("'","\\'") - -def import_fail_info(mod_name,fns=None): - """Inform load failure for a module.""" - - if fns == None: - warn("Loading of %s failed.\n" % (mod_name,)) - else: - warn("Loading of %s from %s failed.\n" % (fns,mod_name)) - -def qw_lol(indata): - """qw_lol('a b') -> [['a','b']], - otherwise it's just a call to qw(). - - We need this to make sure the modules_some keys *always* end up as a - list of lists.""" +def softspace(file, newvalue): + """Copied from code.py, to remove the dependency""" + oldvalue = 0 + try: + oldvalue = file.softspace + except AttributeError: + pass + try: + file.softspace = newvalue + except (AttributeError, TypeError): + # "attribute-less object" or "read-only attributes" + pass + return oldvalue - if type(indata) in StringTypes: - return [qw(indata)] - else: - return qw(indata) +#**************************************************************************** +# These special functions get installed in the builtin namespace, to provide +# programmatic (pure python) access to magics and aliases. This is important +# for logging, user scripting, and more. def ipmagic(arg_s): """Call a magic function by name. @@ -173,21 +162,6 @@ def ipalias(arg_s): else: error("Alias `%s` not found." % alias_name) -def softspace(file, newvalue): - """Copied from code.py, to remove the dependency""" - oldvalue = 0 - try: - oldvalue = file.softspace - except AttributeError: - pass - try: - file.softspace = newvalue - except (AttributeError, TypeError): - # "attribute-less object" or "read-only attributes" - pass - return oldvalue - - #**************************************************************************** # Local use exceptions class SpaceInInput(exceptions.Exception): pass @@ -249,7 +223,6 @@ class SyntaxTB(ultraTB.ListTB): # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell', # 'self.value'] - class InteractiveShell(Magic): """An enhanced console for Python.""" @@ -1802,10 +1775,12 @@ want to merge them back into the new files.""" % locals() """Handle alias input lines. """ theRest = esc_quotes(theRest) - line_out = "%s%s.call_alias('%s','%s')" % (pre,self.name,iFun,theRest) - self.log(line_out,continue_prompt) - self.update_cache(line_out) - return line_out + # log the ipalias form, which doesn't depend on the instance name + line_log = 'ipalias("%s %s")' % (iFun,theRest) + self.log(line_log,continue_prompt) + self.update_cache(line_log) + # this is what actually gets executed + return "%s%s.call_alias('%s','%s')" % (pre,self.name,iFun,theRest) def handle_shell_escape(self, line, continue_prompt=None, pre=None,iFun=None,theRest=None): diff --git a/IPython/ipmaker.py b/IPython/ipmaker.py index ae3c393..09eb302 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 966 2005-12-29 08:34:07Z fperez $""" +$Id: ipmaker.py 967 2005-12-29 09:02:13Z fperez $""" #***************************************************************************** # Copyright (C) 2001-2004 Fernando Perez. @@ -49,7 +49,7 @@ from IPython import DPyGetOpt from IPython.Struct import Struct from IPython.OutputTrap import OutputTrap from IPython.ConfigLoader import ConfigLoader -from IPython.iplib import InteractiveShell,qw_lol,import_fail_info +from IPython.iplib import InteractiveShell from IPython.usage import cmd_line_usage,interactive_usage from IPython.genutils import *