-
Notifications
You must be signed in to change notification settings - Fork 0
/
ascii-flowchart-generator.py
266 lines (204 loc) · 8.86 KB
/
ascii-flowchart-generator.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import ast
import astpp
import numpy as np
class FlowchartNode:
def __init__(self, parent):
self.parent = parent
self.child = None
self.row = None
self.col = None # for display logic
def __str__(self):
return "base flowchart node?"
class StartNode(FlowchartNode):
def __init__(self, text):
self.name = text
super().__init__(None)
self.inputs = []
self.outputs = []
def __str__(self):
return "( start )"
class InputNode(FlowchartNode):
def __init__(self, parent, varName):
self.name = varName
super().__init__(parent)
def __str__(self):
return "/ input {} /".format(self.name)
class OutputNode(FlowchartNode):
def __init__(self, parent, varName):
self.name = varName
super().__init__(parent)
def __str__(self):
return "/ output {} /".format(self.name)
class ProcessNode(FlowchartNode):
def __init__(self, parent, text):
self.text = text
super().__init__(parent)
def __str__(self):
return "[ {} ]".format(self.text)
class VariableAssignmentNode(ProcessNode):
def __init__(self, parent, varName, expression):
text = "{} = {}".format(varName, expression)
super().__init__(parent, text)
class ConditionalNode(FlowchartNode):
def __init__(self, parent, condition):
self.condition = condition
super().__init__(parent)
self.child = {"No": None, "Yes": None}
def __str__(self):
return "< {} >".format(self.condition)
class SubProcessNode(FlowchartNode):
def __init__(self, parent, subprocessName):
self.name = subprocessName
super().__init__(parent)
def __str__(self):
return "[ | {} | ]".format(self.name)
class DummyConjunctionNode(FlowchartNode): # node to allow two nodes to return to the same place
def __init__(self, parent1, parent2):
self.parent2 = parent2
super().__init__(parent1)
def __str__(self):
return " | <---"
class DummyMiddleNode(FlowchartNode):
def __str__(self):
return ''
class FlowchartMakingVisitor(ast.NodeVisitor):
operators = {ast.Mult: '*', ast.Add: '+', ast.Sub: '-', ast.Div: '/', ast.Or: 'or', ast.And: 'and', ast.Gt: '>', ast.Lt: '<', ast.Eq: '=', ast.NotEq: '!='}
def __init__(self):
self.start = StartNode("start")
self.currentParent = self.start
def visit_Module(self, node: ast.Module):
for n in node.body:
self.visit(n)
def visit_Assign(self, node):
if isinstance(node.value, ast.Call): # special case for / input / nodes
if node.value.func.id == 'input':
self.appendNode(InputNode(self.currentParent, node.targets[0].id))
return
if isinstance(node.value.args[0], ast.Call) and node.value.args[0].func.id == 'input': # basically, assume that any single function wrapping an input() statement in an assign is just a typecast
self.appendNode(InputNode(self.currentParent, node.targets[0].id))
return
rhs = self.parseChunk(node.value)
self.appendNode(VariableAssignmentNode(self.currentParent, node.targets[0].id, rhs))
def visit_AugAssign(self, node):
rhs = self.parseChunk(node.value)
self.appendNode(VariableAssignmentNode(self.currentParent, node.target.id, "{0} {1} {2}".format(node.target.id, self.operators[node.op.__class__], rhs)))
def visit_Expr(self, node):
if isinstance(node.value, ast.Call):
if node.value.func.id in {'print', 'pprint'}: # / output / special casing
self.appendNode(OutputNode(self.currentParent, ', '.join(self.parseFunctionArgs(node.value))))
else:
self.appendNode(ProcessNode(self.currentParent, self.parseFunctionCall(node.value)))
else:
raise Exception("Unknown Node type in Expr: {0} (line {0.lineno} col {0.col_offset})".format(node.value))
def visit_If(self, node):
cond = self.parseChunk(node.test)
condNode = ConditionalNode(self.currentParent, cond)
self.appendNode(condNode)
condNode.child['Yes'] = DummyMiddleNode(condNode)
self.currentParent = condNode.child['Yes']
for n in node.body:
self.visit(n)
endbody = self.currentParent # so we can connect it to the endcap eventually
condNode.child['No'] = DummyMiddleNode(condNode)
self.currentParent = condNode.child['No']
for n in node.orelse:
self.visit(n)
endcap = DummyConjunctionNode(self.currentParent, endbody)
endbody.child = endcap
self.currentParent.child = endcap
self.currentParent = endcap
def visit_ImportFrom(self, node):
pass
def generic_visit(self, node):
raise Exception("Unknown Node type: {0} (line {0.lineno} col {0.col_offset})".format(node))
def appendNode(self, node):
self.currentParent.child = node
self.currentParent = node
def parseFunctionCall(self, call):
name = call.func.id
args = self.parseFunctionArgs(call)
return "{0}({1})".format(name, ', '.join(map(str, args)))
def parseFunctionArgs(self, call):
args = []
for arg in call.args:
args.append(self.parseChunk(arg))
return args
def parseBinOp(self, op):
left = self.parseChunk(op.left)
right = self.parseChunk(op.right)
return "{0} {1} {2}".format(left, self.operators[op.op.__class__], right)
def parseBoolOp(self, op):
things = []
for thing in op.values:
things.append(self.parseChunk(thing))
return (' ' + self.operators[op.op.__class__] + ' ').join(things)
def parseCompare(self, op):
str = self.parseChunk(op.left)
for i in range(len(op.ops)):
str += ' ' + self.operators[op.ops[i].__class__] + ' ' + self.parseChunk(op.comparators[i])
return str
def parseChunk(self, o): # parse pretty much anything
if isinstance(o, ast.BinOp):
return '(' + self.parseBinOp(o) + ')'
elif isinstance(o, ast.Num):
return str(o.n)
elif isinstance(o, ast.Name):
return o.id
elif isinstance(o, ast.Str):
return repr(o.s)
elif isinstance(o, ast.Call):
return self.parseFunctionCall(o)
elif isinstance(o, ast.BoolOp):
return '(' + self.parseBoolOp(o) + ')'
elif isinstance(o, ast.Compare):
return '(' + self.parseCompare(o) + ')'
else:
raise Exception("Unknown object to parse: {0} (line {0.lineno} col {0.col_offset})".format(o))
blockWidth = 60
blockHeight = 3
# This stuff is all for printing the graph to the console. It doesn't work well with conditionals, but arrow drawing gets really really complicated quickly. Use the graphviz version instead
def followNodePath(node, col=0, row=0):
if node.col is not None:
return
node.col = col
node.row = row
if node.child:
if isinstance(node.child, dict):
if isinstance(node.child["No"], DummyMiddleNode):
node.child["No"] = node.child["No"].child
if isinstance(node.child["Yes"], DummyMiddleNode):
node.child["Yes"] = node.child["Yes"].child
followNodePath(node.child["No"], col, row + 1)
followNodePath(node.child["Yes"], col + 1, row)
else:
followNodePath(node.child, col, row + 1)
def nodeToText(po, node):
po[node.row * 3][node.col * blockWidth:(node.col + 1) * blockWidth] = list(map(ord, ("{0:^" + str(blockWidth) + "}").format(str(node))))
po[node.row * 3 + 1][node.col * blockWidth:(node.col + 1) * blockWidth] = list(map(ord, ("{0:^" + str(blockWidth) + "}").format("|")))
po[node.row * 3 + 2][node.col * blockWidth:(node.col + 1) * blockWidth] = list(map(ord, ("{0:^" + str(blockWidth) + "}").format("V")))
if isinstance(node.child, dict):
e1 = nodeToText(po, node.child['Yes'])
e2 = nodeToText(po, node.child['No'])
return max(e1, e2)
else:
if node.child is None:
return (node.row + 1) * 3
else:
return nodeToText(po, node.child)
tree = ast.parse(open('test.py', 'r').read())
print(tree)
print(astpp.dump(tree))
visitor = FlowchartMakingVisitor()
visitor.visit(tree)
start = visitor.start
followNodePath(start) # mark each node with it's position in the printout
# print layout
# each column: 10 chars for up arrow space, then 50 chars padded for node space
# each node: 1 line for node itself, 2 lines of arrow (TODO: maybe special case conjunction nodes?)
printout = np.zeros((1000, 1000), dtype=np.uint8)
lastLine = nodeToText(printout, start)
printout[lastLine][0:60] = list(map(ord, "{0:^60}".format('( stop )')))
for line in printout:
l = ''.join(map(chr, np.trim_zeros(line)))
if l:
print(l)