Skip to main content

Library of the useful macroses for the xonsh shell.

Project description

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):
    world

By importing certain macro - recommended for scripts:

from xontrib.macro.data import Write
with! Write('/tmp/hello', replace=True):
    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', '']

data.Write

Write a file from block (rich list of parameters):

from xontrib.macro.data import Write

with! Write('/tmp/t/hello.xsh', chmod=0o600, exec='u', replace=True, makedir=True, verbose=True):
    echo world
    
## 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xontrib-macro-0.3.3.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

xontrib_macro-0.3.3-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file xontrib-macro-0.3.3.tar.gz.

File metadata

  • Download URL: xontrib-macro-0.3.3.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for xontrib-macro-0.3.3.tar.gz
Algorithm Hash digest
SHA256 2a2b14c9f282780dd485373e1205365fa4efee4921661af7d4a0d8f754909e49
MD5 3a0bc485d81303a64767e8158c7facae
BLAKE2b-256 b3e924ff39eca7522fe97ac2603f6b423f2b4132901199b274f98ca8c0d45926

See more details on using hashes here.

File details

Details for the file xontrib_macro-0.3.3-py3-none-any.whl.

File metadata

File hashes

Hashes for xontrib_macro-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 32b1d59b62403c7b9bd469f923eadeec261685ad4b2816613f1bac4d8dc6ff77
MD5 78031f373f2606d1f5815464cb8e7ff8
BLAKE2b-256 de133c4cc3bcf22b24c28b59a5de6a76776a378dbb5990d705248b06a8db7882

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page