File size: 2,903 Bytes
71687cf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | # Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
"""YAML serialization utilities for conda."""
from __future__ import annotations
from enum import Enum
from functools import cache
from io import StringIO
from pathlib import Path
from typing import TYPE_CHECKING, overload
import ruamel.yaml
if TYPE_CHECKING:
from io import IO
from typing import Any
from ..path import PathType
class CondaYAMLRepresenter(ruamel.yaml.representer.RoundTripRepresenter):
def default(self, data: Any) -> Any:
# Python types
if isinstance(data, Enum):
return self.represent_str(data.value)
elif isinstance(data, Path):
return self.represent_str(str(data))
# auxlib entity types
for attr in ("dump", "__json__", "to_json", "as_json"):
if method := getattr(data, attr, None):
return self.represent_data(method())
# mirror JSON behavior
raise TypeError(
f"Object of type {data.__class__.__name__} is not YAML serializable"
)
CondaYAMLRepresenter.add_representer(None, CondaYAMLRepresenter.default)
@cache
def _yaml() -> ruamel.yaml.YAML:
parser = ruamel.yaml.YAML(typ="rt")
parser.Representer = CondaYAMLRepresenter
parser.indent(mapping=2, offset=2, sequence=4)
parser.default_flow_style = False
parser.sort_base_mapping_type_on_output = False
return parser
@overload
def write(obj: Any) -> str: ...
@overload
def write(obj: Any, *, fp: IO[str]) -> None: ...
@overload
def write(obj: Any, *, path: PathType) -> None: ...
def write(
obj: Any, *, fp: IO[str] | None = None, path: PathType | None = None
) -> None | str:
if fp and path:
raise ValueError("At most one of fp or path must be provided")
if fp is not None:
_yaml().dump(obj, fp)
return None
else:
stream = StringIO()
_yaml().dump(obj, stream=stream)
text = stream.getvalue()
if path is not None:
Path(path).write_text(text)
return None
else:
return text
def dump(obj: Any, fp: IO[str]) -> None:
write(obj, fp=fp)
def dumps(obj: Any) -> str:
return write(obj)
@overload
def read(*, text: str) -> Any: ...
@overload
def read(*, fp: IO[str]) -> Any: ...
@overload
def read(*, path: PathType) -> Any: ...
def read(
*, text: str | None = None, fp: IO[str] | None = None, path: PathType | None = None
) -> Any:
if (text, fp, path).count(None) != 2:
raise ValueError("Exactly one of text, fp or path must be provided")
if fp is not None:
text = fp.read()
elif path is not None:
text = Path(path).read_text()
return _yaml().load(text)
def load(fp: IO[str]) -> Any:
return read(fp=fp)
def loads(s: str) -> Any:
return read(text=s)
YAMLError = ruamel.yaml.YAMLError
|