Metadata-Version: 2.4
Name: ryaml
Version: 0.6.0
Classifier: Programming Language :: Python
Classifier: Programming Language :: Rust
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: Programming Language :: Python :: 3.15
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: WebAssembly :: Emscripten
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: MacOS
Classifier: Operating System :: Android
Classifier: Operating System :: iOS
Classifier: Typing :: Typed
License-File: LICENSE
Summary: A pyyaml compatible YAML 1.1 parser in Rust.
Keywords: yaml,parsing
Author-email: Emma Harper Smith <emma@emmatyping.dev>
License: MIT
Requires-Python: >=3.11, <4
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: homepage, https://github.com/emmatyping/ryaml
Project-URL: repository, https://github.com/emmatyping/ryaml

ryaml
=====

### *Quickly and safely parse yaml*

### What is ryaml?

ryaml is a Python library that wraps a Rust yaml parser, [libyaml-safer](https://github.com/simonask/libyaml-safer),
to quickly and safely parse and dump yaml to and from Python objects.

The project is *drop-in compatible* with PyYAML's `CSafeLoader` and `CSafeDumper`, meaning that any behavior differences are a bug and should be reported against this project.

Like PyYAML, this library implements the YAML 1.1 specification.

## Installation

We ship binary wheels for Linux (manylinux and musllinux), Windows, macOS, Android, iOS,
and Pyodide (WebAssembly), including free-threaded CPython builds, so as long as you are
using Python 3.11+, you can run:

```
$ python -m pip install ryaml
```

Otherwise, you will need to build from source. To do so, first install Rust 1.89 or newer.

Then you should be able to just

```shell
$ git clone https://github.com/emmatyping/ryaml
$ cd ryaml
$ python -m pip install .
```

Or if you want to build a wheel:

```shell
$ git clone https://github.com/emmatyping/ryaml
$ cd ryaml
$ python -m pip install maturin
$ maturin build --release
# OR if you want an abi3 wheel (compatible with Python 3.11+, except free-threaded builds)
$ maturin build --release --features abi3
```

And a wheel will be created in `target/wheels` which you can install.

## Usage

The API of `ryaml` is very similar to that of `json` in the standard library:

You can use `ryaml.loads` to read from a `str` (or UTF-8/UTF-16 `bytes`):

```python
import ryaml
obj = ryaml.loads('key: [10, "hi"]')
assert isinstance(obj, dict) # True
assert obj['key'][1] == "hi" # True
```

And `ryaml.dumps` to dump an object into a yaml file:

```python
import ryaml
s = ryaml.dumps({ 'key' : None })
print(s)
# prints:
# key: null
```

There are also `ryaml.load` and `ryaml.load_all` to read yaml document(s) from files:

```python
import ryaml
obj = {'a': [{'b': 1}]}
with open('test.yaml', 'w') as w:
    ryaml.dump(w, obj)
with open('test.yaml', 'r') as r:
    assert ryaml.load(r) == obj
with open('multidoc.yaml', 'w') as multi:
    multi.write('''
---
a:
  key:
...
---
b:
  key:
    ''')
with open('multidoc.yaml', 'r') as multi:
    docs = ryaml.load_all(multi)
assert len(docs) == 2
assert docs[0]['a']['key'] is None
```

`ryaml.load_all` will, as seen above, load multiple documents from a single file, and `ryaml.loads_all`
does the same for a `str`.

### Using ryaml with PyYAML

If you already use PyYAML, pass `ryaml.RSafeLoader` or `ryaml.RSafeDumper` in place of `CSafeLoader`
or `CSafeDumper`:

```python
import yaml
import ryaml
obj = yaml.load('key: [10, "hi"]', Loader=ryaml.RSafeLoader)
print(yaml.dump(obj, Dumper=ryaml.RSafeDumper))
# prints:
# key:
# - 10
# - hi
```

### Custom representers

With PyYAML installed, register callbacks on `ryaml.RSafeDumper` or on a subclass
to control how Python objects become YAML nodes. `add_representer` matches an
exact type. `add_multi_representer` matches a type and its subclasses using
Python's method resolution order. An exact registration takes precedence.

```python
from datetime import timedelta
from pathlib import PurePath

import ryaml


class ConfigDumper(ryaml.RSafeDumper):
    pass


def represent_path(dumper, path):
    return dumper.represent_scalar("tag:yaml.org,2002:str", path.as_posix())


def represent_duration(dumper, duration):
    return dumper.represent_scalar("tag:yaml.org,2002:int", str(int(duration.total_seconds())))


ConfigDumper.add_multi_representer(PurePath, represent_path)
ConfigDumper.add_representer(timedelta, represent_duration)

config = {"path": PurePath("config.yaml"), "timeout": timedelta(minutes=25)}
print(ryaml.dumps(config, Dumper=ConfigDumper))
# prints:
# path: config.yaml
# timeout: 1500
```

Pass the same class as `Dumper=` to `ryaml.dump` or `yaml.dump`. The module-level
`ryaml.add_representer` and `ryaml.add_multi_representer` functions default to
registering on `ryaml.RSafeDumper`.

Representers work as in PyYAML: they can call `dumper.represent_scalar`,
`represent_sequence`, `represent_mapping` and `represent_data`, change the nodes
these return, or build `yaml.nodes` objects themselves. Registering a representer
for a built-in type such as `str` or `dict` replaces ryaml's. Built-in types without
one are still represented in Rust, so registering representers does not slow down
dumping the rest of the data.

The nodes a representer returns are written as PyYAML's `CSafeDumper` writes them,
with three differences: ryaml raises `TypeError` for anything that is not a node,
which `CSafeDumper` silently leaves out of sequences and otherwise rejects with
`EmitterError`; it writes subclasses of the node classes, as PyYAML's pure-Python
`SafeDumper` does, where `CSafeDumper` treats them as non-nodes; and for a `bytes`
tag or value that is not UTF-8 it raises `UnicodeDecodeError`, where `CSafeDumper`
raises `MemoryError`.

## Performance

ryaml is faster than PyYAML's `CSafeLoader` and `CSafeDumper` on all the files in our
[benchmarks](https://github.com/emmatyping/ryaml/tree/main/benchmark), which also has instructions for running them yourself.

## Development

Tests are run with [Hatch](https://hatch.pypa.io/), which installs from the hash-pinned
lockfiles in `locks/`:

```shell
$ hatch test                  # current Python
$ hatch test --python 3.14t   # a specific Python, e.g. free-threaded 3.14
$ hatch run lint:run          # pre-commit: rustfmt, clippy, zizmor and others
```

## Thanks

This project is standing on the shoulders of giants, and would not be possible without:

[pyo3](https://pyo3.rs/)

[libyaml-safer](https://github.com/simonask/libyaml-safer)

[saphyr-parser-bw](https://github.com/bourumir-wyngs/saphyr/tree/dev/saphyr-parser)

[pyyaml](https://github.com/yaml/pyyaml/)

[yaml-test-suite](https://github.com/yaml/yaml-test-suite)

Earlier versions of this library were based on the following projects:

[serde-yaml](https://github.com/dtolnay/serde-yaml)

[yaml-rust](https://github.com/chyh1990/yaml-rust)

[pythonize](https://github.com/davidhewitt/pythonize)

