From f1320212a040a8dfed56524f282aef22156e38a0 2007-09-05 21:54:50 From: vivainio Date: 2007-09-05 21:54:50 Subject: [PATCH] added _ip.itpl, to enable var expansion with callable aliases --- diff --git a/IPython/ipapi.py b/IPython/ipapi.py index 1b2e065..daa107b 100644 --- a/IPython/ipapi.py +++ b/IPython/ipapi.py @@ -362,6 +362,14 @@ class IPApi: res = pre + self.IP.expand_aliases(fn,rest) return res + def itpl(self, s, depth = 1): + """ Expand Itpl format string s. + + Only callable from command line (i.e. prefilter results); + If you use in your scripts, you need to use a bigger depth! + """ + return self.IP.var_expand(s, depth) + def defalias(self, name, cmd): """ Define a new alias @@ -463,7 +471,7 @@ class IPApi: self.extensions[mod] = m return m - + class DebugTools: """ Used for debugging mishaps in api usage diff --git a/IPython/iplib.py b/IPython/iplib.py index c2f9164..03da63f 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 2713 2007-09-05 18:34:29Z vivainio $ +$Id: iplib.py 2718 2007-09-05 21:54:50Z vivainio $ """ #***************************************************************************** @@ -316,7 +316,6 @@ class InteractiveShell(object,Magic): 'internal':self.internal_ns, 'builtin':__builtin__.__dict__ } - # The user namespace MUST have a pointer to the shell itself. self.user_ns[name] = self @@ -592,6 +591,8 @@ class InteractiveShell(object,Magic): # tracking what it did via the builtins_added dict. self.add_builtins() + + # end __init__ def var_expand(self,cmd,depth=0): @@ -2172,8 +2173,13 @@ want to merge them back into the new files.""" % locals() tgt = self.alias_table[line_info.iFun] # print "=>",tgt #dbg if callable(tgt): - line_out = "_sh.%s(%s)" % (line_info.iFun, - make_quoted_expr(self.var_expand(line_info.line, depth=2))) + if '$' in line_info.line: + call_meth = '(_ip.itpl(%s))' + else: + call_meth = '(%s)' + line_out = ("%s_sh.%s" + call_meth) % (line_info.preWhitespace, + line_info.iFun, + make_quoted_expr(line_info.line)) else: transformed = self.expand_aliases(line_info.iFun,line_info.theRest)