项目作者: anki-code

项目描述 :
Library of the useful macro for the xonsh shell.
高级语言: Python
项目地址: git://github.com/anki-code/xontrib-macro-lib.git
创建时间: 2021-03-27T09:11:56Z
项目社区:https://github.com/anki-code/xontrib-macro-lib

开源协议:MIT License

下载



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:

  1. xpip install xontrib-macro
  2. # 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>):

  1. xontrib load macro
  2. with! macro.data.Write('/tmp/hello', replace=True): # more macros below
  3. world

By importing certain macro - recommended for scripts:

  1. from xontrib.macro.data import Write
  2. with! Write('/tmp/hello', replace=True): # more macros below
  3. world

Macros

Block (xonsh builtin)

  1. from xonsh.contexts import Block
  2. with! Block() as b:
  3. any
  4. text
  5. here
  6. b.macro_block
  7. # 'any\ntext\nhere\n\n'
  8. b.lines
  9. # ['any', 'text', 'here', '']

data.Write

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

  1. from xontrib.macro.data import Write
  2. with! Write('/tmp/t/hello.xsh', chmod=0o700, replace=True, makedir=True, format={'name': 'world'}, verbose=True):
  3. echo {name}
  4. ## Make directories: /tmp/t
  5. ## Write to file: /tmp/t/hello.xsh
  6. ## Set chmod: rw- --- ---
  7. ## Set exec: rwx --- ---
  8. /tmp/t/hello.xsh
  9. # 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:

  1. from xontrib.macro.data import JsonBlock
  2. with! JsonBlock() as j:
  3. {"hello": "world"}
  4. j['hello']
  5. # 'world'

data.XmlBlock

Simple XML macro context manager from xonsh macro tutorial. This will return the parsed XML tree from a macro block

  1. from xontrib.macro.data import XmlBlock
  2. with! XmlBlock() as tree:
  3. <note>
  4. <heading>Hello world!</heading>
  5. <body>
  6. Hello!
  7. </body>
  8. </note>
  9. type(tree)
  10. # xml.etree.ElementTree.Element
  11. tree.find('body').text
  12. # '\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:

  1. from xontrib.macro.run import Once
  2. with! Once('First install'):
  3. if $(which pacman):
  4. pacman -S vim htop
  5. elif $(which apt):
  6. apt update && apt install -y vim htop

podman.RunInPodman

  1. from xontrib.macro.container import RunInPodman as podman
  2. with! podman(): # default: image='ubuntu', executor='bash'
  3. echo hello
  4. # hello

podman.RunInXonshPodman

  1. from xontrib.macro.container import RunInXonshPodman as Pod
  2. with! Pod(): # default: image='xonsh/xonsh:slim', executor='/usr/local/bin/xonsh'
  3. echo Installing...
  4. pip install -U -q pip lolcat
  5. echo "We are in podman container now!" | lolcat
  6. # We are in podman container now! (colorized)

This is the same as:

  1. podman run -it --rm xonsh/xonsh:slim xonsh -c @("""
  2. pip install -U -q pip lolcat
  3. echo "We are in podman container now!" | lolcat
  4. """)

signal.DisableInterrupt

This macro allows disabling SIGINT (Ctrl+C) for group of commands.

  1. from xontrib.macro.signal import DisableInterrupt
  2. echo start
  3. with! DisableInterrupt():
  4. echo 'sleep start'
  5. sleep 10
  6. echo 'sleep end'
  7. echo finish
  8. # start
  9. # sleep start
  10. # [Press Ctrl+C]
  11. # KeyboardInterrupt will be raised at the end of current transaction.
  12. # sleep end
  13. # 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). 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 '{}').

Credits

This package was created with xontrib cookiecutter template.