2019-11-27 15:01:47 +00:00
|
|
|
#!/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')
|
2019-11-27 15:01:47 +00:00
|
|
|
except IndexError:
|
2020-06-04 09:40:25 +00:00
|
|
|
print("Usage: ./h2json.py msg_has_us.h")
|
|
|
|
sys.exit(1)
|
2019-11-27 15:01:47 +00:00
|
|
|
|
2020-06-04 09:40:25 +00:00
|
|
|
if h_filename == 'msg_hash_lbl.h':
|
|
|
|
print("Skip")
|
|
|
|
sys.exit(0)
|
2019-11-27 15:01:47 +00:00
|
|
|
|
2020-06-04 09:40:25 +00:00
|
|
|
p = re.compile(r'MSG_HASH\(\s*\/?\*?.*\*?\/?\s*[a-zA-Z0-9_]+\s*,\s*\".*\"\s*\)')
|
2019-11-27 15:01:47 +00:00
|
|
|
|
|
|
|
def parse_message(message):
|
2020-06-04 09:40:25 +00:00
|
|
|
key_start = max(message.find('(') + 1, message.find('*/') + 2)
|
2020-08-07 19:46:17 +00:00
|
|
|
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
|
2019-11-27 15:01:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2020-08-07 19:46:17 +00:00
|
|
|
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)
|
2019-11-27 15:01:47 +00:00
|
|
|
except EnvironmentError:
|
2020-06-04 09:40:25 +00:00
|
|
|
print('Cannot read/write ' + h_filename)
|