scripting: run shell command and put results in a node

100 views
Skip to first unread message

Matt Wilkie

unread,
Nov 28, 2019, 1:22:28 AM11/28/19
to leo-editor
Prior to now I've only written Leo scripts that operate on things 'out there' somewhere. Here's the first Leo script I've written that modifies content in Leo. No doubt there's things to be done better/differently, important bits are hardcoded, but it actually works and does what I was looking for so I'm happy for today. ;-)

-matt

Put this in an empty node and run with Ctrl-B or [script-button].

@language python
''' Search folder tree for the specified pattern

Windows only at present.

matt wilkie <map...@gmail.com> Nov 2019
'''

import os
import shlex
import subprocess

# what to search for
pattern
="dist.leo"
   
#Todo: make this a parameter or prompt!

g
.es_print('='*40)
g
.es_print('Running search-dirs-for-string')

searchdir
=os.path.join(g.app.loadDir, '../../*')

command
= f'findstr -i -s -p "{pattern}" {searchdir}'
   
# windows specific cmd

#g.es_print(shlex.split(command))  # debug

p
= subprocess.run(shlex.split(command),
        shell
=True,
        stdout
=subprocess.PIPE,
        stderr
=None)

out = p.stdout
#err = p.stderr()
lines
= [g.toUnicode(z) for z in g.splitLines(out or [])]
lines
= [os.path.relpath(z, searchdir[:-2]) for z in lines]
   
# Remove Leo app dir prefix, less noise

p2
= c.p.insertAfter()
p2
.h = f"Find results: {pattern}"
p2
.b = f"""--- Matches for {pattern} under:
--- {searchdir}

{''.join(lines)}
"""

c
.selectPosition(p2)
c
.redraw()
g
.es_print('Results in next node')

'''Sources:

Run shell command and capture results:
LeoPyRef.leo#Found:shlex.split(command)-->g.execGitCommand

Put results into new node and redraw (EKR):
https://groups.google.com/d/topic/leo-editor/s16fP2pcxqM/discussion

Removing a path prefix (Mitch):
https://stackoverflow.com/questions/8693024/how-to-remove-a-path-prefix-in-python
'''



jkn

unread,
Nov 28, 2019, 8:39:19 AM11/28/19
to leo-editor
Thanks Matt, that looks useful. I will have a go at a Linux version...

    J^n

Matt Wilkie

unread,
Dec 2, 2019, 4:19:24 PM12/2/19
to leo-editor
Thanks Matt, that looks useful. I will have a go at a Linux version...

That would be cool. I've pushed it the Leo-editor-contrib repo under Projects/scripts from Scripts.leo in parent folder.

-matt

Matt Wilkie

unread,
Feb 19, 2020, 12:58:02 AM2/19/20
to leo-editor
Edward,

What is the idiomatic way to use and/or share scripts across Leo files?

[...] Here's the first Leo script I've written that modifies content in Leo.
[...] I've pushed it the Leo-editor-contrib repo under Projects/scripts from Scripts.leo in parent folder.

For example I'd like to use this script:
..../leo-contrib/Projects/Scripts.leo#Scripts-->@clean search-dirs-for-text.py

while operating in:
../leo-editor/leo/dist/leoDist.leo

What's the best way to do that?

-matt



Edward K. Ream

unread,
Feb 19, 2020, 6:14:30 AM2/19/20
to leo-editor
On Tue, Feb 18, 2020 at 11:58 PM Matt Wilkie <map...@gmail.com> wrote:

What is the idiomatic way to use and/or share scripts across Leo files?

[...] Here's the first Leo script I've written that modifies content in Leo.
[...] I've pushed it the Leo-editor-contrib repo under Projects/scripts from Scripts.leo in parent folder.

This is a reasonable way. You can also put the script in leo/scripts/scripts.leo and commit the change.

To make a script available, privately, for all your own work, put the script in an @button or @command node in myLeoSettings.leo Like this:

myLeoSettings.leo
- @settings
  - @commands
    - @command my-command
  - @buttons
    - @button my-button

Edward

Matt Wilkie

unread,
Feb 20, 2020, 1:15:33 AM2/20/20
to leo-editor
To make a script available, privately, for all your own work, put the script in an @button or @command node in myLeoSettings.leo Like this:

Thanks!

Edward K. Ream

unread,
Feb 20, 2020, 10:53:54 AM2/20/20
to leo-editor
On Thu, Feb 20, 2020 at 12:15 AM Matt Wilkie <map...@gmail.com> wrote:
To make a script available, privately, for all your own work, put the script in an @button or @command node in myLeoSettings.leo Like this:

Thanks!

You're welcome. Glad the tip helped.

Edward
Reply all
Reply to author
Forward
0 new messages