Show More
@@ -1,46 +1,68 b'' | |||
|
1 | 1 | import IPython.ipapi |
|
2 | 2 | ip = IPython.ipapi.get() |
|
3 | 3 | |
|
4 | 4 | from IPython.external.simplegeneric import generic |
|
5 | 5 | import pprint |
|
6 | 6 | |
|
7 | 7 | @generic |
|
8 | 8 | def format_for_leo(obj): |
|
9 | 9 | """ Convert obj to string representiation (for editing in Leo)""" |
|
10 | 10 | return pprint.pformat(obj) |
|
11 | 11 | |
|
12 | 12 | @format_for_leo.when_type(list) |
|
13 | 13 | def format_list(obj): |
|
14 | return "\n".join(str(s) for s in obj) | |
|
14 | return '@ipy-type list\n' + "\n".join(str(s) for s in obj) | |
|
15 | 15 | |
|
16 | 16 | |
|
17 | 17 | def add_var(self,varname): |
|
18 | 18 | ip = self.getapi() |
|
19 | 19 | leo = ip.user_ns['leox'] |
|
20 | 20 | c,g = leo.c, leo.g |
|
21 | 21 | nodename = '@ipy-var ' + varname |
|
22 | 22 | p2 = g.findNodeAnywhere(c,nodename) |
|
23 | 23 | if not c.positionExists(p2): |
|
24 | 24 | p2 = c.currentPosition().insertAfter() |
|
25 | 25 | c.setHeadString(p2,'@ipy-var ' + varname) |
|
26 | 26 | |
|
27 | 27 | c.setCurrentPosition(p2) |
|
28 | 28 | val = ip.user_ns[varname] |
|
29 | 29 | formatted = format_for_leo(val) |
|
30 | 30 | c.setBodyString(p2,formatted) |
|
31 | 31 | |
|
32 | 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() | |
|
37 | ||
|
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 | ||
|
43 | script = g.getScript(c,p,useSelectedText=False,forcePythonSentinels=True,useSentinels=True) | |
|
44 | script = g.splitLines(script + '\n') | |
|
45 | script = ''.join(z for z in script if z.strip()) | |
|
46 | ip.runlines(script) | |
|
47 | ip.user_ns['leox'].push = push_from_leo | |
|
48 | ||
|
49 | ||
|
50 | ||
|
32 | 51 | def leo_f(self,s): |
|
33 | 52 | ip = self.getapi() |
|
34 | 53 | s = s.strip() |
|
35 | 54 | if s in ip.user_ns: |
|
36 | 55 | add_var(self,s) |
|
56 | elif os.path.isfile(s): | |
|
57 | # todo open file | |
|
58 | pass | |
|
37 | 59 | |
|
38 | 60 | ip.expose_magic('leo',leo_f) |
|
39 | 61 | |
|
40 | 62 | |
|
41 | 63 | |
|
42 | 64 | |
|
43 | 65 | |
|
44 | 66 | |
|
45 | 67 | |
|
46 | 68 |
General Comments 0
You need to be logged in to leave comments.
Login now