-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
428 lines (349 loc) · 17 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import hashlib
import json
import os
import re
import shutil
import tempfile
import urllib.request
import zipfile
import time
from os.path import join
from pathlib import Path
from boto3.session import Session
from special_escape import generate_encoder, generate_printer
_ = join
replace_key_pattern_map = {
"DYNAMIC_NAME_1": "[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_1_ADJ": "[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_2": "[target_name.GetFirstName]ア",
"DYNAMIC_NAME_2_ADJ": "[target_name.GetFirstName]ア",
"DYNAMIC_NAME_3": "[target_name.GetLastWordInDynastyName]ランド",
"DYNAMIC_NAME_3_ADJ": "[target_name.GetLastWordInDynastyName]ランド",
"DYNAMIC_NAME_4": "[target_name.GetLastWordInDynastyName]ン",
"DYNAMIC_NAME_4_ADJ": "[target_name.GetLastWordInDynastyName]ン",
"DYNAMIC_NAME_5": "[target_name.GetFirstName]スタン",
"DYNAMIC_NAME_5_ADJ": "[target_name.GetFirstName]スタン",
"DYNAMIC_NAME_6": "[target_name.GetLastWordInDynastyName]リア",
"DYNAMIC_NAME_6_ADJ": "[target_name.GetLastWordInDynastyName]リア",
"DYNAMIC_NAME_7": "[target_name.GetLastWordInDynastyName]ニア",
"DYNAMIC_NAME_7_ADJ": "[target_name.GetLastWordInDynastyName]ニア",
"DYNAMIC_NAME_8": "[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_8_ADJ": "[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_9": "[target_name.GetFirstName]ランド",
"DYNAMIC_NAME_9_ADJ": "[target_name.GetFirstName]ランド",
"DYNAMIC_NAME_10": "[target_name.GetLastWordInDynastyName]",
"DYNAMIC_NAME_10_ADJ": "[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_11": "上[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_11_ADJ": "上[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_12": "下[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_12_ADJ": "下[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_13": "小[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_13_ADJ": "小[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_14": "大[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_14_ADJ": "大[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_15": "[target_name.GetLastWordInDynastyName]クス",
"DYNAMIC_NAME_15_ADJ": "[target_name.GetLastWordInDynastyName]クス",
"DYNAMIC_NAME_16": "[target_name.GetLastWordInDynastyName]ヴァニア",
"DYNAMIC_NAME_16_ADJ": "[target_name.GetLastWordInDynastyName]ヴァニア",
"DYNAMIC_NAME_17": "[target_name.GetLastWordInDynastyName]ニ",
"DYNAMIC_NAME_17_ADJ": "[target_name.GetLastWordInDynastyName]ニ",
"DYNAMIC_NAME_18": "低[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_18_ADJ": "低[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_19": "高[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_19_ADJ": "高[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_20": "新[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_20_ADJ": "新[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_21": "古[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_21_ADJ": "古[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_22": "中央[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_22_ADJ": "中央[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_23": "ノヴァ・[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_23_ADJ": "ノヴァ・[target_name.GetLastWordInDynastyName]ア",
"DYNAMIC_NAME_24": "[target_name.GetLastWordInDynastyName]ン",
"DYNAMIC_NAME_24_ADJ": "[target_name.GetLastWordInDynastyName]ン",
"DYNAMIC_NAME_25": "[target_name.GetLastWordInDynastyName]リク",
"DYNAMIC_NAME_25_ADJ": "[target_name.GetLastWordInDynastyName]リク",
"DYNAMIC_NAME_26": "[target_name.GetLastWordInDynastyName]ニア",
"DYNAMIC_NAME_26_ADJ": "[target_name.GetLastWordInDynastyName]ニア",
"DYNAMIC_NAME_27": "[target_name.GetLastWordInDynastyName]ケニア",
"DYNAMIC_NAME_27_ADJ": "[target_name.GetLastWordInDynastyName]ケニア",
}
def download_trans_zip_from_paratranz(project_id,
secret,
out_file_path,
base_url="https://paratranz.cn"):
"""
paratranzからzipをダウンロードする。ダウンロード前にre-generateする
:param project_id:
:param secret:
:param base_url:
:param out_file_path:
:return:
"""
regenerate_request_url = "{}/api/projects/{}/artifacts".format(base_url, project_id)
req = urllib.request.Request(regenerate_request_url, method="POST")
req.add_header("Authorization", secret)
with urllib.request.urlopen(req) as response:
print(response.read().decode("utf-8"))
# wait for regenerate
time.sleep(90)
request_url = "{}/api/projects/{}/artifacts/download".format(base_url, project_id)
req = urllib.request.Request(request_url)
req.add_header("Authorization", secret)
with open(out_file_path, "wb") as my_file:
my_file.write(urllib.request.urlopen(req).read())
return out_file_path
def build_dynasties_csv_from_raw(in_dir_path,
out_dir_path):
"""
raw jsonからymlを組み立てる
:param in_dir_path: 入力パス
:param out_dir_path: 出力パス
:return:
"""
encoder = generate_encoder("ck2", "txt")
printer = generate_printer("ck2", "txt")
def subf(m):
t = m.group(1)
if re.search(r"[あ-んア-ン一-鿐]", t) is not None:
return "name = \"" + t + "✐" + "\""
else:
return "name = \"" + t + "\""
for f in Path(in_dir_path).glob("00_dynasties.txt"):
# TXTを読み込んでcsvに変換
with open(str(f), "rt", encoding='utf-8') as fr:
text = fr.read()
result = re.sub(r'name\s*=\s*"([^"]+)"', subf, text)
# ファイルに保存
encode = encoder(src_array=map(ord, result))
printer(src_array=encode, out_file_path=_(out_dir_path, f.name))
def build_yml_from_raw_json(in_dir_path,
out_dir_path,
pattern_map):
"""
raw jsonからymlを組み立てる
:param in_dir_path: 入力パス
:param out_dir_path: 出力パス
:param pattern_map: マッチキーパターン
:return:
"""
encoder = generate_encoder("ck2", "csv")
printer = generate_printer("ck2", "csv")
os.makedirs(out_dir_path, exist_ok=True)
for f in Path(in_dir_path).glob("HolyFury.csv.json"):
result = ["#CODE;ENGLISH;FRENCH;GERMAN;;SPANISH;;;;;;;;;x"]
# JSONを読み込んでymlに変換
with open(str(f), "rt", encoding='utf-8') as fr:
for item in json.loads(fr.read()):
key = item['key']
original = item['original']
translation = item['translation']
if key in pattern_map:
text = pattern_map[key]
elif translation == "":
text = original
else:
text = translation
result.append('{};{};;;;;;;;;;;;x'.format(key, text))
# ファイルに保存
root = "HolyFury.csv"
encode = encoder(src_array=map(ord, "\n".join(result)))
printer(src_array=encode, out_file_path=_(out_dir_path, root))
def assembly_app_mod_zip_file(resource_image_file_path,
resource_paratranz_trans_zip_file_path,
out_file_path):
"""
Appモッドを作成
:param resource_paratranz_trans_zip_file_path: Paratranzからダウンロードできるzipファイルのパス
:param resource_image_file_path: 画像ファイルパス
:param out_file_path: 出力ファイルパス
:return:
"""
with tempfile.TemporaryDirectory() as temp_dir_path:
# 画像ファイル
shutil.copy(resource_image_file_path, temp_dir_path)
# history
salvage_files_from_paratranz_trans_zip(out_dir_path=_(temp_dir_path, "history"),
folder_list=["history"],
paratranz_zip_path=resource_paratranz_trans_zip_file_path,
head_folder_name="special")
# common
salvage_files_from_paratranz_trans_zip(out_dir_path=_(temp_dir_path, "common"),
folder_list=["common"],
paratranz_zip_path=resource_paratranz_trans_zip_file_path,
head_folder_name="special")
with tempfile.TemporaryDirectory() as temp_dir_path2:
# common/
salvage_files_from_paratranz_trans_zip(out_dir_path=_(temp_dir_path2, "common"),
folder_list=["common"],
paratranz_zip_path=resource_paratranz_trans_zip_file_path,
head_folder_name="utf8")
# localisation
salvage_files_from_paratranz_trans_zip(out_dir_path=_(temp_dir_path2, "localisation"),
folder_list=["localisation"],
paratranz_zip_path=resource_paratranz_trans_zip_file_path,
head_folder_name="raw")
build_dynasties_csv_from_raw(in_dir_path=_(temp_dir_path2, "common", "dynasties"),
out_dir_path=_(temp_dir_path, "common", "dynasties"))
build_yml_from_raw_json(in_dir_path=_(temp_dir_path2, "localisation"),
out_dir_path=_(temp_dir_path, "localisation"),
pattern_map=replace_key_pattern_map)
# zip化する
return shutil.make_archive(out_file_path, 'zip', root_dir=temp_dir_path)
def salvage_files_from_paratranz_trans_zip(out_dir_path,
paratranz_zip_path,
folder_list,
head_folder_name):
with zipfile.ZipFile(paratranz_zip_path) as paratranz_zip:
special_files = filter(lambda name: name.startswith(head_folder_name + "/"), paratranz_zip.namelist())
with tempfile.TemporaryDirectory() as temp_dir_path:
paratranz_zip.extractall(path=temp_dir_path, members=special_files)
for folder in folder_list:
shutil.copytree(_(temp_dir_path, head_folder_name, folder), out_dir_path)
def generate_dot_mod_file(mod_title_name,
mod_file_name,
mod_tags,
mod_image_file_path,
out_dir_path,
mod_user_dir_name=None):
"""
.mod.modファイルを作る
:param mod_title_name:
:param mod_file_name: zipファイルの名前(.zipを含まない)
:param mod_user_dir_name:ユーザ作業ディレクトリ名
:param mod_tags: Set<String>型
:param mod_image_file_path:
:param out_dir_path: 出力ディレクトリのパス
:return: 出力ファイルパス
"""
os.makedirs(out_dir_path, exist_ok=True)
out_file_path = _(out_dir_path, "{}.mod.mod".format(mod_file_name))
if mod_user_dir_name is None:
mod_user_dir_name = mod_file_name
with open(out_file_path, "w", encoding="utf-8") as fw:
lines = [
'name="{}"'.format(mod_title_name),
'archive="mod/{}.zip"'.format(mod_file_name),
'user_dir="{}"'.format(mod_user_dir_name),
'tags={}'.format("{" + " ".join(mod_tags) + "}"),
'picture="{}"'.format(mod_image_file_path)
]
fw.write("\n".join(lines))
return out_file_path
def generate_distribution_file(url,
mod_file_path,
out_file_path):
"""
trielaで使用する配布用設定ファイルを作成する。
:param url:
:param mod_file_path:
:param out_file_path:
:return:
"""
with open(mod_file_path, 'rb') as fr:
md5 = hashlib.md5(fr.read()).hexdigest()
d_new = {'file_md5': md5,
'url': url,
'file_size': os.path.getsize(mod_file_path)}
with open(out_file_path, "w", encoding="utf-8") as fw:
json.dump(d_new, fw, indent=2, ensure_ascii=False)
def upload_mod_to_s3(upload_file_path,
name,
bucket_name,
access_key,
secret_access_key,
region):
"""
S3にファイルをアップロードする
:param upload_file_path:
:param name:
:param bucket_name:
:param access_key:
:param secret_access_key:
:param region:
:return: CDNのURL
"""
session = Session(aws_access_key_id=access_key,
aws_secret_access_key=secret_access_key,
region_name=region)
s3 = session.resource('s3')
s3.Bucket(bucket_name).upload_file(upload_file_path, name)
return "{}/{}".format("https://d3fxmsw7mhzbqi.cloudfront.net", name)
def pack_mod(out_file_path,
mod_zip_path,
mod_title_name,
mod_file_name,
mod_tags,
mod_image_file_path,
mod_user_dir_name=None):
with tempfile.TemporaryDirectory() as temp_dir_path:
# .mod.modファイルを作成する
generate_dot_mod_file(
mod_title_name=mod_title_name,
mod_file_name=mod_file_name,
mod_tags=mod_tags,
mod_user_dir_name=mod_user_dir_name,
mod_image_file_path=mod_image_file_path,
out_dir_path=temp_dir_path)
# zipをコピー
shutil.copy(mod_zip_path, _(temp_dir_path, "{}.zip".format(mod_file_name)))
return shutil.make_archive(out_file_path, 'zip', root_dir=temp_dir_path)
def update_source(resource_paratranz_trans_zip_file_path):
shutil.rmtree("source", ignore_errors=True)
os.makedirs("source", exist_ok=True)
salvage_files_from_paratranz_trans_zip(out_dir_path=_("source", "history"),
folder_list=["history"],
paratranz_zip_path=resource_paratranz_trans_zip_file_path,
head_folder_name="utf8")
salvage_files_from_paratranz_trans_zip(out_dir_path=_("source", "common"),
folder_list=["common"],
paratranz_zip_path=resource_paratranz_trans_zip_file_path,
head_folder_name="utf8")
def main():
# 一時フォルダ用意
os.makedirs(_(".", "tmp"), exist_ok=True)
os.makedirs(_(".", "out"), exist_ok=True)
# 翻訳の最新版をダウンロードする
p_file_path = download_trans_zip_from_paratranz(
project_id=91,
secret=os.environ.get("PARATRANZ_SECRET"),
out_file_path=_(".", "tmp", "paratranz.zip"))
print("p_file_path:{}".format(p_file_path))
# AppModを構築する
app_mod_zip_file_path = assembly_app_mod_zip_file(
resource_paratranz_trans_zip_file_path=p_file_path,
resource_image_file_path=_(".", "resource", "title.jpg"),
out_file_path=_(".", "tmp", "mod"))
print("app_mod_zip_file_path:{}".format(app_mod_zip_file_path))
# packする
mod_pack_file_path = pack_mod(
out_file_path=_(".", "out", "ck2_ap2_mod"),
mod_file_name="jpmod_ap2_mod",
mod_zip_path=app_mod_zip_file_path,
mod_title_name="JPMOD Sub 1: Character Names",
mod_tags={"Translation", "Localisation"},
mod_image_file_path="title.jpg",
mod_user_dir_name="JLM")
print("mod_pack_file_path:{}".format(mod_pack_file_path))
# S3にアップロード from datetime import datetime as dt
from datetime import datetime as dt
cdn_url = upload_mod_to_s3(
upload_file_path=mod_pack_file_path,
name=dt.now().strftime('%Y-%m-%d_%H-%M-%S-{}'.format("ck2-ap2")),
bucket_name="triela-file",
access_key=os.environ.get("AWS_S3_ACCESS_KEY"),
secret_access_key=os.environ.get("AWS_S3_SECRET_ACCESS_KEY"),
region="ap-northeast-1")
print("cdn_url:{}".format(cdn_url))
# URLはリンク先に置く
print("::set-output name=download_url::{}".format(cdn_url))
# distributionファイルを生成する
generate_distribution_file(url=cdn_url,
out_file_path=_(".", "out", "dist.v2.json"),
mod_file_path=mod_pack_file_path)
# utf8ファイルを移動する(この後git pushする)
update_source(resource_paratranz_trans_zip_file_path=p_file_path)
if __name__ == "__main__":
main()