ipy_leo.py
197 lines
| 5.0 KiB
| text/x-python
|
PythonLexer
vivainio
|
r983 | """ Leo plugin for IPython | ||
Example use: | ||||
nodes.foo = "hello world" | ||||
-> create '@ipy foo' node with text "hello world" | ||||
Access works also, and so does tab completion. | ||||
""" | ||||
vivainio
|
r979 | import IPython.ipapi | ||
vivainio
|
r982 | import IPython.genutils | ||
vivainio
|
r983 | import IPython.generics | ||
import re | ||||
vivainio
|
r982 | |||
vivainio
|
r979 | ip = IPython.ipapi.get() | ||
vivainio
|
r982 | leo = ip.user_ns['leox'] | ||
c,g = leo.c, leo.g | ||||
vivainio
|
r979 | |||
vivainio
|
r983 | # will probably be overwritten by user, but handy for experimentation early on | ||
ip.user_ns['c'] = c | ||||
ip.user_ns['g'] = g | ||||
vivainio
|
r979 | from IPython.external.simplegeneric import generic | ||
vivainio
|
r980 | import pprint | ||
vivainio
|
r979 | |||
@generic | ||||
def format_for_leo(obj): | ||||
""" Convert obj to string representiation (for editing in Leo)""" | ||||
vivainio
|
r980 | return pprint.pformat(obj) | ||
vivainio
|
r979 | |||
@format_for_leo.when_type(list) | ||||
def format_list(obj): | ||||
vivainio
|
r983 | return "\n".join(str(s) for s in obj) | ||
nodename_re = r'(@ipy?[\w-]+)?\s?(\w+)' | ||||
def all_cells(): | ||||
d = {} | ||||
for p in c.allNodes_iter(): | ||||
h = p.headString() | ||||
if h.startswith('@') and len(h.split()) == 1: | ||||
continue | ||||
mo = re.match(nodename_re, h) | ||||
if not mo: | ||||
continue | ||||
d[mo.group(2)] = p.copy() | ||||
return d | ||||
vivainio
|
r984 | class TrivialLeoWorkbook: | ||
vivainio
|
r983 | """ class to find cells """ | ||
def __getattr__(self, key): | ||||
cells = all_cells() | ||||
p = cells[key] | ||||
body = p.bodyString() | ||||
return eval_body(body) | ||||
def __setattr__(self,key,val): | ||||
cells = all_cells() | ||||
p = cells.get(key,None) | ||||
if p is None: | ||||
add_var(key,val) | ||||
else: | ||||
c.setBodyString(p,format_for_leo(val)) | ||||
def __str__(self): | ||||
vivainio
|
r984 | return "<TrivialLeoWorkbook>" | ||
__repr__ = __str__ | ||||
ip.user_ns['nodes'] = TrivialLeoWorkbook() | ||||
class LeoNode(object): | ||||
def __init__(self,p): | ||||
self.p = p.copy() | ||||
def get_h(self): return self.p.headString() | ||||
def set_h(self,val): | ||||
print "set head",val | ||||
c.setHeadString(self.p,val) | ||||
h = property( get_h, set_h) | ||||
def get_b(self): return self.p.bodyString() | ||||
def set_b(self,val): | ||||
print "set body",val | ||||
c.setBodyString(self.p, val) | ||||
b = property(get_b, set_b) | ||||
def set_val(self, val): | ||||
self.b = pprint.pformat(val) | ||||
val = property(lambda self: ip.ev(self.b.strip()), set_val) | ||||
def set_l(self,val): | ||||
self.b = '\n'.join(val ) | ||||
l = property(lambda self : IPython.genutils.SList(self.b.splitlines()), | ||||
set_l) | ||||
def __iter__(self): | ||||
return (LeoNode(p) for p in self.p.children_iter()) | ||||
class LeoWorkbook: | ||||
""" class for 'advanced' node access """ | ||||
def __getattr__(self, key): | ||||
if key.startswith('_') or key == 'trait_names': | ||||
raise AttributeError | ||||
cells = all_cells() | ||||
p = cells.get(key, None) | ||||
if p is None: | ||||
p = add_var(key,None) | ||||
return LeoNode(p) | ||||
def __str__(self): | ||||
vivainio
|
r983 | return "<LeoWorkbook>" | ||
__repr__ = __str__ | ||||
vivainio
|
r984 | ip.user_ns['wb'] = LeoWorkbook() | ||
vivainio
|
r979 | |||
vivainio
|
r983 | _dummyval = object() | ||
@IPython.generics.complete_object.when_type(LeoWorkbook) | ||||
def workbook_complete(obj, prev): | ||||
return all_cells().keys() | ||||
vivainio
|
r979 | |||
vivainio
|
r983 | def add_var(varname, value = _dummyval): | ||
vivainio
|
r979 | nodename = '@ipy-var ' + varname | ||
p2 = g.findNodeAnywhere(c,nodename) | ||||
if not c.positionExists(p2): | ||||
p2 = c.currentPosition().insertAfter() | ||||
vivainio
|
r983 | c.setHeadString(p2,'@ipy ' + varname) | ||
vivainio
|
r979 | |||
c.setCurrentPosition(p2) | ||||
vivainio
|
r983 | if value is _dummyval: | ||
val = ip.user_ns[varname] | ||||
else: | ||||
val = value | ||||
vivainio
|
r984 | if val is not None: | ||
formatted = format_for_leo(val) | ||||
c.setBodyString(p2,formatted) | ||||
return p2 | ||||
vivainio
|
r979 | |||
vivainio
|
r981 | def add_file(self,fname): | ||
p2 = c.currentPosition().insertAfter() | ||||
vivainio
|
r982 | def push_script(p): | ||
vivainio
|
r985 | script = g.getScript(c,p,useSelectedText=False,forcePythonSentinels=True,useSentinels=False) | ||
vivainio
|
r981 | script = g.splitLines(script + '\n') | ||
script = ''.join(z for z in script if z.strip()) | ||||
ip.runlines(script) | ||||
vivainio
|
r985 | g.es('ipy script:',p.headString()) | ||
print " ->" | ||||
vivainio
|
r982 | |||
def eval_body(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 | ||||
vivainio
|
r985 | g.es('ipy var:',varname) | ||
vivainio
|
r982 | |||
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 | ||||
vivainio
|
r981 | |||
vivainio
|
r982 | ip.user_ns['leox'].push = push_from_leo | ||
vivainio
|
r981 | |||
vivainio
|
r979 | def leo_f(self,s): | ||
ip = self.getapi() | ||||
s = s.strip() | ||||
if s in ip.user_ns: | ||||
vivainio
|
r983 | add_var(s) | ||
vivainio
|
r981 | elif os.path.isfile(s): | ||
# todo open file | ||||
pass | ||||
vivainio
|
r979 | |||
ip.expose_magic('leo',leo_f) | ||||