GeoIPS YAML Utilities
Project description
# # # This source code is subject to the license referenced at
# # # https://github.com/NRLMMD-GEOIPS.
GeoIPS YAML Utilities
geoips_yaml_utils is the standard YAML interface for GeoIPS. It is a drop-in
replacement for import yaml that adds two safeguards on top of PyYAML while leaving
the rest of the library untouched.
First, it detects duplicate mapping keys: safe_load and safe_load_all raise
DuplicateKeyError when a YAML document repeats a key, instead of silently keeping the
last value the way plain PyYAML does. Second, it resolves environment variables in YAML
files: the EnvVarLoader and EnvVarLoaderNoDuplicates loaders (adapted from the
MIT-licensed pyaml-env) expand !ENV ${VAR} and !ENV ${VAR:default} tags, raising
MissingEnvironmentVariableError when a referenced variable is unset and has no default.
Every other PyYAML symbol (dump, SafeLoader, YAMLError, and so on) passes through
unchanged, so geoips_yaml_utils can serve as the only yaml import in a GeoIPS
package. No GeoIPS plugins or domain logic live here; this package is a focused,
standalone utility intended to provide consistent YAML handling across GeoIPS and its
plugin packages.
Install geoips_yaml_utils package
From source:
git clone https://github.com/NRLMMD-GEOIPS/geoips_yaml_utils.git
# cd to geoips_yaml_utils's top level dir
pip install -e .
Usage
geoips_yaml_utils is a drop-in replacement for import yaml. Import it under the
yaml name and the duplicate-key-checking loaders are used automatically:
import geoips_yaml_utils as yaml
cfg = yaml.safe_load(open("plugin.yaml")) # raises DuplicateKeyError on duplicate keys
Resolving environment variables
The EnvVarLoader and EnvVarLoaderNoDuplicates loaders resolve !ENV tags by
substituting environment variables. This functionality is adapted from the
MIT-licensed pyaml-env project.
Tag a scalar with !ENV, then reference variables inside it with ${...}:
${VAR}is replaced with the value of environment variableVAR. IfVARis not set,MissingEnvironmentVariableErroris raised.${VAR:default}falls back todefaultwhenVARis not set. The separator is a colon (:), not bash's:-.- A single scalar may contain multiple references, e.g.
${HOST}:${PORT}. - Resolved values are always strings, since environment variables are strings.
Given config.yaml:
database:
host: !ENV ${DB_HOST}
port: !ENV ${DB_PORT:5432}
url: !ENV "${DB_HOST}:${DB_PORT:5432}"
with DB_HOST=db.example.com exported and DB_PORT unset:
import geoips_yaml_utils as yaml
with open("config.yaml") as f:
config = yaml.load(f, Loader=yaml.EnvVarLoaderNoDuplicates)
# {
# "database": {
# "host": "db.example.com",
# "port": "5432",
# "url": "db.example.com:5432",
# }
# }
EnvVarLoaderNoDuplicates also rejects duplicate keys (raising DuplicateKeyError).
For !ENV resolution without duplicate detection, use EnvVarLoader instead:
config = yaml.load(f, Loader=yaml.EnvVarLoader)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file geoips_yaml_utils-0.0.0.post38.dev0.tar.gz.
File metadata
- Download URL: geoips_yaml_utils-0.0.0.post38.dev0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d02f0a5348808109303df45c21e22f3788944df52e5e10025a9fe6eebceeae6
|
|
| MD5 |
e9461d0bdff5129bd7bf5218b80d4dfc
|
|
| BLAKE2b-256 |
73a325ab1c14beca1f0ca3ac658a286cf10e4d8f613a29e5f29611ecf95627d8
|
File details
Details for the file geoips_yaml_utils-0.0.0.post38.dev0-py3-none-any.whl.
File metadata
- Download URL: geoips_yaml_utils-0.0.0.post38.dev0-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
671c2fda85747660a26afb425bcbf2a8039a39c7c3c3284d5f0e9142bf6fef08
|
|
| MD5 |
654d86f4a552b096a6d2d9c813d21d44
|
|
| BLAKE2b-256 |
cd2874dca1652d9a7e52c20e7995f6c327d0d0b7c2f90a3729223de9a73b05d9
|