Show More
@@ -1,68 +1,80 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 | |
6 |
|
10 | |||
7 | @generic |
|
11 | @generic | |
8 | def format_for_leo(obj): |
|
12 | def format_for_leo(obj): | |
9 | """ Convert obj to string representiation (for editing in Leo)""" |
|
13 | """ Convert obj to string representiation (for editing in Leo)""" | |
10 | return pprint.pformat(obj) |
|
14 | return pprint.pformat(obj) | |
11 |
|
15 | |||
12 | @format_for_leo.when_type(list) |
|
16 | @format_for_leo.when_type(list) | |
13 | def format_list(obj): |
|
17 | def format_list(obj): | |
14 | return '@ipy-type list\n' + "\n".join(str(s) for s in obj) |
|
18 | return '@ipy-type list\n' + "\n".join(str(s) for s in 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): | |
24 | p2 = c.currentPosition().insertAfter() |
|
25 | p2 = c.currentPosition().insertAfter() | |
25 | c.setHeadString(p2,'@ipy-var ' + varname) |
|
26 | c.setHeadString(p2,'@ipy-var ' + varname) | |
26 |
|
27 | |||
27 | c.setCurrentPosition(p2) |
|
28 | c.setCurrentPosition(p2) | |
28 | val = ip.user_ns[varname] |
|
29 | val = ip.user_ns[varname] | |
29 | formatted = format_for_leo(val) |
|
30 | formatted = format_for_leo(val) | |
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_ |
|
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() | |
53 | s = s.strip() |
|
73 | s = s.strip() | |
54 | if s in ip.user_ns: |
|
74 | if s in ip.user_ns: | |
55 | add_var(self,s) |
|
75 | add_var(self,s) | |
56 | elif os.path.isfile(s): |
|
76 | elif os.path.isfile(s): | |
57 | # todo open file |
|
77 | # todo open file | |
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