Skip to main content

Library of the useful macros for the xonsh shell.

If you like the idea click ⭐ on the repo and tweet.

Installation

To install use pip:

xpip install xontrib-macro
# or: xpip install -U git+https://github.com/anki-code/xontrib-macro

Usage

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

Macros

Block (xonsh builtin)

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', '']

signal.DisableInterrupt

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.

data.Write

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").

data.JsonBlock

Make json block and use it as dict:

from xontrib.macro.data import JsonBlock

with! JsonBlock() as j:
    {"hello": "world"}

j['hello']
# 'world'

data.XmlBlock

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.Once

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

docker.RunInDocker

from xontrib.macro.docker import RunInDocker as docker

with! docker():  # default: image='ubuntu', executor='bash'
    echo hello

# hello

docker.RunInXonshDocker

from xontrib.macro.docker import RunInXonshDocker as Doxer

with! Doxer():  # default: image='xonsh/xonsh:slim', executor='/usr/local/bin/xonsh'
   echo Installing...
   pip install -U -q pip lolcat
   echo "We are in docker container now!" | lolcat
   
# We are in docker container now! (colorized)

This is the same as:

docker run -it --rm xonsh/xonsh:slim xonsh -c @("""
pip install -U -q pip lolcat
echo "We are in docker container now!" | lolcat
""")

Known issues

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!

Credits

This package was created with xontrib cookiecutter template.

Metadata

Release files for xontrib-macro 0.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xontrib-macro 0.4.2
File Size Uploaded Provenance Actions
xontrib_macro-0.4.2.tar.gz 10.0 kB Without provenance Details for xontrib_macro-0.4.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for xontrib-macro 0.4.2
File Interpreter ABI Platform Actions
xontrib_macro-0.4.2-py3-none-any.whl Python 3 none any Details for xontrib_macro-0.4.2-py3-none-any.whl

Total release size: 18.7 kB

Release files / xontrib_macro-0.4.2.tar.gz

Download URL xontrib_macro-0.4.2.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
3b50830fb00dcd33d29816579f60ac2f4bf084756eed46839e06a5c8c30ecd5c
BLAKE2b-256 checksum
How to use checksums
72173c27d136a6ecda9f590119daca78033fbeb09bc169db6efb804cd197e62c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.9.19

Release files / xontrib_macro-0.4.2-py3-none-any.whl

Download URL xontrib_macro-0.4.2-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ef0b12eca0700f74677fcbc42e336320bc549b1f1ad1d951290b2bbb1a817030
BLAKE2b-256 checksum
How to use checksums
42cff117840a2a9aee50031fa144dfed92e1d23f8173805b0f73a93bc0f6c5d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.9.19

Security for xontrib-macro 0.4.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page