Skip to content

Commit

Permalink
deprecated @ipy header prefix, use @cl alone as first body line instead
Browse files Browse the repository at this point in the history
introduce DictMixin stuff for LeoNode for dict-like child access
  • Loading branch information
vivainio committed Feb 18, 2008
1 parent 9d2adcd commit 48ae66e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 57 deletions.
103 changes: 60 additions & 43 deletions IPython/Extensions/ipy_leo.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
""" Leo plugin for IPython
""" ILeo - 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.
"""
import IPython.ipapi
import IPython.genutils
import IPython.generics
import re

import UserDict


ip = IPython.ipapi.get()
Expand Down Expand Up @@ -41,42 +34,20 @@ def format_for_leo(obj):
def format_list(obj):
return "\n".join(str(s) for s in obj)

nodename_re = r'^(@ipy\w*\s+)?(\w+)$'
attribute_re = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$')
def valid_attribute(s):
return attribute_re.match(s)

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()
if not valid_attribute(h):
continue
d[h] = p.copy()
return d


class TrivialLeoWorkbook:
""" class to find cells with simple syntax
"""
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):
return "<TrivialLeoWorkbook>"
__repr__ = __str__

ip.user_ns['nodes'] = TrivialLeoWorkbook()

def eval_node(n):
body = n.b
Expand All @@ -85,14 +56,21 @@ def eval_node(n):
return ip.ev(n.b)
# @cl nodes deserve special treatment - first eval the first line (minus cl), then use it to call the rest of body
first, rest = body.split('\n',1)
cl, hd = first.split(None, 1)
if cl != '@cl':
return None
tup = first.split(None, 1)
# @cl alone SPECIAL USE-> dump var to user_ns
if len(tup) == 1:
val = ip.ev(rest)
ip.user_ns[n.h] = val
es("%s = %s" % (n.h, repr(val)[:20] ))
return val

cl, hd = tup

xformer = ip.ev(hd.strip())
es('Transform w/ %s' % repr(xformer))
return xformer(rest)

class LeoNode(object):
class LeoNode(object, UserDict.DictMixin):
def __init__(self,p):
self.p = p.copy()

Expand Down Expand Up @@ -130,12 +108,48 @@ def set_l(self,val):

def __iter__(self):
return (LeoNode(p) for p in self.p.children_iter())


def _children(self):
d = {}
for child in self:
head = child.h
tup = head.split(None,1)
if len(tup) > 1 and tup[0] == '@k':
d[tup[1]] = child
continue

if not valid_attribute(head):
d[head] = child
continue
return d
def keys(self):
d = self._children()
return d.keys()
def __getitem__(self, key):
key = str(key)
d = self._children()
return d[key]
def __setitem__(self, key, val):
key = str(key)
d = self._children()
if key in d:
d[key].v = val
return

if not valid_attribute(key):
head = key
else:
head = '@k ' + key
p = c.createLastChildNode(self.p, head, '')
LeoNode(p).v = val
def __delitem__(self,key):
pass


class LeoWorkbook:
""" class for 'advanced' node access """
def __getattr__(self, key):
if key.startswith('_') or key == 'trait_names':
if key.startswith('_') or key == 'trait_names' or not valid_attribute(key):
raise AttributeError
cells = all_cells()
p = cells.get(key, None)
Expand All @@ -146,6 +160,9 @@ def __getattr__(self, key):

def __str__(self):
return "<LeoWorkbook>"
def __setattr__(self,key, val):
raise AttributeError("Direct assignment to workbook denied, try wb.%s.v = %s" % (key,val))

__repr__ = __str__
ip.user_ns['wb'] = LeoWorkbook()

Expand Down
21 changes: 7 additions & 14 deletions doc/examples/leo_bridge_demo.leo
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,34 @@
<leo_file>
<leo_header file_format="2" tnodes="0" max_tnode_index="0" clone_windows="0"/>
<globals body_outline_ratio="0.5">
<global_window_position top="167" left="763" height="600" width="800"/>
<global_window_position top="262" left="259" height="600" width="800"/>
<global_log_window_position top="0" left="0" height="0" width="0"/>
</globals>
<preferences/>
<find_panel_settings/>
<vnodes>
<v t="vivainio.20080218184525"><vh>@chapters</vh></v>
<v t="vivainio.20080218184540" a="E"><vh>@ipy-startup</vh>
<v t="vivainio.20080218184613"><vh>a</vh></v>
<v t="vivainio.20080218184613.1"><vh>b</vh></v>
<v t="vivainio.20080218200031" a="E"><vh>Some classes</vh>
<v t="vivainio.20080218184540" a="ET"><vh>@ipy-startup</vh>
<v t="vivainio.20080218184613.1" a="V"><vh>b</vh></v>
<v t="vivainio.20080218200031"><vh>Some classes</vh>
<v t="vivainio.20080218190816"><vh>File-like access</vh></v>
<v t="vivainio.20080218200106"><vh>csv data</vh></v>
</v>
</v>
<v t="vivainio.20080218195413"><vh>Class tests</vh>
<v t="vivainio.20080218195413" a="E"><vh>Class tests</vh>
<v t="vivainio.20080218200509"><vh>csvr</vh></v>
<v t="vivainio.20080218191007"><vh>tempfile</vh></v>
<v t="vivainio.20080218195413.1"><vh>rfile</vh></v>
</v>
<v t="vivainio.20080218201219" a="E"><vh>Direct variables</vh>
<v t="vivainio.20080218201219.1" a="TV"><vh>@ipy foo</vh></v>
<v t="vivainio.20080218201622"><vh>@ipy </vh></v>
<v t="vivainio.20080218201219.2"><vh>@ipy bar</vh></v>
<v t="vivainio.20080218201219.2"><vh>bar</vh></v>
</v>
</vnodes>
<tnodes>
<t tx="vivainio.20080218184525">?</t>
<t tx="vivainio.20080218184540"># this stuff will be pushed at ipython bridge startup

@others</t>
<t tx="vivainio.20080218184613">print "hello"</t>
<t tx="vivainio.20080218184613.1">print "world"</t>
<t tx="vivainio.20080218190816">def rfile(body):
""" @cl rfile
Expand All @@ -60,7 +56,7 @@ def tmpfile(body):
<t tx="vivainio.20080218191007">@cl tmpfile

Hello</t>
<t tx="vivainio.20080218195413"></t>
<t tx="vivainio.20080218195413">?</t>
<t tx="vivainio.20080218195413.1">@cl rfile
These
lines
Expand All @@ -78,9 +74,6 @@ readable </t>
a,b,b
1,2,2</t>
<t tx="vivainio.20080218201219"></t>
<t tx="vivainio.20080218201219.1">@cl rfile
121212</t>
<t tx="vivainio.20080218201219.2">"hello world"</t>
<t tx="vivainio.20080218201622"></t>
</tnodes>
</leo_file>

0 comments on commit 48ae66e

Please sign in to comment.