Created
August 16, 2019 03:51
-
-
Save noize-e/889712370d726db5dac6c48926141f19 to your computer and use it in GitHub Desktop.
Parse JSON maps for Jit.js Interactive Maps & Graphviz Diagrams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import pprint | |
import sys | |
if sys.argv[1] is None: | |
print "JSON source not defined" | |
exit() | |
# e.g. _data/source-tree-templates.json | |
source_map = json.load(open(sys.argv[1])) | |
# Enum: graph | json | |
output = str(sys.argv[2] or "json") | |
_src_types = { | |
"directory": { | |
'prefix': 'd', | |
'counter': 0 | |
}, | |
"file": { | |
'prefix': 'f', | |
'counter': 0 | |
}, | |
} | |
def format_items(items): | |
nodes = [] | |
for item in items: | |
src = _src_types[item["type"]] | |
counter = src["counter"] + 1 | |
_src_types[item["type"]].update({ | |
"counter": counter | |
}) | |
node = { | |
"id": src["prefix"] + str(counter), | |
"name": item["name"] | |
} | |
print(item["name"]) | |
try: | |
print("children: ") | |
node.update({ | |
"children": format_items(item["contents"]) | |
}) | |
except Exception as e: | |
pass | |
nodes.append(node) | |
return nodes | |
def format_items_grahiz(parent, items): | |
nodes = [] | |
for item in items: | |
src = _src_types[item["type"]] | |
print("\"%s\" -> \"%s\"" % (parent, item["name"])) | |
try: | |
format_items_grahiz(item["name"], item["contents"]) | |
except Exception as e: | |
pass | |
if output == "graph": | |
format_items_grahiz("app", source_map) | |
if output == "json": | |
map_dump = json.dumps(format_items(source_map), sort_keys=False, indent=2) | |
nodes_json = open("_data/suite-dirs-level2.json", "w") | |
for line in map_dump: | |
nodes_json.write(line) | |
nodes_json.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment