-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_parser.py
90 lines (86 loc) · 2.51 KB
/
project_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from graphviz import Digraph
def to_dot(tree):
g = Digraph(format='svg')
g.node_attr['shape'] = 'box'
g.node_attr['style'] = 'rounded'
g.graph_attr['rankdir'] = 'BT'
g.graph_attr['splines'] = 'ortho'
g.edge_attr['arrowhead'] = 'none'
#leafs = set()
#nonleafs = set()
def draw_subtree(tree):
for node in tree:
if type(node) == str:
yield node
last_node = node
continue
for node in draw_subtree(node):
g.edge(node, last_node)
#leafs.add(node)
#nonleafs.add(last_node)
with g.subgraph() as s:
s.attr(rank='same')
for node in draw_subtree(tree):
s.node(node)
#leafs -= nonleafs
#with g.subgraph() as s:
# s.attr(rank='same')
# for leaf in leafs:
# s.node(leaf)
return g
def parse_tree(arg):
lines = arg.split('\n') if type(arg) == str else arg
tree_stack = []
indent_stack = [0]
tree = []
indent = 0
for line in lines:
line = line.rstrip()
sline = line.lstrip()
if not sline:
continue
indent = len(line) - len(sline)
while indent < indent_stack[-1]:
tree_stack[-1].append(tree)
tree = tree_stack.pop()
indent_stack.pop()
if indent > indent_stack[-1]:
tree_stack.append(tree)
indent_stack.append(indent)
tree = [sline]
elif indent == indent_stack[-1]:
tree.append(sline)
while tree_stack:
tree_stack[-1].append(tree)
tree = tree_stack.pop()
return tree
if __name__ == '__main__':
import sys
fname = sys.argv[1]
if len(sys.argv) >= 3:
subproject = sys.argv[2]
else:
subproject = None
with open(fname) as f:
code = f.read()
tree = parse_tree(code)
if subproject:
it_tree = iter(tree)
projects = []
for node in it_tree:
if node == subproject:
try:
subtree = next(it_tree)
except StopIteration:
tree = [node]
else:
tree = [node, subtree]
break
elif type(node) == str:
projects.append(node)
else:
print(f'No subproject {subproject}.', file=sys.stderr)
print('Choose from:', *projects, file=sys.stderr)
sys.exit(1)
g = to_dot(tree)
print(g.source)