2019-09-25 12:07:17 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2019-11-27 15:01:47 +00:00
|
|
|
# Convert *.json to *.h
|
|
|
|
# Usage: ./json2h.py msg_hash_fr.json
|
2019-09-25 12:07:17 +00:00
|
|
|
|
|
|
|
import re
|
|
|
|
import sys
|
2019-11-27 15:01:47 +00:00
|
|
|
import json
|
2019-09-25 12:07:17 +00:00
|
|
|
|
|
|
|
try:
|
2019-11-27 15:01:47 +00:00
|
|
|
json_filename = sys.argv[1]
|
|
|
|
h_filename = json_filename.replace('.json', '.h')
|
2019-09-25 14:08:14 +00:00
|
|
|
except IndexError:
|
2019-09-25 12:07:17 +00:00
|
|
|
print("Usage: ./template.py <language_postfix>")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2020-01-04 19:56:06 +00:00
|
|
|
if json_filename == 'msg_hash_us.json':
|
|
|
|
print("Skip")
|
|
|
|
sys.exit(0)
|
2019-09-25 12:07:17 +00:00
|
|
|
|
2020-01-04 19:56:06 +00:00
|
|
|
p = re.compile('MSG_HASH\(\s*[A-Z0-9_]+,\s*\".*\"\s*\)')
|
2019-09-25 12:07:17 +00:00
|
|
|
|
2019-11-27 15:01:47 +00:00
|
|
|
|
2019-09-25 12:07:17 +00:00
|
|
|
def parse_message(message):
|
|
|
|
key_start = message.find('(') + 1
|
|
|
|
key_end = message.find(',')
|
|
|
|
key = message[key_start:key_end].strip()
|
|
|
|
value_start = message.find('"') + 1
|
|
|
|
value_end = message.rfind('"')
|
|
|
|
value = message[value_start:value_end].strip()
|
|
|
|
return key, value
|
|
|
|
|
2019-11-27 15:01:47 +00:00
|
|
|
|
|
|
|
def parse_messages(text):
|
2019-09-25 12:07:17 +00:00
|
|
|
result = p.findall(text)
|
2019-09-25 14:20:17 +00:00
|
|
|
seen = set()
|
2019-09-25 12:07:17 +00:00
|
|
|
msg_list = []
|
|
|
|
for msg in result:
|
|
|
|
key, val = parse_message(msg)
|
|
|
|
item = {'key': key, 'val': val, 'msg': msg}
|
|
|
|
msg_list.append(item)
|
2019-09-25 14:20:17 +00:00
|
|
|
if key not in seen:
|
|
|
|
seen.add(key)
|
|
|
|
else:
|
|
|
|
print("Duplicate key: " + key)
|
|
|
|
|
2019-09-25 12:07:17 +00:00
|
|
|
return msg_list
|
|
|
|
|
2019-11-27 15:01:47 +00:00
|
|
|
|
|
|
|
def update(messages, template):
|
2020-01-04 19:56:06 +00:00
|
|
|
new_translation = template
|
2019-11-27 15:01:47 +00:00
|
|
|
template_messages = parse_messages(template)
|
2019-09-25 12:07:17 +00:00
|
|
|
for tp_msg in template_messages:
|
2019-11-27 15:01:47 +00:00
|
|
|
if tp_msg['key'] in messages:
|
|
|
|
tp_msg_val = tp_msg['val']
|
|
|
|
tl_msg_val = messages[tp_msg['key']]
|
|
|
|
old_msg = tp_msg['msg']
|
|
|
|
new_msg = old_msg.replace(tp_msg_val, tl_msg_val)
|
|
|
|
new_translation = new_translation.replace(old_msg, new_msg)
|
2019-09-25 12:07:17 +00:00
|
|
|
return new_translation
|
|
|
|
|
2019-11-27 15:01:47 +00:00
|
|
|
|
2019-09-25 12:07:17 +00:00
|
|
|
with open('msg_hash_us.h', 'r') as template_file:
|
|
|
|
template = template_file.read()
|
2019-09-25 14:08:14 +00:00
|
|
|
try:
|
2019-11-27 15:01:47 +00:00
|
|
|
with open(json_filename, 'r+') as json_file:
|
|
|
|
messages = json.load(json_file)
|
|
|
|
new_translation = update(messages, template)
|
|
|
|
with open(h_filename, 'w') as h_file:
|
|
|
|
h_file.seek(0)
|
|
|
|
h_file.write(new_translation)
|
|
|
|
h_file.truncate()
|
2019-09-25 14:08:14 +00:00
|
|
|
except EnvironmentError:
|
2019-11-27 15:01:47 +00:00
|
|
|
print('Cannot read/write ' + json_filename)
|