Skip to main content

ycg (yaml config generator) merges and templates yaml files

Project description

yaml-config-generator (ycg)

ycg merges and templates yaml files.

$ ycg -h
usage: ycg [-h] [-o FILE] [-d FILE] [-V KEY VALUE] [-b DIR] [--version] FILE [FILE ...]

yaml config generator

positional arguments:
  FILE                  input files (use '-' for stdin)

options:
  -h, --help            show this help message and exit
  -o FILE, --output FILE
                        output to this file (default is stdout)
  -d FILE, --data FILE  data files (use '-' for stdin)
  -V KEY VALUE, --var KEY VALUE
                        add a variable that can be used in templates
  -b DIR, --basedir DIR
                        base directory for resolving paths in stdin input (ignored for files, defaults to current working directory)
  --version             show program's version number and exit

Installation

Install from PyPI with pip install ycg or just download the ycg.py file (make sure you have pyyaml and jinja2 installed).

$ curl -o ycg.py https://raw.githubusercontent.com/racccoooon/ycg/refs/heads/main/src/ycg.py
$ chmod +x ycg.py
# install to /usr/local/bin
$ sudo mv ycg.py /usr/local/bin/ycg
# or install to your local bin dir
$ mv ycg.py ~/.local/bin/ycg

Merging

If multiple input files are specified they will be merged together. Use - as the filename to read from stdin.

Files are merged in the order they are specified: The second file is merged into the first, then the third into the result of that and so on.

By default, lists are concatenated and mappings/dictionaries are combined. If a value uses the !overwrite tag it will replace the existing value rather than merging with it. Lists of mappings can use the !merge_by:<key> tag to specify a key by which to identify items. Items with the same key will be merged, rather than preserving both values.

Example !merge_by:<key>

# file1.yml
users: 
  - {id: 1, name: "Foo"}
  - {id: 2, name: "Bar"}
# file2.yml
users: !merge_by:id
  - {id: 1, email: "foo@example.com"}
  - {id: 3, name: "Bar"}
$ ycg file1.yml file2.yml
# result
users:
  - {id: 1, name: "Foo", email: "foo@example.com"}
  - {id: 2, name: "Bar"}
  - {id: 3, name: "Bar"}

Example !overwrite

# file1.yml
numbers: [1, 2, 3, 4]
words: ["foo", "bar", "qux"]
# file2.yml
numbers: !overwrite [2, 3, 5, 7]
words: ["foo", "foobar", "bar"]
$ ycg file1.yml file2.yml
# output
numbers: [2, 3, 5, 7]
words: ["foo", "bar", "qux", "foo", "foobar", "bar"]

Inclusion

Other files can be included using !include (as a string value) or !include:yaml (as yaml data).

The value after the tag must be a string and specifies the path of the file to include. Paths are always relative to the file in which they are specified. Paths in stdin are relative to the current working directory, this can be overridden with the --basedir option.

Included yaml files can also use the same tags for inclusion, merging and templating.

Example

# input.yml
string_value: !include lorem.txt
yaml_value: !include:yaml lorem.yml
# lorem.txt
Lorem Ipsum Dolor Sit Amet
# lorem.yml
Lorem: Ipsum
Dolor: Sit Amet
$ ycg input.yml
# output
string_value: Lorem Ipsum Dolor Sit Amet
yaml_value:
  Lorem: Ipsum
  Dolor: Sit Amet

Templating

The tags !template and !include:template can be used to template strings or included files with jinja2.

Data for the templates can be specified as files (with the --data option) or as individual variables (with the --var option). Use - as a data file to use stdin. If multiple data files are specified they are merged together just like the input files, and can use the same tags except for !template and !include:template.

Example

# input.yml
hello: !template "Hello {{ name }}"
numbers: !include:template template.j2
# data1.yml
numbers: [1, 2]
# data2.yml
numbers: [3, 4]
{# template.j2 #}
{% for x in numbers %}
Number {{ x }}
{% endfor %}
$ ycg -d data1.yml -d data2.yml -V name World input.yml
# output
hello: Hello World
numbers: |
    Number 1
    Number 2
    Number 3
    Number 4

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

ycg-1.0.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

ycg-1.0.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file ycg-1.0.0.tar.gz.

File metadata

  • Download URL: ycg-1.0.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.2

File hashes

Hashes for ycg-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6a6a5049742b8d35531735a0c8d960f9015b1b0404975050ad59a525d5bb412b
MD5 526a69150afc00b173e4c796e345463f
BLAKE2b-256 f9734fe533fa890ef33201b07afb94c4cc3799406a0f1f94b83415e4fe23fb3f

See more details on using hashes here.

File details

Details for the file ycg-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: ycg-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.2

File hashes

Hashes for ycg-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 731976aa8438821eeda43fbd46860ee48c33653a48e278d1476ef74815e2fe73
MD5 c2063a3c3c8ce409c425e99c1119ef21
BLAKE2b-256 812a0b8793bd463732abf0ec5477b6b0ba3f869fb1030fcae40fc090e4d7827b

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