Skip to main content

Tests Python Support

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

uv add nac-yaml

Using pip

pip install nac-yaml

Metadata

Release files for nac-yaml 2.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nac-yaml 2.2.1
File Size Uploaded Provenance Actions
nac_yaml-2.2.1.tar.gz 38.2 kB Without provenance Details for nac_yaml-2.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nac-yaml 2.2.1
File Interpreter ABI Platform Actions
nac_yaml-2.2.1-py3-none-any.whl Python 3 none any Details for nac_yaml-2.2.1-py3-none-any.whl

Total release size: 63.2 kB

Release files / nac_yaml-2.2.1.tar.gz

Download URL nac_yaml-2.2.1.tar.gz
Size 38.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8c054d80d750c504721a5398fca36bcff1f195c23938436120710e0f6e83eedd
BLAKE2b-256 checksum
How to use checksums
fe72606769dccc766ab53a7e77fb1488645433919478e679e7c328239f46accd
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.1-py3-none-any.whl

Download URL nac_yaml-2.2.1-py3-none-any.whl
Size 25.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
14578b9d5e185bfbe9d59dc78c4ab544e4bffd1da8b44662f668cb4ceb11074a
BLAKE2b-256 checksum
How to use checksums
39d5ae9f269d923bfaeff40c136af89a599be0b3eb68888a615317fbe4f26813
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.1

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page