Skip to main content

An opinionated yaml formatter based on ruamel.yaml

Project description

yamkix

Pypi

Why?

What?

> ./yamkix -h
usage: yamkix [-h] -i INPUT [-t TYP] [-o OUTPUT] [-n] [-e] [-q] [-f] [-d]

Format yaml input file. By default, explicit_start is `On`, explicit_end is
`Off` and array elements are pushed inwards the start of the matching
sequence. Comments are preserved thanks to default parsing mode `rt`.

optional arguments:
  -h, --help            show this help message and exit
  -i INPUT, --input INPUT
                        the file to parse
  -t TYP, --typ TYP     the yaml parser mode. Can be `safe` or `rt`
  -o OUTPUT, --output OUTPUT
                        the name of the file to generate (same as input file
                        if not specied)
  -n, --no-explicit-start
                        by default, explicit start of the yaml doc is `On`,
                        you can disable it with this option
  -e, --explicit-end    by default, explicit end of the yaml doc is `Off`, you
                        can enable it with this option
  -q, --no-quotes-preserved
                        by default, quotes are preserverd you can disable this
                        with this option
  -f, --default-flow-style
                        enable the default flow style `Off` by default. In
                        default flow style (with typ=`rt`), maps and lists are
                        written like json
  -d, --no-dash-inwards
                        by default, dash are pushed inwards use `--no-dash-inwards` to have the dash start at the sequence level

Config

  • Explicit start of yaml docs by default (you can disable it with --no-explicit-start)
  • Quotes preserved by default (you can disable it with --no-quotes-preserved)
  • Arrays elements pushed inwards by default (you can disable it with --no-dash-inwards)
  • Output file is input file by default
  • Comments preserved by default thanks to ruamel.yaml round_trip mode (you can disable it with --typ safe)

To preserve or not to preserve quotes?

  • Quotes preserved means : if there were quotes in the input, they will also be present in the output, and it will be the same type (single/double) of quotes
  • Quotes not preserved means :
    • if quotes are not necessary (around pure strings), they will be removed
    • if quotes are present around booleans and numbers, they will be converted to default (single quotes)
    • if quotes are not present around booleans and numbers, there will be no quotes in the output too

Note: there is no option for the moment to force the usage of double quotes when -q/--no-quotes-preserved is used.

Quotes preserved (default behavior)

With input :

---
apiVersion: extensions/v1beta1 # with comment
kind: ReplicaSet
metadata:
  name: tname
  namespace: tns
  annotations:
    string_no_quotes: frontend
    string_single_quotes: 'frontend'
    string_double_quotes: "frontend"
    boolean_no_quotes: true
    boolean_single_quotes: 'true'
    boolean_double_quotes: "true"
    number_no_quotes: 1
    number_single_quotes: '1'
    number_double_quotes: "1"

the output will be the same as the input :

---
apiVersion: extensions/v1beta1 # with comment
kind: ReplicaSet
metadata:
  name: tname
  namespace: tns
  annotations:
    string_no_quotes: frontend
    string_single_quotes: 'frontend'
    string_double_quotes: "frontend"
    boolean_no_quotes: true
    boolean_single_quotes: 'true'
    boolean_double_quotes: "true"
    number_no_quotes: 1
    number_single_quotes: '1'
    number_double_quotes: "1"

Quotes not preserved (using -q/--no-quotes-preserved)

With input :

---
apiVersion: extensions/v1beta1 # with comment
kind: ReplicaSet
metadata:
  name: tname
  namespace: tns
  annotations:
    string_no_quotes: frontend
    string_single_quotes: 'frontend'
    string_double_quotes: "frontend"
    boolean_no_quotes: true
    boolean_single_quotes: 'true'
    boolean_double_quotes: "true"
    number_no_quotes: 1
    number_single_quotes: '1'
    number_double_quotes: "1"

the output will be :

---
apiVersion: extensions/v1beta1 # with comment
kind: ReplicaSet
metadata:
  name: tname
  namespace: tns
  annotations:
    string_no_quotes: frontend
    string_single_quotes: frontend
    string_double_quotes: frontend
    boolean_no_quotes: true
    boolean_single_quotes: 'true'
    boolean_double_quotes: 'true'
    number_no_quotes: 1
    number_single_quotes: '1'
    number_double_quotes: '1'

Note : kubesplit is not fully Kubernetes aware for the moment, so it does not try to enforce this behaviour only on string sensible kubernetes resource fields (.metadata.annotations and .spec.containers.environment values)

Where does the name 'yamkix' come from?

Usage

  • Install the package with pip install --user yamkix
  • Sample vscode task :
{
  "label": "format yaml with yamkix",
  "type": "shell",
  "command": "yamkix --input ${file}",
  "group": "build",
  "presentation": {
    "reveal": "always",
    "panel": "shared"
  },
  "problemMatcher": []
}

Hack

See our CONTRIBUTING guide.

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

yamkix-0.10.0.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

yamkix-0.10.0-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file yamkix-0.10.0.tar.gz.

File metadata

  • Download URL: yamkix-0.10.0.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/5.15.0-1040-azure

File hashes

Hashes for yamkix-0.10.0.tar.gz
Algorithm Hash digest
SHA256 f4d264ed8df46563620dfec14572629da2556ca07f6180677811b618dff99af5
MD5 8f9f9227da07aaf4dba59f1b18cfa4b7
BLAKE2b-256 4d52d32d558ad0e6f3f0e1a6987e21e0df116da5576e81a732b9ca0ea0eb8347

See more details on using hashes here.

File details

Details for the file yamkix-0.10.0-py3-none-any.whl.

File metadata

  • Download URL: yamkix-0.10.0-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/5.15.0-1040-azure

File hashes

Hashes for yamkix-0.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7266b3e281d0e032c59fd907703eeee99dbccab410f408f63620b1acfe5963bb
MD5 7176701b2d8c718e29518da9226825e6
BLAKE2b-256 a8452647705857d9151eeb2c5d921d8f6e7fa0031c26a3c5f5cf0e920428f792

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