2020-12-16 11:45:10 +00:00
|
|
|
def prettyPrintAttrName(attr_name):
|
|
|
|
if "." in attr_name:
|
|
|
|
return prettyPrint(attr_name)
|
|
|
|
return attr_name
|
|
|
|
|
|
|
|
|
2020-12-18 12:04:13 +00:00
|
|
|
stringEscapes = str.maketrans({"\\": "\\\\", '"': '\\"'})
|
|
|
|
|
|
|
|
|
2020-12-16 11:45:10 +00:00
|
|
|
def prettyPrint(item, level=""):
|
|
|
|
next_level = level + " "
|
2020-12-18 12:04:13 +00:00
|
|
|
|
2020-12-16 11:45:10 +00:00
|
|
|
if item is None:
|
|
|
|
return "null"
|
|
|
|
|
|
|
|
elif type(item) == bool:
|
|
|
|
if item:
|
|
|
|
return "true"
|
|
|
|
return "false"
|
|
|
|
|
|
|
|
elif type(item) in (int, float):
|
|
|
|
return f"{item}"
|
|
|
|
|
|
|
|
elif type(item) == str:
|
2020-12-18 12:04:13 +00:00
|
|
|
item = item.strip()
|
2020-12-16 11:45:10 +00:00
|
|
|
if "\n" in item:
|
2020-12-18 12:04:13 +00:00
|
|
|
return "".join(
|
|
|
|
[
|
|
|
|
"''\n",
|
|
|
|
"".join(
|
|
|
|
[
|
|
|
|
f"{next_level}{line}"
|
|
|
|
for line in item.splitlines(keepends=True)
|
|
|
|
]
|
|
|
|
),
|
|
|
|
f"\n{level}''",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
return f'"{item.translate(stringEscapes)}"'
|
2020-12-16 11:45:10 +00:00
|
|
|
|
|
|
|
elif type(item) == list:
|
|
|
|
if len(item) == 0:
|
|
|
|
return "[ ]"
|
|
|
|
return (
|
|
|
|
"[\n"
|
2020-12-18 12:04:13 +00:00
|
|
|
+ ("".join([f"{next_level}{prettyPrint(i, next_level)}\n" for i in item]))
|
2020-12-16 11:45:10 +00:00
|
|
|
+ f"{level}]"
|
|
|
|
)
|
|
|
|
|
|
|
|
elif type(item) == dict:
|
|
|
|
if len(item) == 0:
|
|
|
|
return "{ }"
|
|
|
|
if item.get("_type") == "literalExample":
|
|
|
|
if type(item["text"]) == str:
|
|
|
|
return item["text"]
|
|
|
|
else:
|
2020-12-18 12:04:13 +00:00
|
|
|
return prettyPrint(item["text"], level)
|
2020-12-16 11:45:10 +00:00
|
|
|
return (
|
|
|
|
"{\n"
|
|
|
|
+ (
|
|
|
|
"".join(
|
|
|
|
[
|
2020-12-18 12:04:13 +00:00
|
|
|
f"{next_level}{prettyPrintAttrName(n)} = {prettyPrint(v, next_level)};\n"
|
2020-12-16 11:45:10 +00:00
|
|
|
for n, v in item.items()
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
+ f"{level}}}"
|
|
|
|
)
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise NotImplementedError(item)
|