##// END OF EJS Templates
var = !cmd is no longer verbose
vivainio -
Show More
@@ -1,66 +1,66 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """ IPython extension: new prefilters for output grabbing
2 """ IPython extension: new prefilters for output grabbing
3
3
4 Provides
4 Provides
5
5
6 var = %magic blah blah
6 var = %magic blah blah
7
7
8 var = !ls
8 var = !ls
9
9
10 $Id: genutils.py 1077 2006-01-24 18:15:27Z vivainio $
10 $Id: genutils.py 1077 2006-01-24 18:15:27Z vivainio $
11
11
12 """
12 """
13
13
14 import IPython.ipapi
14 import IPython.ipapi
15 from IPython.genutils import *
15 from IPython.genutils import *
16
16
17 ip = IPython.ipapi.get()
17 ip = IPython.ipapi.get()
18
18
19 import re
19 import re
20
20
21 def hnd_magic(line,mo):
21 def hnd_magic(line,mo):
22 """ Handle a = %mymagic blah blah """
22 """ Handle a = %mymagic blah blah """
23 #cmd = genutils.make_quoted_expr(mo.group('syscmd'))
23 #cmd = genutils.make_quoted_expr(mo.group('syscmd'))
24 #mag = 'ipmagic
24 #mag = 'ipmagic
25 #return "%s = %s"
25 #return "%s = %s"
26 var = mo.group('varname')
26 var = mo.group('varname')
27 cmd = mo.group('cmd')
27 cmd = mo.group('cmd')
28 expr = make_quoted_expr(cmd)
28 expr = make_quoted_expr(cmd)
29 return itpl('$var = _ip.magic($expr)')
29 return itpl('$var = _ip.magic($expr)')
30
30
31 def hnd_syscmd(line,mo):
31 def hnd_syscmd(line,mo):
32 """ Handle a = !ls """
32 """ Handle a = !ls """
33 #cmd = genutils.make_quoted_expr(mo.group('syscmd'))
33 #cmd = genutils.make_quoted_expr(mo.group('syscmd'))
34 #mag = 'ipmagic
34 #mag = 'ipmagic
35 #return "%s = %s"
35 #return "%s = %s"
36 var = mo.group('varname')
36 var = mo.group('varname')
37 cmd = mo.group('cmd')
37 cmd = mo.group('cmd')
38 expr = make_quoted_expr(itpl("sc -lv =$cmd"))
38 expr = make_quoted_expr(itpl("sc -l =$cmd"))
39 return itpl('$var = _ip.magic($expr)')
39 return itpl('$var = _ip.magic($expr)')
40
40
41 def install_re_handler(pat, hnd):
41 def install_re_handler(pat, hnd):
42 ip.meta.re_prefilters.append((re.compile(pat), hnd))
42 ip.meta.re_prefilters.append((re.compile(pat), hnd))
43
43
44 def init_handlers():
44 def init_handlers():
45
45
46 ip.meta.re_prefilters = []
46 ip.meta.re_prefilters = []
47
47
48 install_re_handler('(?P<varname>[\w\.]+)\s*=\s*%(?P<cmd>.*)',
48 install_re_handler('(?P<varname>[\w\.]+)\s*=\s*%(?P<cmd>.*)',
49 hnd_magic
49 hnd_magic
50 )
50 )
51
51
52 install_re_handler('(?P<varname>[\w\.]+)\s*=\s*!(?P<cmd>.*)',
52 install_re_handler('(?P<varname>[\w\.]+)\s*=\s*!(?P<cmd>.*)',
53 hnd_syscmd
53 hnd_syscmd
54 )
54 )
55
55
56 init_handlers()
56 init_handlers()
57
57
58 def regex_prefilter_f(self,line):
58 def regex_prefilter_f(self,line):
59 for pat, handler in ip.meta.re_prefilters:
59 for pat, handler in ip.meta.re_prefilters:
60 mo = pat.match(line)
60 mo = pat.match(line)
61 if mo:
61 if mo:
62 return handler(line,mo)
62 return handler(line,mo)
63
63
64 raise IPython.ipapi.TryNext
64 raise IPython.ipapi.TryNext
65
65
66 ip.set_hook('input_prefilter', regex_prefilter_f)
66 ip.set_hook('input_prefilter', regex_prefilter_f)
General Comments 0
You need to be logged in to leave comments. Login now