41 lines
943 B
Python
41 lines
943 B
Python
from io import StringIO
|
|
from ruamel.yaml import YAML
|
|
|
|
from . import hints
|
|
|
|
yaml = YAML(typ="safe")
|
|
|
|
|
|
def dump_scalar(entry: JSONDataScalar) -> str:
|
|
match entry:
|
|
case None:
|
|
return "null\n"
|
|
case True:
|
|
return "true\n"
|
|
case False:
|
|
return "false\n"
|
|
case _:
|
|
return f"{entry:s}\n"
|
|
|
|
|
|
def dump(obj: dict[str, JSONDataScalar | list[JSONDataScalar]]) -> str:
|
|
ret = ""
|
|
for key, value in obj.items():
|
|
ret += key
|
|
ret += ":"
|
|
|
|
match value:
|
|
case list() as entries:
|
|
ret += "\n"
|
|
for entry in entries:
|
|
ret += f" - {dump_scalar(entry)}"
|
|
case entry:
|
|
ret += f" {dump_scalar(entry)}"
|
|
|
|
return ret
|
|
|
|
|
|
def load(content: str) -> JSONData:
|
|
with StringIO(content) as sio:
|
|
return yaml.load(sio) # pyright: ignore[reportAny, reportUnknownMemberType]
|