search across multiple leo files

17 views
Skip to first unread message

Terry Brown

unread,
Feb 26, 2011, 11:51:12 AM2/26/11
to leo-e...@googlegroups.com

Here's a short script using the leo.external.leosax module I recently pushed, which searches all the leo files referenced under the 'Links (@bookmarks)' node, which looks like this:

Links (@bookmarks)
  Contacts
     body: /home/tbrown/.contacts.leo
  Dates
     body: /home/tbrown/.leo/.dates.leo
  GLRI
     body: /home/tbrown/Desktop/Proj/GLRI/GLRI.leo
  etc. (about 35 files linked altogether)

It doesn't really matter how you store / manipulate the list of .leo file locations, I just happened to have that list already set up, with the bookmarks plugin I can click the headlines to open the files.

So, I lost a short SQL script for doing something tricky, and wanted to search all those .leo files to find it.

import os
from leo.external.leosax import get_leo_data

target=r'\set src'

list_head = g.findTopLevelNode(c, 'Links (@bookmarks)')

for i in list_head.subtree_iter():

    fn = i.b.strip().split('\n')[0]
    if not os.path.isfile(fn):
        g.es('NOT FOUND: %s'%i.h)
        continue

    leo_data = get_leo_data(fn)

    for j in leo_data.flat():
        j.b = '\n'.join(j.b)
        if target in j.h or target in j.b:
            g.es("%s" % (fn))
            g.es("  %s" % (j.h))
            # or g.es("  %s" % (j.UNL()))

The advantage of get_leo_data is that it's fast, just a couple of seconds to scan all 35 files. Of course it doesn't expand derived files, so it wouldn't have found anything in there, but I knew the lost snippet was just in a .leo file in this case.

Of course rather than just dumping the locations in the log pane you can construct a tree of @url links to jump you to the file and node containing the finds, but that was overkill for me in this case.

Cheers -Terry

Edward K. Ream

unread,
Feb 27, 2011, 7:38:28 AM2/27/11
to leo-e...@googlegroups.com
On Sat, Feb 26, 2011 at 10:51 AM, Terry Brown <terry_...@yahoo.com> wrote:
> Here's a short script using the leo.external.leosax module I recently
> pushed, which searches all the leo files referenced under the 'Links
> (@bookmarks)' node

Thanks for this. This would be a good addition to the contrib branch.

Edward

Reply all
Reply to author
Forward
0 new messages