##// END OF EJS Templates
ipy_leo: implement support for @ipy-root
Ville M. Vainio -
Show More
@@ -77,10 +77,39 b" attribute_re = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$')"
77 77 def valid_attribute(s):
78 78 return attribute_re.match(s)
79 79
80 _rootnode = None
81 def rootnode():
82 """ Get ileo root node (@ipy-root)
83
84 if node has become invalid or has not been set, return None
85
86 Note that the root is the *first* @ipy-root item found
87 """
88 global _rootnode
89 if _rootnode is None:
90 return None
91 if c.positionExists(_rootnode.p):
92 return _rootnode
93 _rootnode = None
94 return None
95
80 96 def all_cells():
97 global _rootnode
81 98 d = {}
82 for p in c.allNodes_iter():
99 r = rootnode()
100 if r is not None:
101 nodes = r.p.children_iter()
102 else:
103 nodes = c.allNodes_iter()
104
105 for p in nodes:
83 106 h = p.headString()
107 if h.strip() == '@ipy-root':
108 # update root node (found it for the first time)
109 _rootnode = LeoNode(p)
110 # the next recursive call will use the children of new root
111 return all_cells()
112
84 113 if h.startswith('@a '):
85 114 d[h.lstrip('@a ').strip()] = p.parent().copy()
86 115 elif not valid_attribute(h):
@@ -307,14 +336,17 b' def workbook_complete(obj, prev):'
307 336
308 337 def add_var(varname):
309 338 c.beginUpdate()
339 r = rootnode()
310 340 try:
311 p2 = g.findNodeAnywhere(c,varname)
341 if r is None:
342 p2 = g.findNodeAnywhere(c,varname)
343 else:
344 p2 = g.findNodeInChildren(c, r.p, varname)
312 345 if p2:
313 346 return LeoNode(p2)
314 347
315 rootpos = g.findNodeAnywhere(c,'@ipy-results')
316 if rootpos:
317 p2 = rootpos.insertAsLastChild()
348 if r is not None:
349 p2 = r.p.insertAsLastChild()
318 350
319 351 else:
320 352 p2 = c.currentPosition().insertAfter()
@@ -387,7 +419,7 b' def push_plain_python(node):'
387 419 def push_cl_node(node):
388 420 """ If node starts with @cl, eval it
389 421
390 The result is put to root @ipy-results node
422 The result is put as last child of @ipy-results node, if it exists
391 423 """
392 424 if not node.b.startswith('@cl'):
393 425 raise TryNext
@@ -539,3 +571,5 b' def run_leo_startup_node():'
539 571 print "Running @ipy-startup nodes"
540 572 for n in LeoNode(p):
541 573 push_from_leo(n)
574
575
General Comments 0
You need to be logged in to leave comments. Login now