-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
98 lines (75 loc) · 3.34 KB
/
main.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
import os
import hjson_parser
def list_paths(directory, is_file):
for path in os.listdir(directory):
if os.path.isdir(directory + "\\" + path) != is_file:
yield directory + "\\" + path
class Methods:
name = ""
bundle = []
category = ["", ""]
def parse_f(self, file):
if "mod." in file:
self.name = hjson_parser.parser.parse(open(file, "r").read())["name"]
if not ("content" in file): return
if not ((".json" in file) or (".hjson" in file)): return
name = hjson_parser.get(file, "\\")
n_n = name.count("\\")
while n_n > 0:
n_n = name.count("\\")
name = hjson_parser.get(name, "\\")
p_type = hjson_parser.get(file, start="content\\", end="\\")
p_name = hjson_parser.get(name, end=".")
file_dict = hjson_parser.parser.parse((open(file, "r").read() + "\n").replace("]\n", "\n]\n"), log_p)
if "localizedName" in file_dict.keys():
p_lname = file_dict["localizedName"]
else:
p_lname = " ".join([n.capitalize() for n in p_name.split("-")])
line_n = p_type + "." + self.name + "-" + p_name + ".name = " + p_lname
# print(line_n)
self.bundle.append(line_n)
if "description" in file_dict.keys():
line_d = p_type + "." + self.name + "-" + p_name + ".description = " + file_dict["description"]
# print(line_d)
self.bundle.append(line_d.replace("\n", "\\n"))
if "details" in file_dict.keys():
line_d = p_type + "." + self.name + "-" + p_name + ".details = " + file_dict["details"]
self.bundle.append(line_d.replace("\n", "\\n"))
def parse_d(self, dir):
if "content\\" in dir:
new_category = ["", ""]
folder = hjson_parser.get(dir, "content\\")
new_category[0] = hjson_parser.get(folder, end="\\")
new_category[1] = hjson_parser.get(folder, new_category[0]+"\\", "\\")
NSOF = bool(len(self.bundle)) # Not Start Of File
if new_category[0] != self.category[0]:
self.bundle.append("\n\n"*NSOF+"# "+new_category[0].capitalize())
NFC = ([""]+self.bundle)[-1].replace("\n", "")[:2] != "# " # Not First Category
if new_category[1] != self.category[1]:
if new_category[1] == "":
new_category[1] = "other"
self.bundle.append("\n"*NFC+"## "+new_category[1].capitalize())
self.category = new_category
for f in list_paths(dir, True): # files
try:
self.parse_f(f)
except Exception:
raise Exception(f"in {f}") # print file that occurred error
for d in list_paths(dir, False): # dirs
self.parse_d(d)
def parse_a(self, d):
self.parse_d(d)
if __name__ == "__main__":
log_p = False
log_s = True
m = Methods()
d = input("put json mod directory: ")
while not os.path.isdir(d):
d = input("\nthis is invalid directory.\nput valid json mod directory: ")
print()
m.parse_a(d)
if not os.path.exists(d+"\\bundles"):
os.mkdir(d+"\\bundles")
with open(d+"\\bundles\\bundle_new.properties", "w") as tf:
tf.write("\n".join(m.bundle))
print(f"Successful created {d}\\bundles\\bundle_new.properties")