Skip to content

Library of decorator aliases (daliases) for @xonsh.

License

Notifications You must be signed in to change notification settings

anki-code/xontrib-dalias

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Library of decorator aliases (daliases) for the xonsh shell.

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

Installation

To install use pip:

xpip install xontrib-dalias
# or
xpip install 'xontrib-dalias[dict,yaml]'  # With extra decorators.

Load:

xontrib load dalias

Usage

Transform output to object

Decorators:

  • Default:
    • @lines - return list of lines.
    • @json - json to Python dict.
    • @jsonl - json lines to Python list with dict objects.
    • @path - string to pathlib.Path.
    • @split - split output by whitespaces.
    • @split-lines - split output lines by whitespaces.
    • @split-lexer - split output into parts using xonsh lexer. It's the same as builtin @$() operator.
    • @split-lexer-lines - split output lines into parts using xonsh lexer. It's the same as builtin @$() operator.
  • Extra (xpip install 'xontrib-dalias[dict,yaml]'):
    • @dict - dict-like object (json, JavaScript object, Python dict) to Python dict.
    • @dictl - dict-like objects (json, JavaScript object, Python dict) to Python list with dict objects.
    • @yaml - YAML to Python dict.

Examples

$(@lines ls /)
# ['/bin', '/etc', '/home']

$(@json echo '{"a":1}')  # Try with `curl` ;)
# dict({"a":1})

$(@jsonl echo '{"a":1}\n{"b":2}')
# [{'a': 1}, {'b': 2}]

docker inspect @($(@json docker ps --format json)['ID'])
# Container info


$(@path which -s xonsh)
# Path('/path/to/xonsh')

$(@path which -s xonsh).parent
# Path('/path/to')

$(@path echo '/tmp1\n/tmp2')
# [Path('/tmp1'), Path('/tmp2')]

$(@path echo ' ')  # Safe
# None

$(@path echo ' \n\n/tmp1')  # Safe
# [Path('/tmp1')]


aliases['ydig'] = '@yaml dig +yaml'  # Update `dig` via `brew install bind` to have `+yaml`.
y = $(ydig google.com)
y[0]['type']
# 'MESSAGE'

Piping into decorated alias to get object:

$(echo '{"a":1}' | @json cat)
# dict({"a":1})
aliases['@j'] = '@json cat'
$(echo '{"a":1}' | @j)
# dict({"a":1})

Error handling

Default decorators:

  • @err - set $RAISE_SUBPROC_ERROR=True for the command.
  • @noerr - set $RAISE_SUBPROC_ERROR=False for the command.

Examples:

$RAISE_SUBPROC_ERROR = True  # General environment.
if ![@noerr ls nononofile]:  # Do not raise exception in case of error.
    echo file 

Credits

This package was created with xontrib template.

About

Library of decorator aliases (daliases) for @xonsh.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Contributors 2

  •  
  •  

Languages