Skip to main content

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: parse_config (adapted from the MIT-licensed pyaml-env) expands !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 with parse_config

parse_config reads a YAML file from a path and resolves any !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 variable VAR. If VAR is not set, MissingEnvironmentVariableError is raised.
  • ${VAR:default} falls back to default when VAR is 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

config = yaml.parse_config("config.yaml")
# {
#     "database": {
#         "host": "db.example.com",
#         "port": "5432",
#         "url": "db.example.com:5432",
#     }
# }

By default parse_config also rejects duplicate keys (raising DuplicateKeyError). Pass detect_duplicates=False to keep the !ENV resolution but skip that check:

config = yaml.parse_config("config.yaml", detect_duplicates=False)

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

geoips_yaml_utils-1.0.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

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

geoips_yaml_utils-1.0.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: geoips_yaml_utils-1.0.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for geoips_yaml_utils-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0a1b4d1069c5f6d69da5baf0e19f246f2f3b8a87a8281383d9b897aa992bc77b
MD5 3dca8b2e3a756f9ae93755f75cfd7954
BLAKE2b-256 7336423c6e56b8baa3a6b44b41310252b5bb0d3bf9b1155680a4a7abab0b05f3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for geoips_yaml_utils-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b3c9134b392ea921c2b58502f080972bcce0b3b90ee75720bbe42494de5b6830
MD5 1a9c320082b146bb7f28ae626f7e2089
BLAKE2b-256 359305bb59fd8515f64cd56f2e4051b3cd7a3f645edd48e34bb1d7161471ab2a

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