-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrowdin_parse.py
More file actions
129 lines (103 loc) · 3.8 KB
/
crowdin_parse.py
File metadata and controls
129 lines (103 loc) · 3.8 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
128
129
#!/usr/bin/python3
import csv
import os
import re
import shutil
import subprocess
import configparser
import click
config = configparser.RawConfigParser()
config.read('crowdin.cfg')
try:
TEMPLATE = config.get('TEMPLATES', 'template')
ITEM_TEMPLATE = config.get('TEMPLATES', 'item-template')
KEYS = dict(config.items('KEYS'))
BASE_PATH = config.get('PATHS', 'base-path')
PATH = config.get('PATHS', 'path')
FILENAME = config.get('FILE', 'filename')
FILE_EXT = config.get('FILE', 'extension')
ONLY_FILES = config.get('FILE', 'only_files')
except configparser.NoOptionError as e:
click.secho(e.message, fg='red')
exit()
@click.group()
def cli():
"""Simple Crowdin parser"""
pass
def _mkdir(dir):
if not os.path.exists(dir):
os.makedirs(dir)
@cli.command()
@click.argument("namespace", nargs=1)
def download(namespace):
"""Download and extract project"""
if namespace in KEYS:
directory = os.path.join(BASE_PATH, 'translations_source', namespace)
url = "https://api.crowdin.com/api/project/%s/download/all.zip?key=%s" % (namespace, KEYS[namespace])
_mkdir(directory)
subprocess.Popen(['wget', url, '-O', namespace + '.zip'], cwd=directory).wait()
subprocess.Popen(['unzip', '-o', '-q', namespace + '.zip'], cwd=directory).wait()
else:
click.secho("%s not found in projects.json file." % namespace, fg='red')
exit()
def parse(path, namespace, filename):
with open(path) as csv_file:
if ONLY_FILES and filename not in ONLY_FILES:
return
reader = csv.DictReader(csv_file)
path_list = path.split('/')
lang = path_list[path_list.index('translations_source') + 2]
directory = BASE_PATH + PATH.format(lang=lang)
if not FILENAME:
filename = re.sub(r'(\'|&| )', '', filename)
result = os.path.join(directory, filename.replace('csv', FILE_EXT))
else:
result = os.path.join(directory, FILENAME.format(
lang=lang
)) + '.' + FILE_EXT
_mkdir(directory)
with open(result, 'w') as file:
str_keys = ''
for row in reader:
key = row['key-id'].replace('"', "")
translated = row['source'] if not row['translation'] else row['translation']
str_keys += ITEM_TEMPLATE.format(
key=key,
value=translated.replace('"', "'").splitlines()[0],
n='\n',
t='\t'
)
file.write(TEMPLATE.format(
array=str_keys,
n='\n'
))
@cli.command()
@click.argument('namespace', nargs=1)
def convert(namespace):
"""Convert project to language files"""
path = os.path.join(BASE_PATH, 'translations_source', namespace)
if os.path.isdir(path):
for (dirpath, dirnames, filenames) in os.walk(path):
for filename in [f for f in filenames if f.endswith(".csv")]:
parse(os.path.join(dirpath, filename), namespace, filename)
click.secho("%s successfully converted." % namespace, fg='green')
else:
click.secho("%s not found in source folder. You need to download in first." % namespace, fg='red')
exit()
@cli.command()
def cleanup():
"""Delete 'translations_source' folder"""
SOURCE_DIR = os.path.join(BASE_PATH, 'translations_source')
if os.path.exists(SOURCE_DIR):
shutil.rmtree(SOURCE_DIR)
@cli.command()
@click.pass_context
def run(ctx):
"""Download and convert all projects"""
for namespace in KEYS:
ctx.invoke(download, namespace=namespace)
ctx.invoke(convert, namespace=namespace)
ctx.invoke(cleanup)
click.secho("All done!", fg='green', bold=True)
if __name__ == '__main__':
run()