Skip to content

About

YAML 1.2 for Python: fast, spec-correct, minimal-diff edits

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

oneyaml

json-style YAML 1.2 and comment-preserving editing for Python, in one package. Edit a file or apply a JSON Patch to it and only what you touch changes; a native backend and a pure-Python one sit behind the same API.

Python 3.10+ CI License: MIT OR Apache-2.0

Full results, with memory and the method →

Why oneyaml

  • Edits that keep your file. An unedited document writes back byte for byte. Setting a value changes only that value, in its own style, and comments stay where they were (documents and edits).
  • JSON Patch and merge patch on YAML. RFC 6902 and RFC 7396 patches apply to a YAML file all or nothing, and keep its layout and comments (patches).
  • YAML 1.2 by default. yes is a string and 0755 is the integer 755; << merge keys are there when you ask for them. Checked against the yaml-test-suite (conformance; what changes from YAML 1.1).
  • The json API you know. loads, load, dumps and dump with plain dict, list and str, parse_float, default and sort_keys, and one YAMLError that is a ValueError.
  • Safe on untrusted input. No tag constructs anything unless you hand it a function. Depth and alias expansion are bounded by default, and Limits.untrusted() adds caps on sizes for network input (limits).
  • Drop-in modules for PyYAML and ruamel.yaml. Change one import and keep your calls; the guides for PyYAML and ruamel.yaml list what differs.

Install

oneyaml isn't on PyPI. To try it, install from a clone:

pip install ./oneyaml-python

Quick start

Read a file into plain Python data, then edit it in place: a literal block gets a new line, a patch changes one value and adds a key, and the comments and every other byte stay.

import oneyaml

text = """\
# deploy settings
name: api
script: |
  make build
replicas: 2  # raised by the release job
"""

config = oneyaml.loads(text)  # plain Python data, as json.loads gives
print(config)

doc = oneyaml.parse(text)  # a document that keeps every byte you don't change
doc["script"] = "make build\nmake test\n"
doc.patch([
    {"op": "replace", "path": "/replicas", "value": 3},
    {"op": "add", "path": "/region", "value": "eu-west-1"},
])
print(doc, end="")
{'name': 'api', 'script': 'make build\n', 'replicas': 2}
# deploy settings
name: api
script: |
  make build
  make test
replicas: 3  # raised by the release job
region: eu-west-1

Coming from PyYAML? import oneyaml.compat.pyyaml as yaml keeps your code working, and YAML 1.2 for PyYAML users shows what changes when you move to oneyaml.loads. For input you don't control, pass limits=oneyaml.Limits.untrusted().

Where to go

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

About

YAML 1.2 for Python: fast, spec-correct, minimal-diff edits

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages