##// END OF EJS Templates
ipy_leo: handle @ipy varname on leox.push()
vivainio -
Show More
@@ -1,5 +1,9 b''
1 import IPython.ipapi
1 import IPython.ipapi
2 import IPython.genutils
3
2 ip = IPython.ipapi.get()
4 ip = IPython.ipapi.get()
5 leo = ip.user_ns['leox']
6 c,g = leo.c, leo.g
3
7
4 from IPython.external.simplegeneric import generic
8 from IPython.external.simplegeneric import generic
5 import pprint
9 import pprint
@@ -15,9 +19,6 b' def format_list(obj):'
15
19
16
20
17 def add_var(self,varname):
21 def add_var(self,varname):
18 ip = self.getapi()
19 leo = ip.user_ns['leox']
20 c,g = leo.c, leo.g
21 nodename = '@ipy-var ' + varname
22 nodename = '@ipy-var ' + varname
22 p2 = g.findNodeAnywhere(c,nodename)
23 p2 = g.findNodeAnywhere(c,nodename)
23 if not c.positionExists(p2):
24 if not c.positionExists(p2):
@@ -30,23 +31,42 b' def add_var(self,varname):'
30 c.setBodyString(p2,formatted)
31 c.setBodyString(p2,formatted)
31
32
32 def add_file(self,fname):
33 def add_file(self,fname):
33 ip = self.getapi()
34 leo = ip.user_ns['leox']
35 c,g = leo.c, leo.g
36 p2 = c.currentPosition().insertAfter()
34 p2 = c.currentPosition().insertAfter()
37
35
38 def push_from_leo(p):
36 def push_script(p):
39 print "Pushed from leo",p
40 leo = ip.user_ns['leox']
41 c,g = leo.c, leo.g
42
43 script = g.getScript(c,p,useSelectedText=False,forcePythonSentinels=True,useSentinels=True)
37 script = g.getScript(c,p,useSelectedText=False,forcePythonSentinels=True,useSentinels=True)
44 script = g.splitLines(script + '\n')
38 script = g.splitLines(script + '\n')
45 script = ''.join(z for z in script if z.strip())
39 script = ''.join(z for z in script if z.strip())
46 ip.runlines(script)
40 ip.runlines(script)
47 ip.user_ns['leox'].push = push_from_leo
41 print "- Script end -"
48
42
43 def eval_body(body):
44 print "eval",body
45 try:
46 val = ip.ev(body)
47 except:
48 # just use stringlist if it's not completely legal python expression
49 val = IPython.genutils.SList(body.splitlines())
50 return val
51
52 def push_variable(p,varname):
53 body = p.bodyString()
54 val = eval_body(body.strip())
55 ip.user_ns[varname] = val
56
57 def push_from_leo(p):
58 # headstring without @ are just scripts
59 if not p.headString().startswith('@'):
60 push_script(p)
61 return
62 tup = p.headString().split(None,1)
63 # @ipy foo is variable foo
64 if len(tup) == 2 and tup[0] == '@ipy':
65 varname = tup[1]
66 push_variable(p,varname)
67 return
49
68
69 ip.user_ns['leox'].push = push_from_leo
50
70
51 def leo_f(self,s):
71 def leo_f(self,s):
52 ip = self.getapi()
72 ip = self.getapi()
@@ -58,11 +78,3 b' def leo_f(self,s):'
58 pass
78 pass
59
79
60 ip.expose_magic('leo',leo_f)
80 ip.expose_magic('leo',leo_f)
61
62
63
64
65
66
67
68
General Comments 0
You need to be logged in to leave comments. Login now