100 lines
1.7 KiB
Plaintext
100 lines
1.7 KiB
Plaintext
|
#!/usr/bin/env python3
|
||
|
|
||
|
import sys
|
||
|
import getopt
|
||
|
import re
|
||
|
|
||
|
matching = {'<': '>', '(': ')'}
|
||
|
|
||
|
|
||
|
def find_matching(block):
|
||
|
o = block[0]
|
||
|
c = matching[block[0]]
|
||
|
i = 1
|
||
|
d = 1
|
||
|
while d != 0:
|
||
|
if block[i] == o:
|
||
|
d = d + 1
|
||
|
elif block[i] == c:
|
||
|
d = d - 1
|
||
|
i = i + 1
|
||
|
|
||
|
return i
|
||
|
|
||
|
|
||
|
def help(argv):
|
||
|
cmd=argv[0]
|
||
|
print(f"{cmd} [-h] [-l <limit>] [-f <filter>]...")
|
||
|
|
||
|
|
||
|
def cli(argv):
|
||
|
filters = []
|
||
|
limit = 64
|
||
|
substs = {
|
||
|
'std::mersenne_twister_engine<[^>]*>': 'RNG',
|
||
|
}
|
||
|
try:
|
||
|
opts, args = getopt.getopt(argv[1:], "hf:l:", ["limit=", "filter="])
|
||
|
except getopt.GetoptError:
|
||
|
help(argv)
|
||
|
sys.exit(2)
|
||
|
for opt, arg in opts:
|
||
|
if opt == '-h':
|
||
|
help(argv)
|
||
|
sys.exit()
|
||
|
elif opt in ("-f", "--filter"):
|
||
|
filters.append(arg)
|
||
|
elif opt in ("-l", "--limit"):
|
||
|
limit = int(arg)
|
||
|
|
||
|
return [filters, limit, substs]
|
||
|
|
||
|
|
||
|
def process(limit, line, d = 0):
|
||
|
i = 0
|
||
|
print(" "*d, end='')
|
||
|
while i < len(line):
|
||
|
c = line[i]
|
||
|
if c == '(':
|
||
|
trail = line[i:]
|
||
|
e = find_matching(trail)
|
||
|
print(trail[:e], end='')
|
||
|
i = i + e - 1
|
||
|
elif c == '<':
|
||
|
trail = line[i:]
|
||
|
e = find_matching(trail)
|
||
|
if e > limit:
|
||
|
print("<")
|
||
|
process(limit, trail[1:e-1], d+1)
|
||
|
print("\n"+" "*d+">", end='')
|
||
|
else:
|
||
|
print(trail[:e], end='')
|
||
|
i = i + e - 1
|
||
|
elif c == ',':
|
||
|
print(c)
|
||
|
print(" "*d, end='')
|
||
|
elif c != ' ':
|
||
|
print(c, end='')
|
||
|
|
||
|
i = i + 1
|
||
|
|
||
|
|
||
|
def applyReplaces(line, substs):
|
||
|
for fr, to in substs.items():
|
||
|
line = re.sub(fr, to, line)
|
||
|
|
||
|
return line
|
||
|
|
||
|
|
||
|
[filters, limit, substs] = cli(sys.argv)
|
||
|
lines = sys.stdin.readlines()
|
||
|
lines = [item.rstrip() for item in lines if item != "\n"]
|
||
|
data = "".join(lines)
|
||
|
data = applyReplaces(data, substs)
|
||
|
|
||
|
for f in filters:
|
||
|
data = data.replace(f, "")
|
||
|
|
||
|
process(limit, data)
|
||
|
print()
|