Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
116 lines (96 loc) · 4.16 KB
/
Copy pathmain.py
File metadata and controls
116 lines (96 loc) · 4.16 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
import argparse
import json
import os
import re
import shutil
import zipfile
from io import BytesIO
import babel
import requests
from babel.messages.frontend import CommandLineInterface
from info import info
from mods import mod_names
from modules.factorio import FactorioModGetter
from modules.localization import Localizer
def sync_mod_locale(_mod_getter, _mod_names):
for mod in _mod_getter.get_mods(_mod_names):
mod_zip = zipfile.ZipFile(BytesIO(mod))
regex = re.compile(r".*(locale/.*\.cfg)")
for _info in mod_zip.infolist():
matched = regex.match(_info.orig_filename)
if matched is not None:
filename = matched.group(1)
_info.filename = filename
mod_zip.extract(_info)
def zipdir(path, ziph):
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(root, file))
if __name__ == '__main__':
# Here we monkeypatch this to completely disable python_format.
# noinspection PyPropertyAccess
babel.messages.catalog.Message.python_format = False
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help="Specify a subcommand", dest="subcommand", required=True)
parser_sync = subparsers.add_parser('sync', help='Sync modules.')
parser_sync.add_argument('username',
help='The username(not email address) of your Factorio account.')
parser_sync.add_argument('password',
help='The password of your Factorio account.')
parser_sync.add_argument('-p', '--proxy',
help="Specify a proxy to use. Format: http://host:port")
parser_extract = subparsers.add_parser('extract', help='extract translation content.')
parser_extract.add_argument('locale',
help='Choose the locale template you want to generate.')
parser_render = subparsers.add_parser('render', help='render locale.')
parser_render.add_argument('locale',
help='Choose the locale you want to render.')
parser_release = subparsers.add_parser('release', help="release a mod.")
args = parser.parse_args()
if args.subcommand == "sync":
username = args.username
password = args.password
session = requests.Session()
if args.proxy:
proxy = args.proxy
proxies = {
"http": proxy,
"https": proxy
}
session.proxies.update(proxies)
mod_getter = FactorioModGetter(username, password, session)
try:
sync_mod_locale(mod_getter, mod_names)
except ValueError as e:
print(e)
elif args.subcommand in ["extract", "render"]:
locale = args.locale
if args.subcommand == "extract":
localizer = Localizer()
localizer.generate_template("locale/en")
CommandLineInterface().run(
['pybabel', 'extract', '-F', 'babel.cfg', '-o', 'messages.pot', '.'])
if os.path.exists(f"lang/{locale}"):
CommandLineInterface().run(
['pybabel', 'update', '-i', 'messages.pot', '-d', 'lang', '-l', locale])
else:
CommandLineInterface().run(
['pybabel', 'init', '-i', 'messages.pot', '-d', 'lang', '-l', locale])
os.remove("messages.pot")
else:
CommandLineInterface().run(
['pybabel', 'compile', '-f', '-d', 'lang'])
localizer = Localizer()
localizer.render_locale(locale)
elif args.subcommand == "release":
version = info["version"]
dir_name = "bobsmodslocale" + "_" + version
shutil.rmtree(dir_name, ignore_errors=True)
os.makedirs(dir_name, exist_ok=True)
shutil.copytree("locale", os.path.join(dir_name, "locale"))
with open(os.path.join(dir_name, "info.json"), "w") as f:
json.dump(info, f, indent=4)
with zipfile.ZipFile(dir_name + '.zip', 'w', zipfile.ZIP_DEFLATED) as f:
zipdir(dir_name, f)
shutil.rmtree(dir_name, ignore_errors=True)