##// END OF EJS Templates
ipy_leo: handle @ipy varname on leox.push()
vivainio -
Show More
@@ -1,68 +1,80 b''
1 1 import IPython.ipapi
2 import IPython.genutils
3
2 4 ip = IPython.ipapi.get()
5 leo = ip.user_ns['leox']
6 c,g = leo.c, leo.g
3 7
4 8 from IPython.external.simplegeneric import generic
5 9 import pprint
6 10
7 11 @generic
8 12 def format_for_leo(obj):
9 13 """ Convert obj to string representiation (for editing in Leo)"""
10 14 return pprint.pformat(obj)
11 15
12 16 @format_for_leo.when_type(list)
13 17 def format_list(obj):
14 18 return '@ipy-type list\n' + "\n".join(str(s) for s in obj)
15 19
16 20
17 21 def add_var(self,varname):
18 ip = self.getapi()
19 leo = ip.user_ns['leox']
20 c,g = leo.c, leo.g
21 22 nodename = '@ipy-var ' + varname
22 23 p2 = g.findNodeAnywhere(c,nodename)
23 24 if not c.positionExists(p2):
24 25 p2 = c.currentPosition().insertAfter()
25 26 c.setHeadString(p2,'@ipy-var ' + varname)
26 27
27 28 c.setCurrentPosition(p2)
28 29 val = ip.user_ns[varname]
29 30 formatted = format_for_leo(val)
30 31 c.setBodyString(p2,formatted)
31 32
32 33 def add_file(self,fname):
33 ip = self.getapi()
34 leo = ip.user_ns['leox']
35 c,g = leo.c, leo.g
36 34 p2 = c.currentPosition().insertAfter()
37 35
38 def push_from_leo(p):
39 print "Pushed from leo",p
40 leo = ip.user_ns['leox']
41 c,g = leo.c, leo.g
42
36 def push_script(p):
43 37 script = g.getScript(c,p,useSelectedText=False,forcePythonSentinels=True,useSentinels=True)
44 38 script = g.splitLines(script + '\n')
45 39 script = ''.join(z for z in script if z.strip())
46 40 ip.runlines(script)
47 ip.user_ns['leox'].push = push_from_leo
48
41 print "- Script end -"
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 71 def leo_f(self,s):
52 72 ip = self.getapi()
53 73 s = s.strip()
54 74 if s in ip.user_ns:
55 75 add_var(self,s)
56 76 elif os.path.isfile(s):
57 77 # todo open file
58 78 pass
59 79
60 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