-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
executable file
·165 lines (111 loc) · 5.17 KB
/
helper.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#/usr/bin/env python3
import argparse
import json
import os
class SafeDict(dict):
def __missing__(self, key):
return "{" + key + "}"
def create_dump(contents: dict) -> dict:
dump = {}
for key, val in contents.items():
for k, v in val.items():
if key not in dump:
try:
dump[key] = {k: {q: "" for q in v.keys()}}
except AttributeError:
dump[key] = {k: ""}
elif k not in dump[key]:
try:
dump[key][k] = {q: "" for q in v.keys()}
except AttributeError:
dump[key][k] = ""
else:
try:
dump[key][k] = {q: "" for q in v.keys()}
except AttributeError:
dump[key] = {k: ""}
return dump
cog_template = """from discord.ext import commands
class {cog_name}(commands.Cog):
def __init__(self, bot) -> None:
self.bot = bot
self.t = self.bot.i18n.t
self.log = self.bot.logger.log
async def cog_load(self) -> None:
print("Loaded {name} cog!".format(name=self.__class__.__name__))
async def cog_unload(self) -> None:
print("Unloaded {name} cog!".format(name=self.__class__.__name__))
async def setup(bot) -> None:
await bot.add_cog({cog_name}(bot))
async def teardown(bot) -> None:
await bot.remove_cog("{cog_name}")"""
def get_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--add", help="This command has 2 possible modes: (add) cog and (add) translation. Add cog will add a cog file with a template and add translate will add a translation file. All input should be added without file extensions.", nargs=2, type=str, required=False)
parser.add_argument("-c", "--copy", help="This command will copy the keys of one translation file to another. Example: -c en.misc es.misc. Can also copy all the keys. Example: -c en.* es.*. File names may need to be inside of quotes when copying all the keys. All input should be added without file extensions.", nargs=2, type=str, required=False)
return parser.parse_args()
def main() -> None:
args = get_args()
add = args.add
copy = args.copy
if add:
mode = add[0].lower()
name = add[1].lower()
if mode in ["cog", "ext", "extension", "c"]:
path = f"./cogs/{name}.py"
if not os.path.isfile(path):
with open(path, "w") as f:
f.write(cog_template.format_map(SafeDict(cog_name=name.capitalize())))
print(f"Created {path}")
else:
print(f"{path} already exists!")
elif mode in ["trans", "translation", "t"]:
lang = name.split(".")[0]
path = f"./i18n/translations/{lang}/"
file_path = path + name[3:] + ".json"
if not os.path.isdir(path):
print(f"Creating {path} directory...")
os.mkdir(path)
if os.path.isfile(file_path): return print(f"{file_path} already exists!")
with open(file_path, "w") as f:
f.write("{}")
print(f"Created {file_path}!")
elif copy:
original = copy[0].lower()
new = copy[1].lower()
if original.split(".")[1] == "*" and new.split(".")[1] == "*":
original = original.split(".")[0]
new = new.split(".")[0]
path = "./i18n/translations/" + original
new_path = "./i18n/translations/" + new
if not os.path.isdir(new_path):
os.mkdir(new_path)
print(f"Created {new_path}!")
for file in os.listdir(path):
if os.path.isfile(os.path.join(new_path, file)):
print(f"{new_path}/{file} already exists, skipping!")
continue
with open(path + "/" + file, "r") as f:
contents = json.load(f)
dump = create_dump(contents)
with open((path + "/" + file).replace(original, new), "w") as f:
json.dump(dump, f, indent=4)
return print(f"Succesfully created {new} translations from {original} translations!")
original_lang = original.split(".")[0]
new_lang = new.split(".")[0]
original_path = f"./i18n/translations/{original_lang}"
new_path = f"./i18n/translations/{new_lang}"
original_file_path = os.path.join(original_path, original[3:] + ".json")
new_file_path = os.path.join(new_path, new[3:] + ".json")
if not os.path.isdir(new_path):
os.mkdir(new_path)
print(f"Created {new_path}!")
if os.path.isfile(new_file_path): return print(f"{new_file_path} already exists!")
with open(original_file_path, "r") as f:
contents = json.load(f)
dump = create_dump(contents)
with open(new_file_path, "w") as f:
json.dump(dump, f, indent=4)
print(f"Created {new_path} with the keys of {original_path}!")
if __name__ == "__main__":
main()