extend possible in-/output
This commit is contained in:
parent
93a39ad889
commit
d6fc41fe05
|
|
@ -17,7 +17,7 @@ def dump_scalar(entry: hints.JSONDataScalar) -> str:
|
||||||
return f"{entry}\n"
|
return f"{entry}\n"
|
||||||
|
|
||||||
|
|
||||||
def dump(obj: dict[str, hints.JSONDataScalar | list[hints.JSONDataScalar]]) -> str:
|
def dump(obj: hints.JSONDataMap) -> str:
|
||||||
ret = ""
|
ret = ""
|
||||||
for key, value in obj.items():
|
for key, value in obj.items():
|
||||||
ret += key
|
ret += key
|
||||||
|
|
@ -26,11 +26,32 @@ def dump(obj: dict[str, hints.JSONDataScalar | list[hints.JSONDataScalar]]) -> s
|
||||||
match value:
|
match value:
|
||||||
case []:
|
case []:
|
||||||
ret += " []\n"
|
ret += " []\n"
|
||||||
|
case {}:
|
||||||
|
ret += " {}\n"
|
||||||
|
case "":
|
||||||
|
ret += ' ""\n'
|
||||||
case list() as entries:
|
case list() as entries:
|
||||||
ret += "\n"
|
ret += "\n"
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
|
match entry:
|
||||||
|
case dict():
|
||||||
|
subdump = dump(entry)
|
||||||
|
ret += "\n".join(" " + l for l in subdump.splitlines())
|
||||||
|
case list():
|
||||||
|
pass
|
||||||
|
case _:
|
||||||
ret += f" - {dump_scalar(entry)}"
|
ret += f" - {dump_scalar(entry)}"
|
||||||
|
case dict() as substruct:
|
||||||
|
ret += "\n"
|
||||||
|
subdump = dump(substruct)
|
||||||
|
ret += "\n".join(" " + l for l in subdump.splitlines()) + "\n"
|
||||||
|
|
||||||
|
case str() as mlstring if key == "markdown_content":
|
||||||
|
if mlstring.strip() == "" or dump_scalar(mlstring).strip() == "null":
|
||||||
|
ret += ' ""\n'
|
||||||
|
else:
|
||||||
|
indented = "\n".join((" " + l) for l in dump_scalar(mlstring).splitlines())
|
||||||
|
ret += f" >\n{indented}\n"
|
||||||
case entry:
|
case entry:
|
||||||
ret += f" {dump_scalar(entry)}"
|
ret += f" {dump_scalar(entry)}"
|
||||||
|
|
||||||
|
|
@ -56,6 +77,10 @@ def load(content: str) -> hints.JSONData:
|
||||||
... key10:
|
... key10:
|
||||||
... - str
|
... - str
|
||||||
... - "str"
|
... - "str"
|
||||||
|
... key11: >
|
||||||
|
... * a
|
||||||
|
... * b:
|
||||||
|
... * c
|
||||||
... '''
|
... '''
|
||||||
>>> yaml_parsed = load(yaml)
|
>>> yaml_parsed = load(yaml)
|
||||||
>>> compare = {
|
>>> compare = {
|
||||||
|
|
@ -69,8 +94,8 @@ def load(content: str) -> hints.JSONData:
|
||||||
... 'key8': False,
|
... 'key8': False,
|
||||||
... 'key9': [1, 23.2],
|
... 'key9': [1, 23.2],
|
||||||
... 'key10': ['str', 'str'],
|
... 'key10': ['str', 'str'],
|
||||||
|
... 'key11': "* a\\n* b:\\n * c",
|
||||||
... }
|
... }
|
||||||
|
|
||||||
>>> {k: yaml_parsed.get(k, None) for k in compare if compare[k] != yaml_parsed[k]}
|
>>> {k: yaml_parsed.get(k, None) for k in compare if compare[k] != yaml_parsed[k]}
|
||||||
{}
|
{}
|
||||||
"""
|
"""
|
||||||
|
|
@ -78,20 +103,34 @@ def load(content: str) -> hints.JSONData:
|
||||||
ret = {}
|
ret = {}
|
||||||
key = None
|
key = None
|
||||||
value = None
|
value = None
|
||||||
for line in sub(r":[\s\n]*", ":\n ", dedent(content).strip()).splitlines():
|
mlstring = False
|
||||||
if line.endswith(":"):
|
for line in sub(r"(:( >)?)[\s\n]*", r"\1\n ", dedent(content).strip()).splitlines():
|
||||||
|
if line.startswith("#") or line.strip() == "---":
|
||||||
|
continue
|
||||||
|
elif line.endswith(":"):
|
||||||
if key:
|
if key:
|
||||||
ret[key] = value
|
ret[key] = value
|
||||||
value = None
|
value = None
|
||||||
|
mlstring = False
|
||||||
|
|
||||||
key = line.removesuffix(":")
|
key = line.removesuffix(":")
|
||||||
elif line.startswith(" -"):
|
elif line.endswith(": >"):
|
||||||
|
if key:
|
||||||
|
ret[key] = value
|
||||||
|
value = None
|
||||||
|
mlstring = True
|
||||||
|
|
||||||
|
key = line.removesuffix(": >")
|
||||||
|
elif line.startswith(" -") and not mlstring:
|
||||||
value = value or []
|
value = value or []
|
||||||
try:
|
try:
|
||||||
parsed = loads(line.removeprefix(" -").strip())
|
parsed = loads(line.removeprefix(" -").strip())
|
||||||
except:
|
except:
|
||||||
parsed = loads('"' + line.removeprefix(" -").strip() + '"')
|
parsed = loads('"' + line.removeprefix(" -").strip() + '"')
|
||||||
value.append(parsed)
|
value.append(parsed)
|
||||||
|
elif line.startswith(" ") and mlstring:
|
||||||
|
value = value or ""
|
||||||
|
value += line.removeprefix(" ") + "\n"
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
value = loads(line.strip())
|
value = loads(line.strip())
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue