Skip to content

Instantly share code, notes, and snippets.

@bigyihsuan
Last active December 15, 2021 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigyihsuan/03d2a2b6a930dbf91198f66fee96d836 to your computer and use it in GitHub Desktop.
Save bigyihsuan/03d2a2b6a930dbf91198f66fee96d836 to your computer and use it in GitHub Desktop.
parseTree.py
import re
from pprint import pprint
with open('SkyhawkScienceSystem.cfg') as tree:
s = tree.read().strip()
l = re.findall('\{.+?\}', s, re.DOTALL)
props = [[k.strip() for k in e.split('\n')] for e in l]
props = [list(filter(lambda e: e not in '{}', node)) for node in props]
# pprint(props)
# props = [list(filter(lambda s: s != 'Parent', node)) + [f"parents = {node.count('Parent')}"] for node in props]
props = [list(filter(lambda s: s != 'Parent', node)) for node in props]
props = [list(map(lambda s: tuple(s.split(' = ')), node)) for node in props]
props = [list(filter(lambda t: t[0] in ['id', 'parentID', 'parents'], node)) for node in props]
newProps = []
for prop in props:
if prop[0][0] == 'parentID':
newProps[-1] += prop
else:
newProps.append(prop)
props = newProps
# props = [node + nextNode + nextNextNode if len(nextNode) == 1 and len(nextNextNode) == 1 else node + nextNode if len(
# nextNode) == 1 else node for node, nextNode, nextNextNode in zip(props, props[1:], props[2:])]
props = [node for node in props if len(node) != 1]
props = [[node[0], node[1:]] for node in props]
props = [(node[0][1], [parent[1] for parent in node[1]]) for node in props]
props = dict(props)
pprint(props)
mermaid = "graph TD;\n"
for node, parents in props.items():
for parent in parents:
mermaid += f" {parent} --> {node}\n"
with open('tree.merm', 'w') as merm:
merm.write(mermaid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment