parent
282d0b1e5a
commit
aea6789af6
|
@ -4,8 +4,12 @@ def prettyPrintAttrName(attr_name):
|
||||||
return attr_name
|
return attr_name
|
||||||
|
|
||||||
|
|
||||||
|
stringEscapes = str.maketrans({"\\": "\\\\", '"': '\\"'})
|
||||||
|
|
||||||
|
|
||||||
def prettyPrint(item, level=""):
|
def prettyPrint(item, level=""):
|
||||||
next_level = level + " "
|
next_level = level + " "
|
||||||
|
|
||||||
if item is None:
|
if item is None:
|
||||||
return "null"
|
return "null"
|
||||||
|
|
||||||
|
@ -18,16 +22,28 @@ def prettyPrint(item, level=""):
|
||||||
return f"{item}"
|
return f"{item}"
|
||||||
|
|
||||||
elif type(item) == str:
|
elif type(item) == str:
|
||||||
|
item = item.strip()
|
||||||
if "\n" in item:
|
if "\n" in item:
|
||||||
return f"''{item}''"
|
return "".join(
|
||||||
return f'"{item}"'
|
[
|
||||||
|
"''\n",
|
||||||
|
"".join(
|
||||||
|
[
|
||||||
|
f"{next_level}{line}"
|
||||||
|
for line in item.splitlines(keepends=True)
|
||||||
|
]
|
||||||
|
),
|
||||||
|
f"\n{level}''",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
return f'"{item.translate(stringEscapes)}"'
|
||||||
|
|
||||||
elif type(item) == list:
|
elif type(item) == list:
|
||||||
if len(item) == 0:
|
if len(item) == 0:
|
||||||
return "[ ]"
|
return "[ ]"
|
||||||
return (
|
return (
|
||||||
"[\n"
|
"[\n"
|
||||||
+ ("".join([f"{level} {prettyPrint(i, next_level)}\n" for i in item]))
|
+ ("".join([f"{next_level}{prettyPrint(i, next_level)}\n" for i in item]))
|
||||||
+ f"{level}]"
|
+ f"{level}]"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -38,13 +54,13 @@ def prettyPrint(item, level=""):
|
||||||
if type(item["text"]) == str:
|
if type(item["text"]) == str:
|
||||||
return item["text"]
|
return item["text"]
|
||||||
else:
|
else:
|
||||||
return prettyPrint(item["text"], next_level)
|
return prettyPrint(item["text"], level)
|
||||||
return (
|
return (
|
||||||
"{\n"
|
"{\n"
|
||||||
+ (
|
+ (
|
||||||
"".join(
|
"".join(
|
||||||
[
|
[
|
||||||
f"{level} {prettyPrintAttrName(n)} = {prettyPrint(v, next_level)};\n"
|
f"{next_level}{prettyPrintAttrName(n)} = {prettyPrint(v, next_level)};\n"
|
||||||
for n, v in item.items()
|
for n, v in item.items()
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
|
@ -7,9 +7,26 @@ import pytest # type: ignore
|
||||||
(None, "null",),
|
(None, "null",),
|
||||||
(True, "true",),
|
(True, "true",),
|
||||||
("text", '"text"',),
|
("text", '"text"',),
|
||||||
|
(
|
||||||
|
"\nnew line is ignored at start and end\n",
|
||||||
|
'"new line is ignored at start and end"',
|
||||||
|
),
|
||||||
|
('"double quotes"', '"\\"double quotes\\""',),
|
||||||
|
("multi\nline\ntext", "''\n multi\n line\n text\n''",),
|
||||||
|
('"multi line\ndouble quotes"', "''\n \"multi line\n double quotes\"\n''",),
|
||||||
(123, "123",),
|
(123, "123",),
|
||||||
(123.123, "123.123",),
|
(123.123, "123.123",),
|
||||||
([False, "text"], ("[\n" " false\n" ' "text"\n' "]"),),
|
(
|
||||||
|
[False, "text", "multi\nline\ntext"],
|
||||||
|
"".join(
|
||||||
|
[
|
||||||
|
"[\n",
|
||||||
|
" false\n",
|
||||||
|
' "text"\n',
|
||||||
|
" ''\n multi\n" " line\n" " text\n" " ''\n" "]",
|
||||||
|
]
|
||||||
|
),
|
||||||
|
),
|
||||||
(
|
(
|
||||||
{"name1": "value1", "name.2": True, "name3": [False, "text"]},
|
{"name1": "value1", "name.2": True, "name3": [False, "text"]},
|
||||||
(
|
(
|
||||||
|
|
Loading…
Reference in a new issue