##// 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 import sys
15 import sys
16
16
17 if sys.version_info >= (2,4):
17 import ext_rehashdir # %rehashdir magic
18 # rehashdir extension requires python 2.4
18 import ext_rescapture # var = !ls and var = %magic
19 import ext_rehashdir No newline at end of file
@@ -1,7 +1,7 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Magic functions for InteractiveShell.
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 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
@@ -2171,14 +2171,15 b' Defaulting color scheme to \'NoColor\'"""'
2171 prechar = ''
2171 prechar = ''
2172 else:
2172 else:
2173 prechar = self.shell.ESC_MAGIC
2173 prechar = self.shell.ESC_MAGIC
2174 print 'Alias\t\tSystem Command\n'+'-'*30
2174 #print 'Alias\t\tSystem Command\n'+'-'*30
2175 atab = self.shell.alias_table
2175 atab = self.shell.alias_table
2176 aliases = atab.keys()
2176 aliases = atab.keys()
2177 aliases.sort()
2177 aliases.sort()
2178 res = []
2178 for alias in aliases:
2179 for alias in aliases:
2179 print prechar+alias+'\t\t'+atab[alias][1]
2180 res.append((alias, atab[alias][1]))
2180 print '-'*30+'\nTotal number of aliases:',len(aliases)
2181 print "Total number of aliases:",len(aliases)
2181 return
2182 return res
2182 try:
2183 try:
2183 alias,cmd = par.split(None,1)
2184 alias,cmd = par.split(None,1)
2184 except:
2185 except:
@@ -2443,6 +2444,18 b' Defaulting color scheme to \'NoColor\'"""'
2443 def magic_sc(self, parameter_s=''):
2444 def magic_sc(self, parameter_s=''):
2444 """Shell capture - execute a shell command and capture its output.
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 %sc [options] varname=command
2459 %sc [options] varname=command
2447
2460
2448 IPython will run the given command using commands.getoutput(), and
2461 IPython will run the given command using commands.getoutput(), and
@@ -2453,6 +2466,8 b' Defaulting color scheme to \'NoColor\'"""'
2453 The '=' sign in the syntax is mandatory, and the variable name you
2466 The '=' sign in the syntax is mandatory, and the variable name you
2454 supply must follow Python's standard conventions for valid names.
2467 supply must follow Python's standard conventions for valid names.
2455
2468
2469 (A special format without variable name exists for internal use)
2470
2456 Options:
2471 Options:
2457
2472
2458 -l: list output. Split the output on newlines into a list before
2473 -l: list output. Split the output on newlines into a list before
@@ -2530,9 +2545,6 b' Defaulting color scheme to \'NoColor\'"""'
2530 _,cmd = parameter_s.split('=',1)
2545 _,cmd = parameter_s.split('=',1)
2531 except ValueError:
2546 except ValueError:
2532 var,cmd = '',''
2547 var,cmd = '',''
2533 if not var:
2534 error('you must specify a variable to assign the command to.')
2535 return
2536 # If all looks ok, proceed
2548 # If all looks ok, proceed
2537 out,err = self.shell.getoutputerror(cmd)
2549 out,err = self.shell.getoutputerror(cmd)
2538 if err:
2550 if err:
@@ -2543,7 +2555,10 b' Defaulting color scheme to \'NoColor\'"""'
2543 out = LSString(out)
2555 out = LSString(out)
2544 if opts.has_key('v'):
2556 if opts.has_key('v'):
2545 print '%s ==\n%s' % (var,pformat(out))
2557 print '%s ==\n%s' % (var,pformat(out))
2558 if var:
2546 self.shell.user_ns.update({var:out})
2559 self.shell.user_ns.update({var:out})
2560 else:
2561 return out
2547
2562
2548 def magic_sx(self, parameter_s=''):
2563 def magic_sx(self, parameter_s=''):
2549 """Shell execute - run a shell command and capture its output.
2564 """Shell execute - run a shell command and capture its output.
@@ -141,6 +141,15 b' class IPApi:'
141 Returns the result of evaluation"""
141 Returns the result of evaluation"""
142 return eval(expr,self.user_ns())
142 return eval(expr,self.user_ns())
143
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
152
144 def launch_new_instance(user_ns = None):
153 def launch_new_instance(user_ns = None):
145 """ Create and start a new ipython instance.
154 """ Create and start a new ipython instance.
146
155
@@ -6,7 +6,7 b' Requires Python 2.3 or newer.'
6
6
7 This file contains all the classes and helper functions specific to IPython.
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 # convenient location for storing additional information and state
242 # convenient location for storing additional information and state
243 # their extensions may require, without fear of collisions with other
243 # their extensions may require, without fear of collisions with other
244 # ipython names that may develop later.
244 # ipython names that may develop later.
245 self.meta = Bunch()
245 self.meta = Struct()
246
246
247 # Create the namespace where the user will operate. user_ns is
247 # Create the namespace where the user will operate. user_ns is
248 # normally the only one used, and it is passed to the exec calls as
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 # accepts it. Probably at least check that the hook takes the number
724 # accepts it. Probably at least check that the hook takes the number
725 # of args it's supposed to.
725 # of args it's supposed to.
726 dp = getattr(self.hooks, name, None)
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 if not dp:
729 if not dp:
728 dp = IPython.hooks.CommandChainDispatcher()
730 dp = IPython.hooks.CommandChainDispatcher()
729
731
@@ -5,6 +5,16 b''
5 NOTE: this DEPRECATES calling ipython with multiple scripts
5 NOTE: this DEPRECATES calling ipython with multiple scripts
6 ("ipython a.py b.py c.py")
6 ("ipython a.py b.py c.py")
7
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.
17
8 2006-01-25 Fernando Perez <Fernando.Perez@colorado.edu>
18 2006-01-25 Fernando Perez <Fernando.Perez@colorado.edu>
9
19
10 * IPython/demo.py (Demo.show): Flush stdout after each block, so
20 * IPython/demo.py (Demo.show): Flush stdout after each block, so
General Comments 0
You need to be logged in to leave comments. Login now