Show More
@@ -1,45 +1,46 b'' | |||||
1 | import IPython.ipapi |
|
1 | import IPython.ipapi | |
2 | ip = IPython.ipapi.get() |
|
2 | ip = IPython.ipapi.get() | |
3 |
|
3 | |||
4 | from IPython.external.simplegeneric import generic |
|
4 | from IPython.external.simplegeneric import generic | |
|
5 | import pprint | |||
5 |
|
6 | |||
6 | @generic |
|
7 | @generic | |
7 | def format_for_leo(obj): |
|
8 | def format_for_leo(obj): | |
8 | """ Convert obj to string representiation (for editing in Leo)""" |
|
9 | """ Convert obj to string representiation (for editing in Leo)""" | |
9 |
return |
|
10 | return pprint.pformat(obj) | |
10 |
|
11 | |||
11 | @format_for_leo.when_type(list) |
|
12 | @format_for_leo.when_type(list) | |
12 | def format_list(obj): |
|
13 | def format_list(obj): | |
13 | return "\n".join(str(s) for s in obj) |
|
14 | return "\n".join(str(s) for s in obj) | |
14 |
|
15 | |||
15 |
|
16 | |||
16 | def add_var(self,varname): |
|
17 | def add_var(self,varname): | |
17 | ip = self.getapi() |
|
18 | ip = self.getapi() | |
18 | leo = ip.user_ns['leox'] |
|
19 | leo = ip.user_ns['leox'] | |
19 | c,g = leo.c, leo.g |
|
20 | c,g = leo.c, leo.g | |
20 | nodename = '@ipy-var ' + varname |
|
21 | nodename = '@ipy-var ' + varname | |
21 | p2 = g.findNodeAnywhere(c,nodename) |
|
22 | p2 = g.findNodeAnywhere(c,nodename) | |
22 | if not c.positionExists(p2): |
|
23 | if not c.positionExists(p2): | |
23 | p2 = c.currentPosition().insertAfter() |
|
24 | p2 = c.currentPosition().insertAfter() | |
24 | c.setHeadString(p2,'@ipy-var ' + varname) |
|
25 | c.setHeadString(p2,'@ipy-var ' + varname) | |
25 |
|
26 | |||
26 | c.setCurrentPosition(p2) |
|
27 | c.setCurrentPosition(p2) | |
27 | val = ip.user_ns[varname] |
|
28 | val = ip.user_ns[varname] | |
28 | formatted = format_for_leo(val) |
|
29 | formatted = format_for_leo(val) | |
29 | c.setBodyString(p2,formatted) |
|
30 | c.setBodyString(p2,formatted) | |
30 |
|
31 | |||
31 | def leo_f(self,s): |
|
32 | def leo_f(self,s): | |
32 | ip = self.getapi() |
|
33 | ip = self.getapi() | |
33 | s = s.strip() |
|
34 | s = s.strip() | |
34 | if s in ip.user_ns: |
|
35 | if s in ip.user_ns: | |
35 | add_var(self,s) |
|
36 | add_var(self,s) | |
36 |
|
37 | |||
37 | ip.expose_magic('leo',leo_f) |
|
38 | ip.expose_magic('leo',leo_f) | |
38 |
|
39 | |||
39 |
|
40 | |||
40 |
|
41 | |||
41 |
|
42 | |||
42 |
|
43 | |||
43 |
|
44 | |||
44 |
|
45 | |||
45 |
|
46 |
General Comments 0
You need to be logged in to leave comments.
Login now