From ab39b3d864b65b20282467d5635767dd91315b6e 2007-06-27 22:51:16 From: vivainio Date: 2007-06-27 22:51:16 Subject: [PATCH] implement callable (i.e. straight python) aliases and _sh shadow namespace --- diff --git a/IPython/OInspect.py b/IPython/OInspect.py index af586c3..340922a 100644 --- a/IPython/OInspect.py +++ b/IPython/OInspect.py @@ -6,7 +6,7 @@ Uses syntax highlighting for presenting the various information elements. Similar in spirit to the inspect module, but all calls take a name argument to reference the name under which an object is being read. -$Id: OInspect.py 1850 2006-10-28 19:48:13Z fptest $ +$Id: OInspect.py 2463 2007-06-27 22:51:16Z vivainio $ """ #***************************************************************************** @@ -337,7 +337,10 @@ class Inspector: ospace = info.namespace # Get docstring, special-casing aliases: if isalias: - ds = "Alias to the system command:\n %s" % obj[1] + if not callable(obj): + ds = "Alias to the system command:\n %s" % obj[1] + else: + ds = "Alias to " + str(obj) else: ds = getdoc(obj) if ds is None: diff --git a/IPython/ipapi.py b/IPython/ipapi.py index ad9e937..e93f7c2 100644 --- a/IPython/ipapi.py +++ b/IPython/ipapi.py @@ -335,6 +335,12 @@ class IPApi: Creates a new alias named 'bb' in ipython user namespace """ + if callable(cmd): + self.IP.alias_table[name] = cmd + import IPython.shawodns + setattr(IPython.shadowns, name,cmd) + return + nargs = cmd.count('%s') if nargs>0 and cmd.find('%l')>=0: diff --git a/IPython/iplib.py b/IPython/iplib.py index 6595047..79eaed9 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 2442 2007-06-14 21:20:10Z vivainio $ +$Id: iplib.py 2463 2007-06-27 22:51:16Z vivainio $ """ #***************************************************************************** @@ -76,7 +76,7 @@ from IPython.strdispatch import StrDispatch import IPython.ipapi import IPython.history import IPython.prefilter as prefilter - +import IPython.shadowns # Globals # store the builtin raw_input globally, and use this always, in case user code @@ -379,6 +379,7 @@ class InteractiveShell(object,Magic): self.user_ns['In'] = self.input_hist self.user_ns['Out'] = self.output_hist + self.user_ns['_sh'] = IPython.shadowns # Object variable to store code object waiting execution. This is # used mainly by the multithreaded shells, but it can come in handy in # other situations. No need to use a Queue here, since it's a single @@ -2119,13 +2120,18 @@ want to merge them back into the new files.""" % locals() return line def handle_alias(self,line_info): - """Handle alias input lines. """ - transformed = self.expand_aliases(line_info.iFun,line_info.theRest) + """Handle alias input lines. """ + tgt = self.alias_table[line_info.iFun] + # print "=>",tgt #dbg + if callable(tgt): + line_out = "_sh." + line_info.iFun + '(r"""' + line_info.theRest + '""")' + else: + transformed = self.expand_aliases(line_info.iFun,line_info.theRest) - # pre is needed, because it carries the leading whitespace. Otherwise - # aliases won't work in indented sections. - line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, - make_quoted_expr( transformed )) + # pre is needed, because it carries the leading whitespace. Otherwise + # aliases won't work in indented sections. + line_out = '%s_ip.system(%s)' % (line_info.preWhitespace, + make_quoted_expr( transformed )) self.log(line_info.line,line_out,line_info.continue_prompt) #print 'line out:',line_out # dbg diff --git a/IPython/shadowns.py b/IPython/shadowns.py new file mode 100644 index 0000000..d2d93b6 --- /dev/null +++ b/IPython/shadowns.py @@ -0,0 +1 @@ +""" Shadow namespace """ \ No newline at end of file diff --git a/doc/ChangeLog b/doc/ChangeLog index 4f1c759..95032ee 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,17 @@ +2007-06-28 Ville Vainio + + * shadowns.py, iplib.py, ipapi.py, OInspect.py: + Implement "shadow" namespace, and callable aliases that reside there. + Use them by: + + _ip.defalias('foo',myfunc) # creates _sh.foo that points to myfunc + + foo hello world + (gets translated to:) + _sh.foo(r"""hello world""") + + In practice, this kind of alias can take the role of a magic function + 2007-06-14 Ville Vainio * iplib.py (handle_auto): Try to use ascii for printing "--->"