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.

PyPI Version Build Status Coverage Status

Installation

Install the pyyaml_env_tag package with pip:

pip install pyyaml_env_tag

Enabling the tag

To enable the tag, pass your loader of choice into the add_env_tag function, which will return the loader with the construstor added to it.

import yaml
from yaml_env_tag import add_env_tag

myLoader = add_env_tag(yaml.Loader)

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

yaml.load(data, Loader=myLoader)

The add_env_tag is a high level helper function. If you need lower level access, you may add the constructor (yaml_env_tag.construct_env_tag) to the loader directly using the add_constructor method of the loader. Note that this requires that the tag (!ENV) be defined as well.

from yaml_env_tag import construct_env_tag

Loader.add_constructor('!ENV', construct_env_tag)

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

[1.0] - 2025-05-09

  • Add the add_env_tag helper function as a higher level way of modifying the loader.

[0.1] - 2020-11-11

The initial release.

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

pyyaml_env_tag-1.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

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

pyyaml_env_tag-1.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyyaml_env_tag-1.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pyyaml_env_tag-1.0.tar.gz
Algorithm Hash digest
SHA256 bc952534a872b583f66f916e2dd83e7a7b9087847f4afca6d9c957c48b258ed2
MD5 69f63881c5ffc333724237a0a911856a
BLAKE2b-256 c29532c8c79d784552ed687c676924381c0dc88b2a0248b50a32f4b5ac0ba03c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyyaml_env_tag-1.0.tar.gz:

Publisher: deploy.yml on waylan/pyyaml-env-tag

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: pyyaml_env_tag-1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pyyaml_env_tag-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 37f081041b8dca44ed8eb931ce0056f97de17251450f0ed08773dc2bcaf9e683
MD5 83ec5f691e8deba8dcb2a6956cfd7eb2
BLAKE2b-256 1a8cc35fdb193c3717bdb4dea0ea361dbe81997164e01deaa2809cc2d71aa6b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyyaml_env_tag-1.0-py3-none-any.whl:

Publisher: deploy.yml on waylan/pyyaml-env-tag

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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