Originally posted at the Google group here, reposting here by request.
Hello all, a very quick request -- really digging xonsh as my main shell and don't think I can go back.
What would be more helpful than anything if there were some "xonsh library" I could drop into my python scripts that would enable me to use $() and !(). I'm thinking of taking something like this:
#!/usr/bin/env xonsh
#myscript.xsh
dir1 = "~/mydir1"
dir2 = "~/mydir2"
dirlisting1 = $(ls @(dir1) -p | grep "/" -v).strip("\n").split("\n")
dirlisting2 = $(ls @(dir2) -p | grep "/" -v).strip("\n").split("\n")
parsed = [x for x in dirlisting1 if x not in dirlisting2]
fileexists = bool(!(test -e myfile).returncode == 0)
and turning it into something like this:
#!/usr/bin/env python3
#myscript.py
from xonsh import sh, shmore
dir1 = "~/mydir1"
dir2 = "~/mydir2"
dirlisting1 = sh(f""" ls {dir1} -p | grep "/" -v """).strip("\n").split("\n")
dirlisting2 = sh(f""" ls {dir2} -p | grep "/" -v """).strip("\n").split("\n")
parsed = [x for x in dirlisting1 if x not in dirlisting2]
fileexists = bool(shmore(""" test -e myfile """).returncode == 0)
This would be fabulous! It would be quick to regex convert things this way. There is no point to the multiline strings above - they just make escaping easy, and visually they look cool.
Or, if f-strings aren't your thing, you can just use the usual ones:
#!/usr/bin/env python3
#myscript.py
from xonsh import sh, shmore
dir1 = "~/mydir1"
dir2 = "~/mydir2"
dirlisting1 = sh('ls '+dir1+' -p | grep "/" -v').strip("\n").split("\n")
dirlisting2 = sh('ls '+dir2+' -p | grep "/" -v').strip("\n").split("\n")
parsed = [x for x in dirlisting1 if x not in dirlisting2]
fileexists = bool(shmore("test -e myfile").returncode == 0)
FWIW, I haven't found any Python library nearly as useful as xonsh for terminal scripting. In comparison, using things like Plumbum are just no fun at all.
Hey @battaglia01 -- thanks for raising this and for the kind words about xonsh! We're glad you like it.
Is there a limitation that you see if you use /usr/bin/env xonsh to run your python + xonsh scripts?
Just that it seems more difficult to distribute - people sometimes don't want to install an entire new shell just to run a python script where there's a few lines of $() and !() that I could replace with calls to Python's os library (which I find difficult to use). But a xonsh library could be a nice middle ground, as well as a way to get people more interested in xonsh.
Hi @battaglia01 - We actually have started moving in this direction a bit. Recently @CJ-Wright has started up a xonsh.lib sub-package, which is usable from pure Python. This is meant as a standard library for xonsh and downstream tools. Currently there are some xonsh-ish wrappers around os and subprocess.
We'd love to see more contributions to this effort! So if there is something like sh() that you'd like to see, by all means please help us add it!
Most helpful comment
Hi @battaglia01 - We actually have started moving in this direction a bit. Recently @CJ-Wright has started up a
xonsh.libsub-package, which is usable from pure Python. This is meant as a standard library for xonsh and downstream tools. Currently there are some xonsh-ish wrappers aroundosandsubprocess.We'd love to see more contributions to this effort! So if there is something like
sh()that you'd like to see, by all means please help us add it!