Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/anki-code/xontrib-macro
Library of the useful macros for the xonsh shell.
https://github.com/anki-code/xontrib-macro
docker fopen json macro macros xml xonsh xontrib yaml
Last synced: about 2 months ago
JSON representation
Library of the useful macros for the xonsh shell.
- Host: GitHub
- URL: https://github.com/anki-code/xontrib-macro
- Owner: anki-code
- License: mit
- Created: 2021-03-27T09:11:56.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-16T08:07:15.000Z (9 months ago)
- Last Synced: 2024-05-21T06:56:02.247Z (7 months ago)
- Topics: docker, fopen, json, macro, macros, xml, xonsh, xontrib, yaml
- Language: Python
- Homepage:
- Size: 114 KB
- Stars: 15
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-xontribs - macro - Library of the useful macros for the xonsh shell. (Plugins / Prompt tweaks)
README
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:
```bash
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.`):
```xsh
xontrib load macro
with! macro.data.Write('/tmp/hello', replace=True): # more macros below
world
```By importing certain macro - recommended for scripts:
```xsh
from xontrib.macro.data import Write
with! Write('/tmp/hello', replace=True): # more macros below
world
```## Macros
### Block (xonsh builtin)
```python
from xonsh.contexts import Blockwith! Block() as b:
any
text
hereb.macro_block
# 'any\ntext\nhere\n\n'
b.lines
# ['any', 'text', 'here', '']
```### data.Write
Write a file from block ([rich list of parameters](https://github.com/anki-code/xontrib-macro/blob/main/xontrib/macro/data.py#L12)):
```xsh
from xontrib.macro.data import Writewith! 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](https://en.wikipedia.org/wiki/Shebang_(Unix)) use `Write(..., shebang="#!/bin/xonsh")`.
### data.JsonBlock
Make json block and use it as dict:
```python
from xontrib.macro.data import JsonBlockwith! JsonBlock() as j:
{"hello": "world"}j['hello']
# 'world'
```### data.XmlBlock
Simple XML macro context manager from [xonsh macro tutorial](https://xon.sh/tutorial_macros.html#context-manager-macros). This will return the parsed XML tree from a macro block
```xsh
from xontrib.macro.data import XmlBlockwith! XmlBlock() as tree:
Hello world!
Hello!
type(tree)
# xml.etree.ElementTree.Elementtree.find('body').text
# '\n Hello!\n '```
### run.Once
Run the code once and save mark about it in [XONSH_DATA_DIR](https://xon.sh/envvars.html#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:
```python
from xontrib.macro.run import Oncewith! Once('First install'):
if $(which pacman):
pacman -S vim htop
elif $(which apt):
apt update && apt install -y vim htop
```### podman.RunInPodman
```xsh
from xontrib.macro.container import RunInPodman as podmanwith! podman(): # default: image='ubuntu', executor='bash'
echo hello# hello
```### podman.RunInXonshPodman
```python
from xontrib.macro.container import RunInXonshPodman as Podwith! 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:
```python
podman run -it --rm xonsh/xonsh:slim xonsh -c @("""
pip install -U -q pip lolcat
echo "We are in podman container now!" | lolcat
""")
```### signal.DisableInterrupt
This macro allows disabling SIGINT (Ctrl+C) for group of commands.
```xsh
from xontrib.macro.signal import DisableInterruptecho 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.
```## Known issues
Context Manager Macro picks up comments from outside the block and ignore the initial comments in the block ([4207](https://github.com/xonsh/xonsh/issues/4207)). We can fix it in the xontrib by checking the indentation in the beginning line and the end line. PR is welcome!
## Related
* [spec-mod](https://github.com/anki-code/xontrib-spec-mod) - Library of xonsh subprocess specification modifiers e.g. `$(@json echo '{}')`.
## Credits
This package was created with [xontrib cookiecutter template](https://github.com/xonsh/xontrib-cookiecutter).