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