> I present it here to show newbies that it's worth the trouble
> to learn how to use Leo :-)
Don't you mean, "newbies that it's worth the trouble to learn how to
=program in Python="?
-------- Original Message --------
Subject: Why I love Leo
From: Edward K. Ream <[email protected]>
To: leo-editor <[email protected]>
Date: Thursday, November 11, 2010 6:36:59 PM
Here is a script I wrote this evening. I present it here to show
newbies that it's worth the trouble to learn how to use Leo :-)
The script that finds all docstrings in leoPlugins.leo. It was
inspired by Terry's plugin_catelog.py program. It opens
leoPlugins.leo, finds the top-level Plugins nodes, then in the
LeoDocs.leo outline creates a tree mirroring the structure of the
Plugins node in leoPlugins.leo, with each node containing just the
docstring of the plugin. I'll use this script to update docs in
LeoDocs.leo.
Here it is, flattened:
QQQQQ
'''Create a tree containing the docstrings
of most plugins in leoPlugins.leo.'''
class controller:
def __init__ (self,c):
self.c = c
def clear (self,p):
p.b = '@language rest\n'
while p.hasChildren():
p.firstChild().doDelete()
def getDocString(self,p):
'''Return the docstring of the @<file> node p.'''
for p2 in p.self_and_subtree():
s = p2.b
for tag in ("'''",'"""'):
i = s.find(tag)
if i> -1:
j = s.find(tag,i+3)
if j> -1:
return s[i+3:j]
else:
return ''
def openLeoPlugins(self):
fn = g.os_path_finalize_join(
g.app.loadDir,'..','plugins','leoPlugins.leo')
ok,frame = g.openWithFileName(fn,
old_c=self.c,enableLog=True,gui=None,readAtFileNodesFlag=True)
if ok:
return frame.c
else:
g.error('can not open leoPlugins.leo')
return None
def run(self):
c = self.c
new_c = self.openLeoPlugins()
if not new_c: return
aList = ('Examples','Experimental','Gui plugins','Testing',)
# Create the output node.
tag = 'get-docstrings-output'
output = c.p.insertAfter()
output.h = tag
self.clear(output)
# Scan the descendants of the Plugins node.
root = g.findNodeAnywhere(new_c,'Plugins')
if not root: return g.error('no Plugins node')
print('='*20)
for p in root.children():
if p.h not in aList and not p.h.startswith(' '):
print('\n**',p.h)
child = output.insertAsLastChild()
child.h = p.h
for p2 in p.subtree():
if p2.isAnyAtFileNode() and p2.h.endswith('.py'):
h = p2.anyAtFileNodeName()
s = self.getDocString(p2)
print('%5s %s' % (len(s),h))
child2 = child.insertAsLastChild()
child2.h = h
child2.b = "%s\n\n" % s.strip()
new_c.close()
c.redraw()
controller(c).run()
QQQQQ
It's easier to understand in the original outline form, but of course
I can't show that here. It's on the trunk at rev 3676.
The point is that a relatively simple script saves me a lot of work.
The script shows how easy it is to access and modify nodes in any
outline.
Edward
P.S. I flattened the script with an @button script. Here it is:
'''Convert a script to a string as if written with @nosent.'''
at = c.atFileCommands
at.write (p,kind = '@unknown',
nosentinels = True,thinFile = False,
scriptWrite = False, toString = True,
)
output = p.insertAfter()
output.h = 'flattened %s' % p.h
output.b = at.stringOutput
c.redraw()
EKR
--
You received this message because you are subscribed to the Google Groups
"leo-editor" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/leo-editor?hl=en.