Skip to main content

A custom YAML tag for referencing environment variables in YAML files.

Project description

pyyaml_env_tag

A custom YAML tag for referencing environment variables in YAML files.

Installation

Install PyYAML and the pyyaml_env_tag package with pip:

pip install pyyaml pyyaml_env_tag

Enabling the tag

To enable the tag, import and add the construct_env_tag constructor to your YAML loader of choice.

import yaml
from yaml_env_tag import construct_env_tag

yaml.Loader.add_constructor('!ENV', construct_env_tag)

Then you may use the loader as per usual. For example:

yaml.load(data, Loader=yaml.Loader)

Using the tag

Include the tag !ENV followed by the name of an environment variable in a YAML file and the value of the environment variable will be used in its place.

key: !ENV SOME_VARIABLE

If SOME_VARIABLE is set to A string!, then the above YAML would result in the following Python object:

{'key': 'A string!'}

The content of the variable is parsed using YAML's implicit scalar types, such as string, bool, integer, float, datestamp and null. More complex types are not recognized and simply passed through as a string. For example, if SOME_VARIABLE was set to the string true, then the above YAML would result in the following:

{'key': True}

If the variable specified is not set, then a null value is assigned as a default. You may define your own default as the last item in a sequence.

key: !ENV [SOME_VARIABLE, default]

In the above example, if SOME_VARIABLE is not defined, the string default would be used instead, as follows:

{'key': 'default'}

You may list multiple variables as fallbacks. The first variable which is set is used. In any sequance with more than one item, the last item must always be a default value and will not be resolved as an environment variable.

key: !ENV [SOME_VARIABLE, FALLBACK, default]

As with variable contents, the default is resolved to a Python object of the implied type (string, bool, integer, float, datestamp and null).

When SOME_VARIABLE is not set, all four of the following items will resolve to the same value (None):

- !ENV SOME_VARIABLE
- !ENV [SOME_VARIABLE]
- !ENV [SOME_VARIABLE, ~]
- !ENV [SOME_VARIABLE, null]

Related

pyyaml_env_tag was inspired by the Ruby package yaml-env-tag.

An alternate method of referencing environment variables in YAML files is implemented by pyyaml-tags and python_yaml_environment_variables. Each of those libraries use a template string and replace the template tag with the content of the variable. While this allows a single value to reference multiple variables and to contain additional content, it restricts all values to strings only and does not provide a way to define defaults.

License

pyyaml_env_tag is licensed under the MIT License as defined in LICENSE.

Changelog

Version 0.1 (released 2020-11-11)

The initial release.

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyyaml_env_tag-0.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

pyyaml_env_tag-0.1-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file pyyaml_env_tag-0.1.tar.gz.

File metadata

  • Download URL: pyyaml_env_tag-0.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.25.0

File hashes

Hashes for pyyaml_env_tag-0.1.tar.gz
Algorithm Hash digest
SHA256 70092675bda14fdec33b31ba77e7543de9ddc88f2e5b99160396572d11525bdb
MD5 bb7743fe8e53d8716da6c1bdb7945641
BLAKE2b-256 fb8eda1c6c58f751b70f8ceb1eb25bc25d524e8f14fe16edcce3f4e3ba08629c

See more details on using hashes here.

File details

Details for the file pyyaml_env_tag-0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pyyaml_env_tag-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 af31106dec8a4d68c60207c1886031cbf839b68aa7abccdb19868200532c2069
MD5 27fa6b84d223d68f0c685bc018564b4e
BLAKE2b-256 5a66bbb1dd374f5c870f59c5bb1db0e18cbe7fa739415a24cbd95b2d1f5ae0c4

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