##// END OF EJS Templates
a = !ls, a = %alias now work (captures output or gets ret val for aliases)...
vivainio -
Show More
@@ -0,0 +1,66 b''
1 # -*- coding: utf-8 -*-
2 """ IPython extension: new prefilters for output grabbing
3
4 Provides
5
6 var = %magic blah blah
7
8 var = !ls
9
10 $Id: genutils.py 1077 2006-01-24 18:15:27Z vivainio $
11
12 """
13
14 import IPython.ipapi
15 from IPython.genutils import *
16
17 ip = IPython.ipapi.get()
18
19 import re
20
21 def hnd_magic(line,mo):
22 """ Handle a = %mymagic blah blah """
23 #cmd = genutils.make_quoted_expr(mo.group('syscmd'))
24 #mag = 'ipmagic
25 #return "%s = %s"
26 var = mo.group('varname')
27 cmd = mo.group('cmd')
28 expr = make_quoted_expr(cmd)
29 return itpl('$var = ipmagic($expr)')
30
31 def hnd_syscmd(line,mo):
32 """ Handle a = !ls """
33 #cmd = genutils.make_quoted_expr(mo.group('syscmd'))
34 #mag = 'ipmagic
35 #return "%s = %s"
36 var = mo.group('varname')
37 cmd = mo.group('cmd')
38 expr = make_quoted_expr(itpl("sc -l =$cmd"))
39 return itpl('$var = ipmagic($expr)')
40
41 def install_re_handler(pat, hnd):
42 ip.meta().re_prefilters.append((re.compile(pat), hnd))
43
44 def init_handlers():
45
46 ip.meta().re_prefilters = []
47
48 install_re_handler('(?P<varname>[\w\.]+)\s*=\s*%(?P<cmd>.*)',
49 hnd_magic
50 )
51
52 install_re_handler('(?P<varname>[\w\.]+)\s*=\s*!(?P<cmd>.*)',
53 hnd_syscmd
54 )
55
56 init_handlers()
57
58 def regex_prefilter_f(self,line):
59 for pat, handler in ip.meta().re_prefilters:
60 mo = pat.match(line)
61 if mo:
62 return handler(line,mo)
63
64 raise IPython.ipapi.TryNext
65
66 ip.set_hook('input_prefilter', regex_prefilter_f) No newline at end of file
@@ -14,6 +14,5 b' import IPython.ipapi as ip'
14 14
15 15 import sys
16 16
17 if sys.version_info >= (2,4):
18 # rehashdir extension requires python 2.4
19 import ext_rehashdir No newline at end of file
17 import ext_rehashdir # %rehashdir magic
18 import ext_rescapture # var = !ls and var = %magic
@@ -1,7 +1,7 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Magic functions for InteractiveShell.
3 3
4 $Id: Magic.py 1077 2006-01-24 18:15:27Z vivainio $"""
4 $Id: Magic.py 1089 2006-01-27 19:04:59Z vivainio $"""
5 5
6 6 #*****************************************************************************
7 7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
@@ -2171,14 +2171,15 b' Defaulting color scheme to \'NoColor\'"""'
2171 2171 prechar = ''
2172 2172 else:
2173 2173 prechar = self.shell.ESC_MAGIC
2174 print 'Alias\t\tSystem Command\n'+'-'*30
2174 #print 'Alias\t\tSystem Command\n'+'-'*30
2175 2175 atab = self.shell.alias_table
2176 2176 aliases = atab.keys()
2177 2177 aliases.sort()
2178 res = []
2178 2179 for alias in aliases:
2179 print prechar+alias+'\t\t'+atab[alias][1]
2180 print '-'*30+'\nTotal number of aliases:',len(aliases)
2181 return
2180 res.append((alias, atab[alias][1]))
2181 print "Total number of aliases:",len(aliases)
2182 return res
2182 2183 try:
2183 2184 alias,cmd = par.split(None,1)
2184 2185 except:
@@ -2443,6 +2444,18 b' Defaulting color scheme to \'NoColor\'"""'
2443 2444 def magic_sc(self, parameter_s=''):
2444 2445 """Shell capture - execute a shell command and capture its output.
2445 2446
2447 DEPRECATED. Suboptimal, retained for backwards compatibility.
2448
2449 You should use the form 'var = !command' instead. Example:
2450
2451 "%sc -l myfiles = ls ~" should now be written as
2452
2453 "myfiles = !ls ~"
2454
2455 myfiles.s, myfiles.l and myfiles.n still apply as documented
2456 below.
2457
2458 --
2446 2459 %sc [options] varname=command
2447 2460
2448 2461 IPython will run the given command using commands.getoutput(), and
@@ -2452,6 +2465,8 b' Defaulting color scheme to \'NoColor\'"""'
2452 2465
2453 2466 The '=' sign in the syntax is mandatory, and the variable name you
2454 2467 supply must follow Python's standard conventions for valid names.
2468
2469 (A special format without variable name exists for internal use)
2455 2470
2456 2471 Options:
2457 2472
@@ -2530,9 +2545,6 b' Defaulting color scheme to \'NoColor\'"""'
2530 2545 _,cmd = parameter_s.split('=',1)
2531 2546 except ValueError:
2532 2547 var,cmd = '',''
2533 if not var:
2534 error('you must specify a variable to assign the command to.')
2535 return
2536 2548 # If all looks ok, proceed
2537 2549 out,err = self.shell.getoutputerror(cmd)
2538 2550 if err:
@@ -2543,7 +2555,10 b' Defaulting color scheme to \'NoColor\'"""'
2543 2555 out = LSString(out)
2544 2556 if opts.has_key('v'):
2545 2557 print '%s ==\n%s' % (var,pformat(out))
2546 self.shell.user_ns.update({var:out})
2558 if var:
2559 self.shell.user_ns.update({var:out})
2560 else:
2561 return out
2547 2562
2548 2563 def magic_sx(self, parameter_s=''):
2549 2564 """Shell execute - run a shell command and capture its output.
@@ -140,6 +140,15 b' class IPApi:'
140 140
141 141 Returns the result of evaluation"""
142 142 return eval(expr,self.user_ns())
143
144 def meta(self):
145 """ Get a session-specific data store
146
147 Object returned by this method can be used to store
148 data that should persist through the ipython session.
149 """
150 return self.IP.meta
151
143 152
144 153 def launch_new_instance(user_ns = None):
145 154 """ Create and start a new ipython instance.
@@ -6,7 +6,7 b' Requires Python 2.3 or newer.'
6 6
7 7 This file contains all the classes and helper functions specific to IPython.
8 8
9 $Id: iplib.py 1088 2006-01-27 17:16:45Z vivainio $
9 $Id: iplib.py 1089 2006-01-27 19:04:59Z vivainio $
10 10 """
11 11
12 12 #*****************************************************************************
@@ -242,7 +242,7 b' class InteractiveShell(object,Magic):'
242 242 # convenient location for storing additional information and state
243 243 # their extensions may require, without fear of collisions with other
244 244 # ipython names that may develop later.
245 self.meta = Bunch()
245 self.meta = Struct()
246 246
247 247 # Create the namespace where the user will operate. user_ns is
248 248 # normally the only one used, and it is passed to the exec calls as
@@ -724,6 +724,8 b' class InteractiveShell(object,Magic):'
724 724 # accepts it. Probably at least check that the hook takes the number
725 725 # of args it's supposed to.
726 726 dp = getattr(self.hooks, name, None)
727 if name not in IPython.hooks.__all__:
728 print "Warning! Hook '%s' is not one of %s" % (name, IPython.hooks.__all__ )
727 729 if not dp:
728 730 dp = IPython.hooks.CommandChainDispatcher()
729 731
@@ -4,6 +4,16 b''
4 4 'ipython' at argv[0]) executed through command line.
5 5 NOTE: this DEPRECATES calling ipython with multiple scripts
6 6 ("ipython a.py b.py c.py")
7
8 * iplib.py, hooks.py: Added configurable input prefilter,
9 named 'input_prefilter'. See ext_rescapture.py for example
10 usage.
11
12 * ext_rescapture.py, Magic.py: Better system command output capture
13 through 'var = !ls' (deprecates user-visible %sc). Same notation
14 applies for magics, 'var = %alias' assigns alias list to var.
15
16 * ipapi.py: added meta() for accessing extension-usable data store.
7 17
8 18 2006-01-25 Fernando Perez <Fernando.Perez@colorado.edu>
9 19
General Comments 0
You need to be logged in to leave comments. Login now