From f8570b62b5d70134a052f261d4eeab0845df0e0a 2007-09-06 18:53:34 From: vivainio Date: 2007-09-06 18:53:34 Subject: [PATCH] callable aliases now get _ip as first arg --- diff --git a/IPython/Extensions/ipy_fsops.py b/IPython/Extensions/ipy_fsops.py index 78fef3b..da93eb4 100644 --- a/IPython/Extensions/ipy_fsops.py +++ b/IPython/Extensions/ipy_fsops.py @@ -37,7 +37,7 @@ def parse_args(args): raise IpyShellCmdException("No files found:" + str(tup2[0:-1])) return flist, trg -def icp(arg): +def icp(ip,arg): """ icp files... targetdir Copy all files to target, creating dirs for target if necessary @@ -57,7 +57,7 @@ def icp(arg): return fs ip.defalias("icp",icp) -def imv(arg): +def imv(ip,arg): """ imv src tgt Move source to target. @@ -71,7 +71,7 @@ def imv(arg): return fs ip.defalias("imv",imv) -def irm(arg): +def irm(ip,arg): """ irm path[s]... Remove file[s] or dir[s] path. Dirs are deleted recursively. @@ -87,7 +87,7 @@ def irm(arg): ip.defalias("irm",irm) -def imkdir(arg): +def imkdir(ip,arg): """ imkdir path Creates dir path, and all dirs on the road @@ -98,7 +98,7 @@ def imkdir(arg): ip.defalias("imkdir",imkdir) -def igrep(arg): +def igrep(ip,arg): """ igrep PAT files... Very dumb file scan, case-insensitive. @@ -124,7 +124,7 @@ def igrep(arg): ip.defalias("igrep",igrep) -def collect(arg): +def collect(ip,arg): """ collect foo/a.txt rec:bar=*.py Copies foo/a.txt to ~/_ipython/collect/foo/a.txt and *.py from bar, @@ -154,7 +154,7 @@ def collect(arg): ip.defalias("collect",collect) -def inote(arg): +def inote(ip,arg): """ inote Hello world Adds timestamp and Hello world to ~/_ipython/notes.txt @@ -170,5 +170,4 @@ def inote(arg): except IndexError: ip.IP.hooks.editor(fname) -ip.defalias("inote",inote) - +ip.defalias("inote",inote) diff --git a/IPython/Extensions/ipy_rehashdir.py b/IPython/Extensions/ipy_rehashdir.py index 02c2a77..6e4f44b 100644 --- a/IPython/Extensions/ipy_rehashdir.py +++ b/IPython/Extensions/ipy_rehashdir.py @@ -21,7 +21,7 @@ ip = IPython.ipapi.get() import os,re,fnmatch,sys -def selflaunch(line): +def selflaunch(ip,line): """ Launch python script with 'this' interpreter e.g. d:\foo\ipython.exe a.py @@ -40,7 +40,7 @@ class PyLauncher: """ def __init__(self,script): self.script = os.path.abspath(script) - def __call__(self, line): + def __call__(self, ip, line): selflaunch("py " + self.script + ' ' + line) def __repr__(self): return 'PyLauncher("%s")' % self.script diff --git a/IPython/Extensions/ipy_workdir.py b/IPython/Extensions/ipy_workdir.py index 38a0b1d..07ae5d4 100644 --- a/IPython/Extensions/ipy_workdir.py +++ b/IPython/Extensions/ipy_workdir.py @@ -6,11 +6,21 @@ ip = IPython.ipapi.get() import os, subprocess workdir = None -def workdir_f(line): +def workdir_f(ip,line): + """ Exceute commands residing in cwd elsewhere + + Example:: + + workdir /myfiles + cd bin + workdir myscript.py + + executes myscript.py (stored in bin, but not in path) in /myfiles + """ global workdir dummy,cmd = line.split(None,1) if os.path.isdir(cmd): - workdir = cmd + workdir = os.path.abspath(cmd) print "Set workdir",workdir elif workdir is None: print "Please set workdir first by doing e.g. 'workdir q:/'" @@ -22,12 +32,12 @@ def workdir_f(line): head, tail = sp if os.path.isfile(head): cmd = os.path.abspath(head) + ' ' + tail - print "Execute command",cmd,"in",workdir - ret = subprocess.call(cmd, shell = True, cwd = workdir) + print "Execute command '" + cmd+ "' in",workdir + olddir = os.getcwd() + os.chdir(workdir) + try: + os.system(cmd) + finally: + os.chdir(olddir) ip.defalias("workdir",workdir_f) - - - - - diff --git a/IPython/iplib.py b/IPython/iplib.py index 03da63f..a779536 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 2718 2007-09-05 21:54:50Z vivainio $ +$Id: iplib.py 2719 2007-09-06 18:53:34Z vivainio $ """ #***************************************************************************** @@ -2174,9 +2174,9 @@ want to merge them back into the new files.""" % locals() # print "=>",tgt #dbg if callable(tgt): if '$' in line_info.line: - call_meth = '(_ip.itpl(%s))' + call_meth = '(_ip, _ip.itpl(%s))' else: - call_meth = '(%s)' + call_meth = '(_ip,%s)' line_out = ("%s_sh.%s" + call_meth) % (line_info.preWhitespace, line_info.iFun, make_quoted_expr(line_info.line)) diff --git a/doc/ChangeLog b/doc/ChangeLog index 8209cae..3447fe3 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,10 @@ +2007-09-06 Ville Vainio + + * ipy_rehashdir.py, ipy_workdir.py, ipy_fsops.py, iplib.py: + Callable aliases now pass the _ip as first arg. This breaks + compatibility with earlier 0.8.2.svn series! (though they should + not have been in use yet outside these few extensions) + 2007-09-05 Ville Vainio * external/mglob.py: expand('dirname') => ['dirname'], instead @@ -305,7 +312,7 @@ * IPython/Extensions/ipipe.py: Added a new table icap, which executes a Python string and captures the output. (Idea and original patch by - St�fan van der Walt) + Stefan van der Walt) 2007-06-01 Fernando Perez @@ -325,7 +332,7 @@ in the input line. Split the statusbar to be able to display the currently active refresh interval. (Patch by Nik Tautenhahn) -2007-05-29 J�rgen Stenarson +2007-05-29 Jorgen Stenarson * fixing set_term_title to use ctypes as default @@ -515,7 +522,7 @@ * UserConfig/ipy_user_conf.py, ipy_profile_sh.py: do not import ipy_stock_completers on default profile, do import on sh profile. -2007-04-22 J�rgen Stenarson +2007-04-22 Jorgen Stenarson * Fix bug in iplib.py/safe_execfile when launching ipython with a script like ipython.py foo.py which raised a IndexError. @@ -731,7 +738,7 @@ * IPython/iplib.py (raw_input): Fix mishandling of unicode at input. Patch sent by Stefan. -2007-03-20 J�rgen Stenarson +2007-03-20 Jorgen Stenarson * IPython/Extensions/ipy_stock_completer.py shlex_split, fix bug in shlex_split. len function call was missing an if statement. Caused shlex_split to @@ -789,7 +796,7 @@ * Extensions/ipy_stock_completers.py: Fixed exception on mismatching quotes in %run completer. Patch by - J�rgen Stenarson. Closes #127. + Jorgen Stenarson. Closes #127. 2007-03-14 Ville Vainio @@ -819,7 +826,7 @@ * IPython/Extensions/igrid.py: Patch by Nik Tautenhahn: Close help window if exiting igrid. -2007-03-02 J�rgen Stenarson +2007-03-02 Jorgen Stenarson * IPython/Extensions/ipy_defaults.py: Check if readline is available before calling functions from readline. diff --git a/test/test_ipapi.py b/test/test_ipapi.py index 81850a2..31fde66 100644 --- a/test/test_ipapi.py +++ b/test/test_ipapi.py @@ -1,3 +1,6 @@ +import sys +sys.path.append('..') + import IPython.ipapi IPython.ipapi.make_session() @@ -17,7 +20,8 @@ def test_db(): def test_defalias(): slot = [None] # test callable alias - def cb(s): + def cb(localip,s): + assert localip is ip slot[0] = s ip.defalias('testalias', cb)