-
Notifications
You must be signed in to change notification settings - Fork 2
/
tx-set-context.py
120 lines (100 loc) · 3.93 KB
/
tx-set-context.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
import argparse
import requests
import json
import csv
import sys
import os
import re
from hashlib import md5
from configparser import ConfigParser
assert sys.version_info >= (3, 6), "Python >= 3.6 is required"
def gen_source_string_hash(string_key):
string_key = string_key.replace('.', '\\.')
keys = [string_key, '']
return md5(':'.join(keys).encode('utf-8')).hexdigest()
def load_transifexrc():
config = ConfigParser()
config.read(os.path.expanduser("~/.transifexrc"))
username = config.get('https://www.transifex.com', 'username')
password = config.get('https://www.transifex.com', 'password')
return (username, password)
def format_payload(
source_string_hash,
character_limit=None,
comment=None,
tags=None):
payload = {
'source_string_hash': source_string_hash,
'character_limit': character_limit,
}
if comment is not None:
payload['comment'] = comment
if tags is not None:
payload['tags'] = tags
return payload
def put_resource(payloads,
project_slug,
resource_slug,
auth=None):
url = "https://www.transifex.com/api/2/project/{}/resource/{}/source/".format(project_slug, resource_slug)
return requests.put(url, json=payloads, auth=auth)
def tx_set_context(in_path, project_slug, resource_slug, auth):
d = {}
with open(in_path) as csvfile:
csv_reader = csv.reader(csvfile)
payloads = []
# Skip header
next(csv_reader)
for row in csv_reader:
comment = ""
character_limit = None
key = row[0]
text = row[1]
max_len = row[2]
# Ignore any columns after the third
if key in d:
raise RuntimeError("Duplicate key {}".format(key))
if key.endswith('Plural'):
comment += 'This is the plural form.'
if key.endswith('Singular'):
comment += 'This is the singular form.'
if 'ooni' in text.lower():
comment += 'Do not translate OONI.'
if 'middlebox' in text.lower() or 'middleboxes' in text.lower():
comment += 'Do not translate middlebox or middleboxes.'
if max_len != "":
character_limit = int(max_len)
source_string_hash = gen_source_string_hash(key)
if character_limit and character_limit < len(text):
print("WARNING: character limit too short for {}".format(key))
print("Setting it to len() + 1")
character_limit = len(text) + 1
payload = format_payload(
source_string_hash=source_string_hash,
character_limit=character_limit,
comment=comment)
payloads.append(payload)
d[key] = None
resp = put_resource(payloads,
project_slug=project_slug,
resource_slug=resource_slug,
auth=auth)
if resp.status_code != 200:
print(resp.text)
raise RuntimeError("Failed to update string")
print("Updating {} resources".format(len(payloads)))
print(resp.text)
return d
def parse_args():
p = argparse.ArgumentParser(description='translations: CSV to KEYVALUEJSON')
p.add_argument('--csv', metavar='PATH', help='path to the input CSV file', required=True)
p.add_argument('--project', metavar='PROJECT_SLUG', help='the name of the project on transifex (ex. ooniprobe)', required=True)
p.add_argument('--resource', metavar='RESOURCE_SLUG', help='the name of the project on transifex (ex. strings-json)', required=True)
opt = p.parse_args()
return opt
def main():
opt = parse_args()
auth = load_transifexrc()
tx_set_context(in_path=opt.csv, project_slug=opt.project, resource_slug=opt.resource, auth=auth)
if __name__ == "__main__":
main()