55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
from rich.console import Console
|
|
from rich.tree import Tree
|
|
import json
|
|
import sys
|
|
|
|
def style_value(value):
|
|
if isinstance(value, str):
|
|
return f"[green]\"{value}\"[/]"
|
|
elif isinstance(value, (int, float)):
|
|
return f"[yellow]{value}[/]"
|
|
elif value is None:
|
|
return "[red]null[/]"
|
|
elif isinstance(value, bool):
|
|
return f"[red]{value}[/]"
|
|
else:
|
|
return f"[cyan]{value}[/]"
|
|
|
|
def build_tree(data, tree):
|
|
if isinstance(data, dict):
|
|
for key, value in data.items():
|
|
if isinstance(value, (dict, list)):
|
|
branch = tree.add(f"[bold magenta]{key}[/]")
|
|
build_tree(value, branch)
|
|
else:
|
|
tree.add(f"[bold magenta]{key}[/]: {style_value(value)}")
|
|
elif isinstance(data, list):
|
|
for value in data:
|
|
if isinstance(value, (dict, list)):
|
|
branch = tree.add(f"[bold magenta]-[/]")
|
|
build_tree(value, branch)
|
|
else:
|
|
tree.add(style_value(value))
|
|
|
|
def main():
|
|
file_path = sys.argv[1]
|
|
with open(file_path, 'r') as f:
|
|
data = json.load(f)
|
|
|
|
console = Console(record=True, force_terminal=True, color_system="truecolor")
|
|
root = Tree(f"[bold white on blue]📁 {file_path}[/]")
|
|
build_tree(data, root)
|
|
|
|
console.print(root)
|
|
|
|
# Export HTML colored view (for browser)
|
|
with open("ci-reports/markdown/json-tree-view.html", "w") as f:
|
|
f.write(console.export_html(clear=False))
|
|
|
|
# Also export plain text if needed
|
|
with open("ci-reports/markdown/json-tree-view.txt", "w") as f:
|
|
f.write(console.export_text(clear=False))
|
|
|
|
if __name__ == "__main__":
|
|
main()
|