Skip to main content

Patch yaml strings

Project description

yaml-patch

Apply patches to a yaml string, keeping most of the formatting and comments.

Some formatting is not kept due to underlying yaml library limitations:

  • Indentation will be forced to two spaces
  • Spacing before sequence dashes will be forced to two spaces
  • Empty lines at the start of the string will be removed

Installation

pip install yaml-patch

As a command line tool

You can pass any number of patches to be applied, they use the following syntax options:

Override a single value:

<field>.<subfield>=<value>

Example:

yaml-patch -f test.yml "spec.replicas=2"

Override a value inside a single list item:

<field>.[<position]>.<subfield>=<value>

Example:

yaml-patch -f test.yml "spec.template.containers.[0].image='mycontainer:latest'"

Override a value inside all list items:

<field>.[].<subfield>=<value>

Example:

yaml-patch -f test.yml "spec.template.containers.[].image='mycontainer:latest'"

Append a single value:

<field>.<subfield>+=<value>

Example (increment int):

yaml-patch -f test.yml "spec.replicas+=2"

Example (append string):

yaml-patch -f test.yml "spec.template.containers.[0].image+=':latest'"

Example (append item to list):

yaml-patch -f test.yml "spec.template.containers.[0].args+=['--verbose']"

As a Python library

To use yaml-patch as a library just import the function and pass patches as you would in the CLI examples above.

Example:

from yaml_patch import patch_yaml
from textwrap import dedent

def override_list_all_values():
    source_yaml = dedent(
        """\
        some_list:
          - alice
          - bob
        """
    )
    patches = ["some_list.[]='charlie'"]
    expected_yaml = dedent(
        """\
        some_list:
          - charlie
          - charlie
        """
    )
    assert patch_yaml(source_yaml, patches) == expected_yaml

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

yaml-patch-1.1.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yaml_patch-1.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file yaml-patch-1.1.0.tar.gz.

File metadata

  • Download URL: yaml-patch-1.1.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.7 Linux/5.15.0-1020-azure

File hashes

Hashes for yaml-patch-1.1.0.tar.gz
Algorithm Hash digest
SHA256 8379d6c33aa6f7f7ff2885e98ca37a331e22afb04471c678451eddf3391be311
MD5 bb2ee4d9371e6bb2a45a28ade6a9f0e7
BLAKE2b-256 15adb3e27a81b9d9d4399595fccbf541f977211a32391a398be946d506f5804f

See more details on using hashes here.

File details

Details for the file yaml_patch-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: yaml_patch-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.7 Linux/5.15.0-1020-azure

File hashes

Hashes for yaml_patch-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8dff052f0507bd452f784b4780491ffd85005606c852dc0dfd2f964fa9a91c50
MD5 110a9841f8ab866e87dcdbe9e4d827a4
BLAKE2b-256 38e1ec0f41217fe34e7ed58fee7d89593819a0259d77a8903111bd4aa1e1d716

See more details on using hashes here.

Supported by

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