Show More
@@ -1,141 +1,195 b'' | |||||
1 | """ Leo plugin for IPython |
|
1 | """ Leo plugin for IPython | |
2 |
|
2 | |||
3 | Example use: |
|
3 | Example use: | |
4 |
|
4 | |||
5 | nodes.foo = "hello world" |
|
5 | nodes.foo = "hello world" | |
6 |
|
6 | |||
7 | -> create '@ipy foo' node with text "hello world" |
|
7 | -> create '@ipy foo' node with text "hello world" | |
8 |
|
8 | |||
9 | Access works also, and so does tab completion. |
|
9 | Access works also, and so does tab completion. | |
10 |
|
10 | |||
11 | """ |
|
11 | """ | |
12 | import IPython.ipapi |
|
12 | import IPython.ipapi | |
13 | import IPython.genutils |
|
13 | import IPython.genutils | |
14 | import IPython.generics |
|
14 | import IPython.generics | |
15 | import re |
|
15 | import re | |
16 |
|
16 | |||
17 |
|
17 | |||
18 |
|
18 | |||
19 | ip = IPython.ipapi.get() |
|
19 | ip = IPython.ipapi.get() | |
20 | leo = ip.user_ns['leox'] |
|
20 | leo = ip.user_ns['leox'] | |
21 | c,g = leo.c, leo.g |
|
21 | c,g = leo.c, leo.g | |
22 |
|
22 | |||
23 | # will probably be overwritten by user, but handy for experimentation early on |
|
23 | # will probably be overwritten by user, but handy for experimentation early on | |
24 | ip.user_ns['c'] = c |
|
24 | ip.user_ns['c'] = c | |
25 | ip.user_ns['g'] = g |
|
25 | ip.user_ns['g'] = g | |
26 |
|
26 | |||
27 |
|
27 | |||
28 | from IPython.external.simplegeneric import generic |
|
28 | from IPython.external.simplegeneric import generic | |
29 | import pprint |
|
29 | import pprint | |
30 |
|
30 | |||
31 | @generic |
|
31 | @generic | |
32 | def format_for_leo(obj): |
|
32 | def format_for_leo(obj): | |
33 | """ Convert obj to string representiation (for editing in Leo)""" |
|
33 | """ Convert obj to string representiation (for editing in Leo)""" | |
34 | return pprint.pformat(obj) |
|
34 | return pprint.pformat(obj) | |
35 |
|
35 | |||
36 | @format_for_leo.when_type(list) |
|
36 | @format_for_leo.when_type(list) | |
37 | def format_list(obj): |
|
37 | def format_list(obj): | |
38 | return "\n".join(str(s) for s in obj) |
|
38 | return "\n".join(str(s) for s in obj) | |
39 |
|
39 | |||
40 | nodename_re = r'(@ipy?[\w-]+)?\s?(\w+)' |
|
40 | nodename_re = r'(@ipy?[\w-]+)?\s?(\w+)' | |
41 |
|
41 | |||
42 | def all_cells(): |
|
42 | def all_cells(): | |
43 | d = {} |
|
43 | d = {} | |
44 | for p in c.allNodes_iter(): |
|
44 | for p in c.allNodes_iter(): | |
45 | h = p.headString() |
|
45 | h = p.headString() | |
46 | if h.startswith('@') and len(h.split()) == 1: |
|
46 | if h.startswith('@') and len(h.split()) == 1: | |
47 | continue |
|
47 | continue | |
48 | mo = re.match(nodename_re, h) |
|
48 | mo = re.match(nodename_re, h) | |
49 | if not mo: |
|
49 | if not mo: | |
50 | continue |
|
50 | continue | |
51 | d[mo.group(2)] = p.copy() |
|
51 | d[mo.group(2)] = p.copy() | |
52 | return d |
|
52 | return d | |
53 |
|
53 | |||
54 |
|
54 | |||
55 | class LeoWorkbook: |
|
55 | class TrivialLeoWorkbook: | |
56 | """ class to find cells """ |
|
56 | """ class to find cells """ | |
57 | def __getattr__(self, key): |
|
57 | def __getattr__(self, key): | |
58 | cells = all_cells() |
|
58 | cells = all_cells() | |
59 | p = cells[key] |
|
59 | p = cells[key] | |
60 | body = p.bodyString() |
|
60 | body = p.bodyString() | |
61 | return eval_body(body) |
|
61 | return eval_body(body) | |
62 | def __setattr__(self,key,val): |
|
62 | def __setattr__(self,key,val): | |
63 | cells = all_cells() |
|
63 | cells = all_cells() | |
64 | p = cells.get(key,None) |
|
64 | p = cells.get(key,None) | |
65 | if p is None: |
|
65 | if p is None: | |
66 | add_var(key,val) |
|
66 | add_var(key,val) | |
67 | else: |
|
67 | else: | |
68 | c.setBodyString(p,format_for_leo(val)) |
|
68 | c.setBodyString(p,format_for_leo(val)) | |
69 | def __str__(self): |
|
69 | def __str__(self): | |
|
70 | return "<TrivialLeoWorkbook>" | |||
|
71 | __repr__ = __str__ | |||
|
72 | ||||
|
73 | ip.user_ns['nodes'] = TrivialLeoWorkbook() | |||
|
74 | ||||
|
75 | ||||
|
76 | class LeoNode(object): | |||
|
77 | def __init__(self,p): | |||
|
78 | self.p = p.copy() | |||
|
79 | ||||
|
80 | def get_h(self): return self.p.headString() | |||
|
81 | def set_h(self,val): | |||
|
82 | print "set head",val | |||
|
83 | c.setHeadString(self.p,val) | |||
|
84 | ||||
|
85 | h = property( get_h, set_h) | |||
|
86 | ||||
|
87 | def get_b(self): return self.p.bodyString() | |||
|
88 | def set_b(self,val): | |||
|
89 | print "set body",val | |||
|
90 | c.setBodyString(self.p, val) | |||
|
91 | ||||
|
92 | b = property(get_b, set_b) | |||
|
93 | ||||
|
94 | def set_val(self, val): | |||
|
95 | self.b = pprint.pformat(val) | |||
|
96 | ||||
|
97 | val = property(lambda self: ip.ev(self.b.strip()), set_val) | |||
|
98 | ||||
|
99 | def set_l(self,val): | |||
|
100 | self.b = '\n'.join(val ) | |||
|
101 | l = property(lambda self : IPython.genutils.SList(self.b.splitlines()), | |||
|
102 | set_l) | |||
|
103 | ||||
|
104 | def __iter__(self): | |||
|
105 | return (LeoNode(p) for p in self.p.children_iter()) | |||
|
106 | ||||
|
107 | ||||
|
108 | class LeoWorkbook: | |||
|
109 | """ class for 'advanced' node access """ | |||
|
110 | def __getattr__(self, key): | |||
|
111 | if key.startswith('_') or key == 'trait_names': | |||
|
112 | raise AttributeError | |||
|
113 | cells = all_cells() | |||
|
114 | p = cells.get(key, None) | |||
|
115 | if p is None: | |||
|
116 | p = add_var(key,None) | |||
|
117 | ||||
|
118 | return LeoNode(p) | |||
|
119 | ||||
|
120 | def __str__(self): | |||
70 | return "<LeoWorkbook>" |
|
121 | return "<LeoWorkbook>" | |
71 | __repr__ = __str__ |
|
122 | __repr__ = __str__ | |
72 |
ip.user_ns[' |
|
123 | ip.user_ns['wb'] = LeoWorkbook() | |
|
124 | ||||
73 |
|
125 | |||
74 | _dummyval = object() |
|
126 | _dummyval = object() | |
75 | @IPython.generics.complete_object.when_type(LeoWorkbook) |
|
127 | @IPython.generics.complete_object.when_type(LeoWorkbook) | |
76 | def workbook_complete(obj, prev): |
|
128 | def workbook_complete(obj, prev): | |
77 | return all_cells().keys() |
|
129 | return all_cells().keys() | |
78 |
|
130 | |||
79 |
|
131 | |||
80 | def add_var(varname, value = _dummyval): |
|
132 | def add_var(varname, value = _dummyval): | |
81 | nodename = '@ipy-var ' + varname |
|
133 | nodename = '@ipy-var ' + varname | |
82 | p2 = g.findNodeAnywhere(c,nodename) |
|
134 | p2 = g.findNodeAnywhere(c,nodename) | |
83 | if not c.positionExists(p2): |
|
135 | if not c.positionExists(p2): | |
84 | p2 = c.currentPosition().insertAfter() |
|
136 | p2 = c.currentPosition().insertAfter() | |
85 | c.setHeadString(p2,'@ipy ' + varname) |
|
137 | c.setHeadString(p2,'@ipy ' + varname) | |
86 |
|
138 | |||
87 | c.setCurrentPosition(p2) |
|
139 | c.setCurrentPosition(p2) | |
88 | if value is _dummyval: |
|
140 | if value is _dummyval: | |
89 | val = ip.user_ns[varname] |
|
141 | val = ip.user_ns[varname] | |
90 | else: |
|
142 | else: | |
91 | val = value |
|
143 | val = value | |
92 | formatted = format_for_leo(val) |
|
144 | if val is not None: | |
93 | c.setBodyString(p2,formatted) |
|
145 | formatted = format_for_leo(val) | |
|
146 | c.setBodyString(p2,formatted) | |||
|
147 | return p2 | |||
94 |
|
148 | |||
95 | def add_file(self,fname): |
|
149 | def add_file(self,fname): | |
96 | p2 = c.currentPosition().insertAfter() |
|
150 | p2 = c.currentPosition().insertAfter() | |
97 |
|
151 | |||
98 | def push_script(p): |
|
152 | def push_script(p): | |
99 | script = g.getScript(c,p,useSelectedText=False,forcePythonSentinels=True,useSentinels=True) |
|
153 | script = g.getScript(c,p,useSelectedText=False,forcePythonSentinels=True,useSentinels=True) | |
100 | script = g.splitLines(script + '\n') |
|
154 | script = g.splitLines(script + '\n') | |
101 | script = ''.join(z for z in script if z.strip()) |
|
155 | script = ''.join(z for z in script if z.strip()) | |
102 | ip.runlines(script) |
|
156 | ip.runlines(script) | |
103 | print "- Script end -" |
|
157 | print "- Script end -" | |
104 |
|
158 | |||
105 | def eval_body(body): |
|
159 | def eval_body(body): | |
106 | try: |
|
160 | try: | |
107 | val = ip.ev(body) |
|
161 | val = ip.ev(body) | |
108 | except: |
|
162 | except: | |
109 | # just use stringlist if it's not completely legal python expression |
|
163 | # just use stringlist if it's not completely legal python expression | |
110 | val = IPython.genutils.SList(body.splitlines()) |
|
164 | val = IPython.genutils.SList(body.splitlines()) | |
111 | return val |
|
165 | return val | |
112 |
|
166 | |||
113 | def push_variable(p,varname): |
|
167 | def push_variable(p,varname): | |
114 | body = p.bodyString() |
|
168 | body = p.bodyString() | |
115 | val = eval_body(body.strip()) |
|
169 | val = eval_body(body.strip()) | |
116 | ip.user_ns[varname] = val |
|
170 | ip.user_ns[varname] = val | |
117 |
|
171 | |||
118 | def push_from_leo(p): |
|
172 | def push_from_leo(p): | |
119 | # headstring without @ are just scripts |
|
173 | # headstring without @ are just scripts | |
120 | if not p.headString().startswith('@'): |
|
174 | if not p.headString().startswith('@'): | |
121 | push_script(p) |
|
175 | push_script(p) | |
122 | return |
|
176 | return | |
123 | tup = p.headString().split(None,1) |
|
177 | tup = p.headString().split(None,1) | |
124 | # @ipy foo is variable foo |
|
178 | # @ipy foo is variable foo | |
125 | if len(tup) == 2 and tup[0] == '@ipy': |
|
179 | if len(tup) == 2 and tup[0] == '@ipy': | |
126 | varname = tup[1] |
|
180 | varname = tup[1] | |
127 | push_variable(p,varname) |
|
181 | push_variable(p,varname) | |
128 | return |
|
182 | return | |
129 |
|
183 | |||
130 | ip.user_ns['leox'].push = push_from_leo |
|
184 | ip.user_ns['leox'].push = push_from_leo | |
131 |
|
185 | |||
132 | def leo_f(self,s): |
|
186 | def leo_f(self,s): | |
133 | ip = self.getapi() |
|
187 | ip = self.getapi() | |
134 | s = s.strip() |
|
188 | s = s.strip() | |
135 | if s in ip.user_ns: |
|
189 | if s in ip.user_ns: | |
136 | add_var(s) |
|
190 | add_var(s) | |
137 | elif os.path.isfile(s): |
|
191 | elif os.path.isfile(s): | |
138 | # todo open file |
|
192 | # todo open file | |
139 | pass |
|
193 | pass | |
140 |
|
194 | |||
141 | ip.expose_magic('leo',leo_f) |
|
195 | ip.expose_magic('leo',leo_f) |
General Comments 0
You need to be logged in to leave comments.
Login now