-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuilder.py
42 lines (34 loc) · 1.12 KB
/
builder.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
from typing import List, Union, cast
class Builder:
def __init__(self, name: str):
self.name = name
self.lines: List[Union[str, Builder]] = [] # list of strings or children builders
self.indentation = 0
def newLine(self, line=""):
self.lines.append((self.indentation * " ") + line)
return self
# returns a reference to the child builder
def newBlock(self):
child = Builder(self.name)
child.indentation = self.indentation
self.lines.append(child)
return child
def addText(self, text=""):
if len(self.lines) == 0 or isinstance(self.lines[-1], Builder):
self.newLine()
self.lines[-1] = cast(str, self.lines[-1]) + text
return self
def indent(self):
self.indentation += 1
return self
def unindent(self):
self.indentation -= 1
return self
def emit(self) -> str:
lines = []
for l in self.lines:
if isinstance(l, str):
lines.append(l)
else:
lines.append(l.emit())
return "\n".join(lines)