Library of the useful macros for the xonsh shell.
If you like the idea click ⭐ on the repo and tweet.
To install use pip:
xpip install xontrib-macro
# or: xpip install -U git+https://github.com/anki-code/xontrib-macro
By loading the whole module - recommended for interactive usage (type macro.<Tab>
):
xontrib load macro
with! macro.data.Write('/tmp/hello', replace=True): # more macros below
world
By importing certain macro - recommended for scripts:
from xontrib.macro.data import Write
with! Write('/tmp/hello', replace=True): # more macros below
world
from xonsh.contexts import Block
with! Block() as b:
any
text
here
b.macro_block
# 'any\ntext\nhere\n\n'
b.lines
# ['any', 'text', 'here', '']
Write a file from block (rich list of parameters):
from xontrib.macro.data import Write
with! Write('/tmp/t/hello.xsh', chmod=0o700, replace=True, makedir=True, format={'name': 'world'}, verbose=True):
echo {name}
## Make directories: /tmp/t
## Write to file: /tmp/t/hello.xsh
## Set chmod: rw- --- ---
## Set exec: rwx --- ---
/tmp/t/hello.xsh
# world
There is also data.Replace()
macro with mode='w', replace=True, makedir=True, replace_keep='a'
.
Note! There is an upstream issue described below in "Known issues" section - the first lines that begin from #
will be ignored in the block. As workaround to create shebang use Write(..., shebang="#!/bin/xonsh")
.
Make json block and use it as dict:
from xontrib.macro.data import JsonBlock
with! JsonBlock() as j:
{"hello": "world"}
j['hello']
# 'world'
Simple XML macro context manager from xonsh macro tutorial. This will return the parsed XML tree from a macro block
from xontrib.macro.data import XmlBlock
with! XmlBlock() as tree:
<note>
<heading>Hello world!</heading>
<body>
Hello!
</body>
</note>
type(tree)
# xml.etree.ElementTree.Element
tree.find('body').text
# '\n Hello!\n '
Run the code once and save mark about it in XONSH_DATA_DIR. In the next run the code will not be executed if it was not changed. If the code will be changed it will be executed again.
Example:
from xontrib.macro.run import Once
with! Once('First install'):
if $(which pacman):
pacman -S vim htop
elif $(which apt):
apt update && apt install -y vim htop
from xontrib.macro.container import RunInPodman as podman
with! podman(): # default: image='ubuntu', executor='bash'
echo hello
# hello
from xontrib.macro.container import RunInXonshPodman as Pod
with! Pod(): # default: image='xonsh/xonsh:slim', executor='/usr/local/bin/xonsh'
echo Installing...
pip install -U -q pip lolcat
echo "We are in podman container now!" | lolcat
# We are in podman container now! (colorized)
This is the same as:
podman run -it --rm xonsh/xonsh:slim xonsh -c @("""
pip install -U -q pip lolcat
echo "We are in podman container now!" | lolcat
""")
This macro allows disabling SIGINT (Ctrl+C) for group of commands.
from xontrib.macro.signal import DisableInterrupt
echo start
with! DisableInterrupt():
echo 'sleep start'
sleep 10
echo 'sleep end'
echo finish
# start
# sleep start
# [Press Ctrl+C]
# KeyboardInterrupt will be raised at the end of current transaction.
# sleep end
# Exception KeyboardInterrupt: KeyboardInterrupt was received during transaction.
Context Manager Macro picks up comments from outside the block and ignore the initial comments in the block (4207). We can fix it in the xontrib by checking the indentation in the beginning line and the end line. PR is welcome!
- spec-mod - Library of xonsh subprocess specification modifiers e.g.
$(@json echo '{}')
.
This package was created with xontrib cookiecutter template.