summaryrefslogtreecommitdiff
path: root/etc/json-format.py
diff options
context:
space:
mode:
authorPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2023-06-27 16:08:22 +0200
committerPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2023-08-29 18:12:12 +0200
commitd501d86b5e974523e965d52001a5c185ce47be92 (patch)
tree6ef775ff9244e8f94a0184221baa31441e292e66 /etc/json-format.py
parent9fc4e4a122117a49c59cbbcecbf7aa892d301d0a (diff)
downloadrules-cc-d501d86b5e974523e965d52001a5c185ce47be92.tar.gz
python: Add type hints and fix style in rules-cc scripts
For maximum compatibility, we use the uppercase types from the typing package instead of the built-in types, therefore compliant with PEP 484 and PEP 526.
Diffstat (limited to 'etc/json-format.py')
-rwxr-xr-xetc/json-format.py87
1 files changed, 50 insertions, 37 deletions
diff --git a/etc/json-format.py b/etc/json-format.py
index 058d098..827205a 100755
--- a/etc/json-format.py
+++ b/etc/json-format.py
@@ -28,45 +28,58 @@
import json
import sys
-def is_simple(entry):
- if isinstance(entry, list):
- return len(entry) == 0
- if isinstance(entry, dict):
- return len(entry) == 0
- return True
+from typing import Any, Dict, List, cast
-def is_short(entry, indent):
- return len(json.dumps(entry)) + indent < 80
-def hdumps(entry, *, _current_indent=0):
- if is_short(entry, _current_indent):
+def is_simple(entry: Any) -> bool:
+ if isinstance(entry, list):
+ return len(cast(List[Any], entry)) == 0
+ if isinstance(entry, dict):
+ return len(cast(Dict[str, Any], entry)) == 0
+ return True
+
+
+def is_short(entry: Any, indent: int) -> bool:
+ return len(json.dumps(entry)) + indent < 80
+
+
+def hdumps(entry: Any, *, _current_indent: int = 0) -> str:
+ if entry:
+ if is_short(entry, _current_indent):
+ return json.dumps(entry)
+ if isinstance(entry, list):
+ result: str = "[ " + hdumps(cast(Any, entry[0]),
+ _current_indent=_current_indent + 2)
+ entries: List[Any] = entry[1:] # explicit type hint
+ for x in entries:
+ result += "\n" + " " * _current_indent + ", "
+ result += hdumps(x, _current_indent=_current_indent + 2)
+ result += "\n" + " " * _current_indent + "]"
+ return result
+ if isinstance(entry, dict):
+ result: str = "{ "
+ is_first: bool = True
+ keys = cast(Dict[str, Any], entry).keys() # explicit type hint
+ for k in keys:
+ key_entry: Any = entry[k] # explicit type hint
+ if not is_first:
+ result += "\n" + " " * _current_indent + ", "
+ result += json.dumps(k) + ":"
+ if is_simple(key_entry):
+ result += " " + json.dumps(key_entry)
+ elif is_short(key_entry,
+ _current_indent + len(json.dumps(k)) + 4):
+ result += " " + json.dumps(key_entry)
+ else:
+ result += "\n" + " " * _current_indent + " "
+ result += hdumps(key_entry,
+ _current_indent=_current_indent + 2)
+ is_first = False
+ result += "\n" + " " * _current_indent + "}"
+ return result
return json.dumps(entry)
- if isinstance(entry, list) and entry:
- result = "[ " + hdumps(entry[0], _current_indent=_current_indent+2)
- for x in entry[1:]:
- result += "\n" + " " * _current_indent + ", "
- result += hdumps(x, _current_indent=_current_indent+2)
- result += "\n" + " " * _current_indent + "]"
- return result
- if isinstance(entry, dict) and entry:
- result = "{ "
- is_first = True
- for k in entry.keys():
- if not is_first:
- result += "\n" + " " * _current_indent + ", "
- result += json.dumps(k) + ":"
- if is_simple(entry[k]):
- result += " " + json.dumps(entry[k])
- elif is_short(entry[k], _current_indent + len(json.dumps(k)) + 4):
- result += " " + json.dumps(entry[k])
- else:
- result += "\n" + " " * _current_indent + " "
- result += hdumps(entry[k], _current_indent=_current_indent+2)
- is_first = False
- result += "\n" + " " * _current_indent + "}"
- return result
- return json.dumps(entry)
+
if __name__ == "__main__":
- data = json.load(sys.stdin)
- print(hdumps(data))
+ data = json.load(sys.stdin)
+ print(hdumps(data))