##// END OF EJS Templates
add leo_bridge_demo example, add @cl (data class) support (e.g. @cl csvdata, @cl tmpfile, @cl rfile)
Ville M. Vainio -
Show More
@@ -0,0 +1,76 b''
1 <?xml version="1.0" encoding="utf-8"?>
2 <?xml-stylesheet ekr_test?>
3 <leo_file>
4 <leo_header file_format="2" tnodes="0" max_tnode_index="0" clone_windows="0"/>
5 <globals body_outline_ratio="0.5">
6 <global_window_position top="263" left="521" height="600" width="800"/>
7 <global_log_window_position top="0" left="0" height="0" width="0"/>
8 </globals>
9 <preferences/>
10 <find_panel_settings/>
11 <vnodes>
12 <v t="vivainio.20080218184525"><vh>@chapters</vh></v>
13 <v t="vivainio.20080218184540" a="E"><vh>@ipy-startup</vh>
14 <v t="vivainio.20080218184613"><vh>a</vh></v>
15 <v t="vivainio.20080218184613.1"><vh>b</vh></v>
16 <v t="vivainio.20080218200031" a="E"><vh>Some classes</vh>
17 <v t="vivainio.20080218190816"><vh>File-like access</vh></v>
18 <v t="vivainio.20080218200106" a="TV"><vh>csv data</vh></v>
19 </v>
20 </v>
21 <v t="vivainio.20080218195413" a="E"><vh>Class tests</vh>
22 <v t="vivainio.20080218200509"><vh>csvr</vh></v>
23 <v t="vivainio.20080218191007"><vh>tempfile</vh></v>
24 <v t="vivainio.20080218195413.1"><vh>rfile</vh></v>
25 </v>
26 </vnodes>
27 <tnodes>
28 <t tx="vivainio.20080218184525"></t>
29 <t tx="vivainio.20080218184540"># this stuff will be pushed at ipython bridge startup
30
31 @others</t>
32 <t tx="vivainio.20080218184613">print "hello"</t>
33 <t tx="vivainio.20080218184613.1">print "world"</t>
34 <t tx="vivainio.20080218190816">def rfile(body):
35 """ @cl rfile
36
37 produces a StringIO (file like obj of the rest of the body) """
38
39 import StringIO
40 return StringIO.StringIO(body)
41
42 def tmpfile(body):
43 """ @cl tmpfile
44
45 Produces a temporary file, with node body as contents
46
47 """
48 import tempfile
49 h, fname = tempfile.mkstemp()
50 f = open(fname,'w')
51 f.write(body)
52 f.close()
53 return fname
54 </t>
55 <t tx="vivainio.20080218191007">@cl tmpfile
56
57 Hello</t>
58 <t tx="vivainio.20080218195413"></t>
59 <t tx="vivainio.20080218195413.1">@cl rfile
60 These
61 lines
62 should
63 be
64 readable </t>
65 <t tx="vivainio.20080218200031"></t>
66 <t tx="vivainio.20080218200106">def csvdata(body):
67 import csv
68 d = csv.Sniffer().sniff(body)
69 reader = csv.reader(body.splitlines(), dialect = d)
70 return reader</t>
71 <t tx="vivainio.20080218200509">@cl csvdata
72
73 a,b,b
74 1,2,2</t>
75 </tnodes>
76 </leo_file>
@@ -28,6 +28,10 b" ip.user_ns['g'] = g"
28 from IPython.external.simplegeneric import generic
28 from IPython.external.simplegeneric import generic
29 import pprint
29 import pprint
30
30
31 def es(s):
32 g.es(s, tabName = 'IPython')
33 pass
34
31 @generic
35 @generic
32 def format_for_leo(obj):
36 def format_for_leo(obj):
33 """ Convert obj to string representiation (for editing in Leo)"""
37 """ Convert obj to string representiation (for editing in Leo)"""
@@ -74,6 +78,21 b' class TrivialLeoWorkbook:'
74
78
75 ip.user_ns['nodes'] = TrivialLeoWorkbook()
79 ip.user_ns['nodes'] = TrivialLeoWorkbook()
76
80
81 def eval_node(n):
82 body = n.b
83 if not body.startswith('@cl'):
84 # plain python repr node, just eval it
85 return ip.ev(n.b)
86 # @cl nodes deserve special treatment - first eval the first line (minus cl), then use it to call the rest of body
87 first, rest = body.split('\n',1)
88 cl, hd = first.split(None, 1)
89 if cl != '@cl':
90 return None
91 xformer = ip.ev(hd.strip())
92 es('Transform w/ %s' % repr(xformer))
93 return xformer(rest)
94
95
77
96
78 class LeoNode(object):
97 class LeoNode(object):
79 def __init__(self,p):
98 def __init__(self,p):
@@ -104,7 +123,7 b' class LeoNode(object):'
104 def set_val(self, val):
123 def set_val(self, val):
105 self.b = pprint.pformat(val)
124 self.b = pprint.pformat(val)
106
125
107 v = property(lambda self: ip.ev(self.b.strip()), set_val)
126 v = property(lambda self: eval_node(self), set_val)
108
127
109 def set_l(self,val):
128 def set_l(self,val):
110 self.b = '\n'.join(val )
129 self.b = '\n'.join(val )
@@ -184,12 +203,12 b' def push_script(p):'
184 has_output = True
203 has_output = True
185 inp = ip.IP.input_hist[idx]
204 inp = ip.IP.input_hist[idx]
186 if inp.strip():
205 if inp.strip():
187 g.es('In: %s' % (inp[:40], ), tabName = 'IPython')
206 es('In: %s' % (inp[:40], ))
188
207
189 g.es('<%d> %s' % (idx, pprint.pformat(ohist[idx],width = 40)), tabName = 'IPython')
208 es('<%d> %s' % (idx, pprint.pformat(ohist[idx],width = 40)))
190
209
191 if not has_output:
210 if not has_output:
192 g.es('ipy run: %s' %( p.headString(),), tabName = 'IPython')
211 es('ipy run: %s' %( p.headString(),))
193 finally:
212 finally:
194 c.endUpdate()
213 c.endUpdate()
195
214
@@ -206,14 +225,15 b' def push_variable(p,varname):'
206 body = p.bodyString()
225 body = p.bodyString()
207 val = eval_body(body.strip())
226 val = eval_body(body.strip())
208 ip.user_ns[varname] = val
227 ip.user_ns[varname] = val
209 g.es('ipy var: %s' % (varname,), tabName = "IPython")
228 es('ipy var: %s' % (varname,))
210
229
211 def push_plain_python(p):
230 def push_plain_python(p):
212 script = g.getScript(c,p,useSelectedText=False,forcePythonSentinels=False,useSentinels=False)
231 script = g.getScript(c,p,useSelectedText=False,forcePythonSentinels=False,useSentinels=False)
213 exec script in ip.user_ns
232 exec script in ip.user_ns
214 g.es('ipy plain: %s' % (p.headString(),), tabName = "IPython")
233 es('ipy plain: %s' % (p.headString(),))
215
234
216 def push_from_leo(p):
235 def push_from_leo(p):
236 nod = LeoNode(p)
217 h = p.headString()
237 h = p.headString()
218 tup = h.split(None,1)
238 tup = h.split(None,1)
219 # @ipy foo is variable foo
239 # @ipy foo is variable foo
@@ -224,7 +244,10 b' def push_from_leo(p):'
224 if h.endswith('P'):
244 if h.endswith('P'):
225 push_plain_python(p)
245 push_plain_python(p)
226 return
246 return
227
247 if nod.b.startswith('@cl'):
248 es(nod.v)
249 return
250
228 push_script(p)
251 push_script(p)
229 return
252 return
230
253
@@ -277,7 +300,7 b' def show_welcome():'
277 print "Welcome to Leo-enabled IPython session!"
300 print "Welcome to Leo-enabled IPython session!"
278 print "Try %leoref for quick reference."
301 print "Try %leoref for quick reference."
279 import IPython.platutils
302 import IPython.platutils
280 IPython.platutils.set_term_title('Leo IPython')
303 IPython.platutils.set_term_title('ILeo')
281 IPython.platutils.freeze_term_title()
304 IPython.platutils.freeze_term_title()
282
305
283 def run_leo_startup_node():
306 def run_leo_startup_node():
General Comments 0
You need to be logged in to leave comments. Login now