forked from HarbourMasters/Ghostship
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatyamls.py
More file actions
127 lines (109 loc) · 3.96 KB
/
formatyamls.py
File metadata and controls
127 lines (109 loc) · 3.96 KB
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
import ruamel.yaml
import re
import sys
import getopt
import os
def convert_offset_to_hex(yaml_file):
yaml = ruamel.yaml.YAML()
with open(yaml_file, 'r') as f:
data = yaml.load(f)
if data is not None:
for node_key, node_data in data.items():
if 'offset' in node_data:
node_data['offset'] = ruamel.yaml.scalarint.HexCapsInt(node_data['offset'])
elif 'mio0' in node_data:
node_data['mio0'] = ruamel.yaml.scalarint.HexCapsInt(node_data['mio0'])
with open(yaml_file, 'w') as f:
yaml.dump(data, f)
def sort_nodes(data):
if data is None:
return data
res = {}
initial_dict = {}
for key in data.keys():
if "offset" in data[key]:
initial_dict[key] = data[key]["offset"]
sorted_dict = dict(sorted(initial_dict.items(), key=lambda item: item[1]))
for new_key in sorted_dict.keys():
res[new_key] = data[new_key]
return res
def sort(yaml_file):
yaml = ruamel.yaml.YAML()
with open(yaml_file, 'r') as f:
data = yaml.load(f)
if data is not None:
with open(yaml_file, 'w') as f:
yaml.dump(sort_nodes(data), f)
def fix_segments_format(yaml_file):
with open (yaml_file, "r") as f:
s = f.read()
ret = re.sub(r" - - ([0-9xA-F]+)\n. - ([0-9xA-F]+)",r" - [\1, \2]", s)
with open(yaml_file, 'w') as f:
f.write(ret)
def format(yaml_file):
with open (yaml_file, "r") as f:
s = f.read()
ret = re.sub(r"([A-Za-z0-9]+)\n([\.A-Za-z0-9_-]+):",r"\1\n\n\2:", s)
ret = re.sub(r"\n\n([\n])+",r"\n\n", ret)
with open(yaml_file, 'w') as f:
f.write(ret)
def grab_config(yaml_file):
with open (yaml_file, "r") as f:
s = f.read()
matches = re.finditer(r":config:(\n [^\n]+)+", s)
# Return the matched sections
output = ""
for match in matches:
output += match.group(0) # Entire matched section
ret = re.sub(r":config:(\n [^\n]+)+",r"", s)
with open(yaml_file, 'w') as f:
f.write(ret)
return output
def re_add_config(yaml_file, config):
with open (yaml_file, "r") as f:
s = f.read()
ret = ""
if config != "":
ret += config + "\n\n"
ret += s
ret = re.sub(r"\n\n([\n])+",r"\n\n", ret)
with open(yaml_file, 'w') as f:
f.write(ret)
def process_file(yaml_file):
config = grab_config(yaml_file)
convert_offset_to_hex(yaml_file)
sort(yaml_file)
convert_offset_to_hex(yaml_file)
fix_segments_format(yaml_file)
format(yaml_file)
re_add_config(yaml_file, config)
def main(argv):
opts, args = getopt.getopt(argv,"hd:f:",["help","dir=","file="])
for opt, arg in opts:
if opt in ("-h", "--help"):
print("This is a tool for formatting Torch's asset yamls, and sorts the assets by offset.\n")
print(" -d / --dir : root directory of asset yamls, which will be walked to find all yaml files to format.")
print(" -f / --file : path to specific yaml file you want to format.")
sys.exit()
elif opt in ("-d", "--dir"):
dir = arg
for dirpath, subdirs, files in os.walk(dir):
for x in files:
file = os.path.join(dirpath, x)
if ".yml" not in file and ".yaml" not in file:
continue
# Skip sound yamls and dialog strings
if "sound/" in file or "strings.yml" in file:
continue
process_file(file)
sys.exit()
elif opt in ("-f", "--file"):
if ".yml" in arg or ".yaml" in arg:
process_file(arg)
else:
print("Provided file is not a yaml file. Please check the extension matches '.yml' or '.yaml'")
sys.exit()
print("\nInvalid arguments. Use -h / --help to find out more.\n")
sys.exit()
if __name__ == "__main__":
main(sys.argv[1:])