"""
graph.py create an accessible graph
"""
import json
import sys
from bs4 import BeautifulSoup as bs
def original(graph):
html = "
"
for x in graph:
html += f"- " + x["name"] + f": from, to"
if "subitems" in x:
html += "
"
for y in x["subitems"]:
html += f"- " + y["name"] + f": from, to" + "
"
html += "
"
html += " "
html += "
"
return html
def flatten(graph):
new = list()
for n in graph:
new.append(n)
if "subitems" in n:
for m in n["subitems"]:
new.append(m)
return new
def sntn(graph, short_name):
for n in flatten(graph):
if n["short_name"] == short_name:
return n["name"]
return None
def connections_to(graph):
def _conns(node):
html = f"" + f"{node['name']}"
if "cons" in node:
html += ""
html += ""
return html
html = ""
for n in flatten(graph):
html += _conns(n)
html += "
"
return html
def get_reverse_connections(sn, graph):
cons = list()
for n in flatten(graph):
if "cons" in n and sn in n["cons"]:
cons.append(n["short_name"])
return cons
def connections_from(graph):
def _conns(node):
html = f"" + f"{node['name']}"
html += ""
for c in get_reverse_connections(node["short_name"], graph):
html += f"- " + f"{sntn(graph, c)}" + "
"
html += "
"
return html
html = ""
for n in flatten(graph):
html += _conns(n)
html += "
"
return html
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Must specify .json file")
exit()
f = open(sys.argv[1], "r")
graph = json.loads(f.read())
html = ""
html += "Graph
"
html += "The ideal solution to this would actually be to turn the graph into a 3D braille model so it can be printed. Due to conditions of remoteness and the time required to do such a task though, this is not possible right now.
"
html += "Please do not navigate to connections manually. Use the links.
"
html += "Nodes of Graph
"
html += original(graph)
html += "Graph Connections (to)
"
html += connections_to(graph)
html += "Graph Connections (form)
"
html += connections_from(graph)
html += ""
soup = bs(html, "html.parser")
print(soup.prettify())