nac-yaml
A Python library with common YAML utility functions supporting Network as Code.
Usage
Load and merge YAML files
from pathlib import Path
from nac_yaml.yaml import load_yaml_files
# Default is ruamel's round-trip loader (preserves formatting internally).
data = load_yaml_files([
Path("path/to/file1.yaml"),
Path("path/to/file2.yaml"),
])
# Use the safe loader to get native dict/list containers.
data_safe = load_yaml_files([
Path("path/to/file1.yaml"),
Path("path/to/file2.yaml"),
], typ="safe")
Note: when typ is not round-trip (e.g. "safe"), formatting features (quotes/comments/style) are not preserved.
Files are merged in the given order; the files of a directory are sorted by relative path, like Terraform's fileset() (e.g. a.yaml, a/b.yaml, b.yaml). The order decides the order of list items and, for a value defined in several files, which one is kept (the last).
Performance
With typ="safe", documents are parsed with the libyaml based C parser of ruamel.yaml.clib, which is several times faster than the pure Python parser. The round-trip loader (the default) is always pure Python. ruamel.yaml.clib is installed on CPython up to 3.14 on the platforms it publishes wheels for: Linux (glibc and musl) x86_64, aarch64 and i686, macOS x86_64 and arm64, and Windows x86 and AMD64. Elsewhere, and if the extension can't be imported, the pure Python parser is used.
Results don't depend on the parser: if the C parser fails, the document is parsed again with the pure Python parser, whose error includes the offending line. Documents the C parser would parse differently (those containing a tab or a %YAML directive) always use the pure Python parser.
yaml_backend(typ) returns the parser in use ("c" or "python"). Set NAC_YAML_PURE=1 to always use the pure Python parser.
Merge parsed documents and find where values come from
For tools that keep documents in memory, such as editors, merge_documents() merges parsed documents like load_yaml_files() without modifying them, and records where each merged value comes from. parse_yaml() parses a document like load_yaml_files() does, find_yaml_files() returns the files it reads in merge order, and node_position() turns a path into a line and column. See the docstrings for details.
from nac_yaml.yaml import merge_documents, parse_yaml
result = merge_documents([("a.yaml", parse_yaml(text_a)), ("b.yaml", parse_yaml(text_b))])
result.origins.get(("nxos", "devices", 0, "name")) # [Origin(source='a.yaml', path=('nxos', 'devices', 0, 'name'))]
Ansible Vault encrypted values
Values encrypted with Ansible Vault can be used with the !vault tag:
password: !vault |
$ANSIBLE_VAULT;1.2;AES256;dev
6535616566336139303566666336633163663431306337356461633439626638...
The vault password is read from the ANSIBLE_VAULT_PASSWORD environment variable, ANSIBLE_VAULT_ID is optional:
export ANSIBLE_VAULT_ID=dev
export ANSIBLE_VAULT_PASSWORD=Password123
Decryption requires ansible-core, either installed in the same Python environment (pip install nac-yaml[vault]) or available as ansible-vault command on PATH. All values are decrypted in a single batch after parsing. If a value cannot be decrypted, load_yaml_files() raises a VaultError for explicitly provided files, while files found in directories are skipped with a warning. Ansible Vault is not supported on Windows.
Installation
Using uv (recommended)
uv add nac-yaml
Using pip
pip install nac-yaml
Metadata
Release files for nac-yaml 2.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| nac_yaml-2.2.0.tar.gz | 38.1 kB | Without provenance | Details for nac_yaml-2.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| nac_yaml-2.2.0-py3-none-any.whl | Python 3 | none | any | Details for nac_yaml-2.2.0-py3-none-any.whl |
Total release size: 63.1 kB
Release files / nac_yaml-2.2.0.tar.gz
| Download URL | nac_yaml-2.2.0.tar.gz |
|---|---|
| Size | 38.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
260f8cfa2f915d352264034bdcdae0e99c06572ab4be78a970ccb16aa866a894
|
|
BLAKE2b-256 checksum How to use checksums |
a4150aeb81a11d79539e770ceea4736a0c2ec00ce4a149975c13bc868b5d4b6c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / nac_yaml-2.2.0-py3-none-any.whl
| Download URL | nac_yaml-2.2.0-py3-none-any.whl |
|---|---|
| Size | 25.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2604d8f6aea29fd2aa2f9e93549e59073236908c73e160a6864f28339a664801
|
|
BLAKE2b-256 checksum How to use checksums |
9b2f2b31269798f7fb24ecbdb49c04be1000fd0a403050052ead49080b3815ed
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for nac-yaml 2.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.