RetroArch/intl/h2json.py

51 lines
1.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# Convert *.h to *.json
# Usage: ./h2json.py msg_has_us.h
import re
import sys
import json
try:
2020-06-04 09:40:25 +00:00
h_filename = sys.argv[1]
json_filename = h_filename.replace('.h', '.json')
except IndexError:
2020-06-04 09:40:25 +00:00
print("Usage: ./h2json.py msg_has_us.h")
sys.exit(1)
2020-06-04 09:40:25 +00:00
if h_filename == 'msg_hash_lbl.h':
print("Skip")
sys.exit(0)
2020-06-04 09:40:25 +00:00
p = re.compile(r'MSG_HASH\(\s*\/?\*?.*\*?\/?\s*[a-zA-Z0-9_]+\s*,\s*\".*\"\s*\)')
def parse_message(message):
2020-06-04 09:40:25 +00:00
key_start = max(message.find('(') + 1, message.find('*/') + 2)
key_end = message.find(',', key_start)
2020-06-04 09:40:25 +00:00
key = message[key_start:key_end].strip()
value_start = message.find('"') + 1
value_end = message.rfind('"')
value = message[value_start:value_end]
return key, value
try:
2020-06-04 09:40:25 +00:00
with open(h_filename, 'r+') as h_file:
text = h_file.read()
result = p.findall(text)
seen = set()
messages = {}
for msg in result:
key, val = parse_message(msg)
if not key.startswith('MENU_ENUM_LABEL_VALUE_LANG_') and val:
2020-07-31 07:50:17 +00:00
messages[key] = val.replace('\\\"', '"') # unescape
if key not in seen:
seen.add(key)
else:
print("Duplicate key: " + key)
2020-06-04 09:40:25 +00:00
with open(json_filename, 'w') as json_file:
json.dump(messages, json_file, indent=2)
except EnvironmentError:
2020-06-04 09:40:25 +00:00
print('Cannot read/write ' + h_filename)