Skip to main content

Lossless YAML parser and editor that preserves formatting, comments, and whitespace

Project description

yaml-edit

Python bindings for the yaml-edit Rust crate: a lossless YAML parser and editor that preserves formatting, comments, and whitespace.

Unlike most YAML libraries, yaml-edit does not round-trip through a plain data model. It keeps the original syntax tree, so edits change only the parts you touch and leave everything else - indentation, comment placement, quoting style, blank lines - exactly as it was.

Installation

$ pip install yaml-edit

A Rust toolchain is required to build from source.

Usage

from yaml_edit import Document

doc = Document.parse("name: old-project\nversion: 1.0.0\n")
doc.set("name", "new-project")
doc.set("version", "2.0.0")
print(doc, end="")
# name: new-project
# version: 2.0.0

Edits preserve surrounding formatting and comments:

doc = Document.parse(
    "# project config\n"
    "name: demo   # the name\n"
    "tags:\n"
    "  - a\n"
    "  - b\n"
)
mapping = doc.as_mapping()
mapping.set("name", "renamed")
print(doc, end="")
# # project config
# name: renamed   # the name
# tags:
#   - a
#   - b

Working with nested structures:

doc = Document.parse("servers:\n  - web\n  - db\n")
servers = doc.as_mapping().get_sequence("servers")
servers.push("cache")
print(doc, end="")
# servers:
#   - web
#   - db
#   - cache

Multi-document streams:

from yaml_edit import YamlFile

stream = YamlFile.parse("a: 1\n---\nb: 2\n")
for document in stream.documents():
    print(document.keys())
# ['a']
# ['b']

Supported value types

Scalar values passed to set, push, and insert may be str, int, float, or bool. None is rejected because the underlying editor has no way to emit a YAML null scalar.

License

Apache-2.0

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_edit-0.1.0.tar.gz (12.7 kB view details)

Uploaded Source

File details

Details for the file yaml_edit-0.1.0.tar.gz.

File metadata

  • Download URL: yaml_edit-0.1.0.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for yaml_edit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9ad352475e615ff4680c89dd2c42e0540502994be63e1f425546694ea2e4474b
MD5 ccee3ea47fea9df233d28911ecb2a260
BLAKE2b-256 17ff5c8c059dafaeb1340d07c45a1fca4b4a5cc56c198f515316920afa90f307

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