Skip to main content

Import YAML anchors from external files, and export them for re-use in other files

Project description

YAML-io

This Python library enables YAML anchors to be imported from external files via custom directives. It also enables imported anchors to be re-exported, so one file can unify many imports and them export them as one source.

This has a few benefits:

  • Re-use the same aliases in multiple files without repeating yourself
  • Use aliases without worrying about reordering your file, because the anchors are defined elsewhere
  • Use multiple layers of imports to logically separate concerns and create versions of files that update all downstream files simultaneously.

Setup

  1. If a user calls yaml.load(...) and passes in:
  • A path to a file on disk

    → _try_file_path_and_load calls load_imports_exports(...) on that file, recursively resolving #!import statements.

  • A file object with a valid .name attribute that actually exists on disk

    → same as above.

  • Otherwise, the library just falls back to normal PyYAML behavior (e.g., inline YAML in a string, or a non‑file input).

  1. There are no code changes.
import yaml
import yaml_io  # triggers the patch

with open("some_yaml.yaml") as f:
    data = yaml.safe_load(f)  # Now automatically supports #!import, #!export

Usage

Mark directives in your YAML like:

#!import ../../global/prod-default/base-package.yml as base

and refer to imported anchors as:

- *base.progress_check_model

Anchors defined directly in the file are automatically available to downstream files. Imported anchors need to be re‑exported with a directive like:

#!export base.progress_check_model

Versioning Example

A versioned file containing all the anchors we want to define once. Imagine we have multiple files and many anchors.

./global/versions/v1.2/actions.yml

- &anchor1
- &anchor2

A router-like file where we refer to the versioned files and export them for re-use:

./global/prod-default/base-package.yml

#!import ../../versions/v1.2/acions.yml as a

#!export a.anchor1, a.anchor2

The most downstream file where all anchors are resolved and used as aliases, such as this example ./workspace/acme/actions.yml:

#!import ../../global/prod-default/base-package.yml as base

- *base.anchor1
- *base.anchor2
  • One advantage to using the base-package intermediary is the ability to change everything from v1.2 to v1.3 with a small number of changes when you're ready.

  • One advantage to using the prod-default folder is you can create other folders, such as prod-beta or uat-default and point specific files there instead for testing.

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

yaml_io-0.1.6.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

yaml_io-0.1.6-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file yaml_io-0.1.6.tar.gz.

File metadata

  • Download URL: yaml_io-0.1.6.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.4

File hashes

Hashes for yaml_io-0.1.6.tar.gz
Algorithm Hash digest
SHA256 0daf9e3978a755acc566f33e90d378f81ef13e79f6552a50ed210f95f4985c48
MD5 c6a604eb0101c40771845f40ed37be7d
BLAKE2b-256 d6450c5c739fa016f0c16f6725a841b7e16529159a4cc38979e4dfa5160dd438

See more details on using hashes here.

File details

Details for the file yaml_io-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: yaml_io-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.4

File hashes

Hashes for yaml_io-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 cb4686e18639df7572cc972dc17e7e090da1751b47f020b07fa396a38a0ae6b3
MD5 ddd015f704177ee7aa044bc1842cc1f5
BLAKE2b-256 3fc6e1d093ee568f6463d84c0ef7f0bbb31c8f678badd42c154dac73bbf4b6d8

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