add files
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
from io import StringIO
|
||||
from ruamel.yaml import YAML
|
||||
|
||||
from .types import JSONData, JSONDataScalar
|
||||
|
||||
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]
|
||||
Reference in New Issue
Block a user