Metadata-Version: 2.5
Name: nac-yaml
Version: 2.2.0
Summary: A Python library with common YAML utility functions supporting 'Network as Code'.
Project-URL: homepage, https://github.com/netascode/nac-yaml
Project-URL: repository, https://github.com/netascode/nac-yaml
Project-URL: documentation, https://github.com/netascode/nac-yaml
Author-email: Daniel Schmidt <danischm@cisco.com>
Maintainer-email: Daniel Schmidt <danischm@cisco.com>
License: MPL-2.0
License-File: LICENSE
Keywords: automation,cisco,network,utilities,yaml
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: ruamel-yaml-clib>=0.2.15; platform_python_implementation == 'CPython' and python_version < '3.15' and ((sys_platform == 'linux' and platform_machine in 'x86_64 aarch64 i686') or (sys_platform == 'darwin' and platform_machine in 'x86_64 arm64') or (sys_platform == 'win32' and platform_machine in 'AMD64 x86'))
Requires-Dist: ruamel-yaml>=0.18.15
Provides-Extra: dev
Requires-Dist: ansible-core>=2.17.6; (sys_platform != 'win32' and python_version < '3.12') and extra == 'dev'
Requires-Dist: ansible-core>=2.20.0; (sys_platform != 'win32' and python_version >= '3.12') and extra == 'dev'
Requires-Dist: bandit[toml]>=1.9.4; extra == 'dev'
Requires-Dist: pre-commit>=4.6.2; extra == 'dev'
Requires-Dist: pytest-cov>=7.1.0; extra == 'dev'
Requires-Dist: pytest-mock>=3.17.0; extra == 'dev'
Requires-Dist: pytest>=9.1.1; extra == 'dev'
Requires-Dist: ruff>=0.17.2; extra == 'dev'
Requires-Dist: ty>=0.0.86; extra == 'dev'
Provides-Extra: vault
Requires-Dist: ansible-core>=2.17.6; (sys_platform != 'win32' and python_version < '3.12') and extra == 'vault'
Requires-Dist: ansible-core>=2.20.0; (sys_platform != 'win32' and python_version >= '3.12') and extra == 'vault'
Description-Content-Type: text/markdown

[![Tests](https://github.com/netascode/nac-yaml/actions/workflows/test.yml/badge.svg)](https://github.com/netascode/nac-yaml/actions/workflows/test.yml)
![Python Support](https://img.shields.io/badge/python-3.11%20%7C%203.12%20%7C%203.13%20%7C%203.14-informational "Python Support: 3.11, 3.12, 3.13, 3.14")

# nac-yaml

A Python library with common YAML utility functions supporting `Network as Code`.

## Usage

### Load and merge YAML files

```python
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.

```python
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](https://docs.ansible.com/ansible/latest/vault_guide/index.html) can be used with the `!vault` tag:

```yaml
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:

```bash
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)

```bash
uv add nac-yaml
```

### Using pip

```bash
pip install nac-yaml
```
