-
Notifications
You must be signed in to change notification settings - Fork 14
/
invert_match.py
112 lines (82 loc) · 2.75 KB
/
invert_match.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
#!/usr/bin/env python3
# usage: python3 invert_match.py <match_location> <inverted_match_location>
import sys
def invert(path, new_path):
with open(path) as match_file:
string = match_file.read()
lines = string.splitlines()
parse(lines, new_path)
match_file.close()
def parse(lines: list[str], new_path):
match_infos = {
"title": lines.pop(0),
"a": [],
"b": [],
"cp": [],
"cp_a": [],
"cp_b": [],
"lines": []
}
lines, match_infos = category(lines, "a", match_infos)
lines, match_infos = category(lines, "b", match_infos)
lines, match_infos = category(lines, "cp", match_infos)
lines, match_infos = category(lines, "cp_a", match_infos)
lines, match_infos = category(lines, "cp_b", match_infos)
while len(lines) > 0:
line = lines.pop(0)
match_infos["lines"].append(line.split("\t"))
match_infos = invert_object(match_infos)
write(new_path, match_infos)
def write(new_path, obj):
lines = [
obj["title"],
"\ta:"
] + obj["a"] + ["\tb:"] + obj["b"] + ["\tcp:"] + obj["cp"] + ["\tcp a:"] + obj["cp_a"] + ["\tcp b:"] + obj[
"cp_b"] + obj["lines"]
txt = ""
for i in lines:
txt += i + "\n"
with open(new_path, 'w') as newFile:
newFile.write(txt)
newFile.close()
def category(lines: list[str], name: str, obj: dict):
if lines[0].startswith("\t" + name.replace("_", " ")):
lines.pop(0)
line = lines.pop(0)
while line.startswith("\t\t"):
obj[name].append(line)
line = lines.pop(0)
lines.insert(0, line)
return lines, obj
def invert_object(match_infos: dict):
match_infos["c"] = match_infos["a"]
match_infos["a"] = match_infos["b"]
match_infos["b"] = match_infos["c"]
match_infos["cp_c"] = match_infos["cp_a"]
match_infos["cp_a"] = match_infos["cp_b"]
match_infos["cp_b"] = match_infos["cp_c"]
for i in range(0, len(match_infos["lines"])):
line = match_infos["lines"][i]
if line[0] == "c":
line[0] = line[1]
line[1] = line[2]
line[2] = line[0]
line[0] = "c"
elif line[1] == "m" or line[1] == "f":
line[0] = line[2]
line[2] = line[3]
line[3] = line[0]
line[0] = ""
elif line[2] == "ma":
line[0] = line[3]
line[3] = line[4]
line[4] = line[0]
line[0] = ""
str_line = ""
for g in line:
str_line += g + "\t"
str_line = str_line.removesuffix("\t")
match_infos["lines"][i] = str_line
return match_infos
if __name__ == '__main__':
invert(sys.argv[1], sys.argv[2])