diff --git a/IPython/Extensions/ipy_leo.py b/IPython/Extensions/ipy_leo.py index c2d72a9..5a18a11 100644 --- a/IPython/Extensions/ipy_leo.py +++ b/IPython/Extensions/ipy_leo.py @@ -1,5 +1,9 @@ import IPython.ipapi +import IPython.genutils + ip = IPython.ipapi.get() +leo = ip.user_ns['leox'] +c,g = leo.c, leo.g from IPython.external.simplegeneric import generic import pprint @@ -15,9 +19,6 @@ def format_list(obj): def add_var(self,varname): - ip = self.getapi() - leo = ip.user_ns['leox'] - c,g = leo.c, leo.g nodename = '@ipy-var ' + varname p2 = g.findNodeAnywhere(c,nodename) if not c.positionExists(p2): @@ -30,23 +31,42 @@ def add_var(self,varname): c.setBodyString(p2,formatted) def add_file(self,fname): - ip = self.getapi() - leo = ip.user_ns['leox'] - c,g = leo.c, leo.g p2 = c.currentPosition().insertAfter() -def push_from_leo(p): - print "Pushed from leo",p - leo = ip.user_ns['leox'] - c,g = leo.c, leo.g - +def push_script(p): script = g.getScript(c,p,useSelectedText=False,forcePythonSentinels=True,useSentinels=True) script = g.splitLines(script + '\n') script = ''.join(z for z in script if z.strip()) ip.runlines(script) -ip.user_ns['leox'].push = push_from_leo - + print "- Script end -" + +def eval_body(body): + print "eval",body + try: + val = ip.ev(body) + except: + # just use stringlist if it's not completely legal python expression + val = IPython.genutils.SList(body.splitlines()) + return val + +def push_variable(p,varname): + body = p.bodyString() + val = eval_body(body.strip()) + ip.user_ns[varname] = val + +def push_from_leo(p): + # headstring without @ are just scripts + if not p.headString().startswith('@'): + push_script(p) + return + tup = p.headString().split(None,1) + # @ipy foo is variable foo + if len(tup) == 2 and tup[0] == '@ipy': + varname = tup[1] + push_variable(p,varname) + return +ip.user_ns['leox'].push = push_from_leo def leo_f(self,s): ip = self.getapi() @@ -58,11 +78,3 @@ def leo_f(self,s): pass ip.expose_magic('leo',leo_f) - - - - - - - -