mirror of
https://github.com/libretro/RetroArch
synced 2024-07-03 00:38:44 +00:00
adjust h2json and json2h scripts to be comment agnostic (#12532)
Co-authored-by: Michael Burgardt <michael.burgardt@rwth-aachen.de>
This commit is contained in:
parent
8b6e58eebd
commit
10f0073778
|
@ -18,20 +18,39 @@ if h_filename == 'msg_hash_lbl.h':
|
|||
print("Skip")
|
||||
sys.exit(0)
|
||||
|
||||
p = re.compile(r'MSG_HASH\(\s*\/?\*?.*\*?\/?\s*[a-zA-Z0-9_]+\s*,\s*\".*\"\s*\)')
|
||||
p = re.compile(
|
||||
r'MSG_HASH\s*(?:\/\*(?:.|[\r\n])*?\*\/\s*)*\(\s*(?:\/\*(?:.|[\r\n])*?\*\/\s*)*[a-zA-Z0-9_]+\s*(?:\/\*(?:.|[\r\n])*?\*\/\s*)*,\s*(?:\/\*(?:.|[\r\n])*?\*\/\s*)*\".*\"\s*(?:\/\*(?:.|[\r\n])*?\*\/\s*)*\)')
|
||||
|
||||
|
||||
def parse_message(message):
|
||||
key_start = max(message.find('(') + 1, message.find('*/') + 2)
|
||||
key_end = message.find(',', key_start)
|
||||
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
|
||||
a = message.find('/*')
|
||||
b = message.find('*/')
|
||||
c = message.find('"')
|
||||
new_msg = message
|
||||
while (a >= 0 and b >= 0) and (a < c < b or b < c):
|
||||
new_msg = new_msg[:a] + new_msg[b + 2:]
|
||||
c = new_msg.find('"', a)
|
||||
b = new_msg.find('*/', a)
|
||||
a = new_msg.find('/*', a)
|
||||
# get key word
|
||||
word = new_msg[new_msg.find('(') + 1:new_msg.find(',')].strip()
|
||||
|
||||
a = new_msg.rfind('/*')
|
||||
b = new_msg.rfind('*/')
|
||||
d = new_msg.rfind('"')
|
||||
while (a >= 0 and b >= 0) and (a < d < b or a > d):
|
||||
new_msg = new_msg[:a]
|
||||
a = new_msg.rfind('/*')
|
||||
b = new_msg.rfind('*/')
|
||||
d = new_msg.rfind('"')
|
||||
# get value
|
||||
value = new_msg[c + 1:d]
|
||||
|
||||
return word, value
|
||||
|
||||
|
||||
try:
|
||||
with open(h_filename, 'r+') as h_file:
|
||||
with open(h_filename, 'r+', encoding='utf-8') as h_file:
|
||||
text = h_file.read()
|
||||
result = p.findall(text)
|
||||
seen = set()
|
||||
|
@ -44,7 +63,7 @@ try:
|
|||
seen.add(key)
|
||||
else:
|
||||
print("Duplicate key: " + key)
|
||||
with open(json_filename, 'w') as json_file:
|
||||
with open(json_filename, 'w', encoding='utf-8') as json_file:
|
||||
json.dump(messages, json_file, indent=2)
|
||||
except EnvironmentError:
|
||||
print('Cannot read/write ' + h_filename)
|
||||
|
|
|
@ -18,7 +18,9 @@ if json_filename == 'msg_hash_us.json' or json_filename == 'msg_hash_lbl.json':
|
|||
print("Skip")
|
||||
sys.exit(0)
|
||||
|
||||
p = re.compile(r'MSG_HASH\(\s*\/?\*?.*\*?\/?\s*[a-zA-Z0-9_]+\s*,\s*\".*\"\s*\)')
|
||||
p = re.compile(
|
||||
r'MSG_HASH\s*(?:\/\*(?:.|[\r\n])*?\*\/\s*)*\(\s*(?:\/\*(?:.|[\r\n])*?\*\/\s*)*[a-zA-Z0-9_]+\s*(?:\/\*(?:.|[\r\n])*?\*\/\s*)*,\s*(?:\/\*(?:.|[\r\n])*?\*\/\s*)*\".*\"\s*(?:\/\*(?:.|[\r\n])*?\*\/\s*)*\)')
|
||||
|
||||
|
||||
def c89_cut(old_str):
|
||||
new_str = ''
|
||||
|
@ -30,14 +32,34 @@ def c89_cut(old_str):
|
|||
new_str += c
|
||||
return new_str
|
||||
|
||||
|
||||
def parse_message(message):
|
||||
key_start = max(message.find('(') + 1, message.find('*/') + 2)
|
||||
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]
|
||||
return key, value
|
||||
# remove all comments before the value (= the string)
|
||||
a = message.find('/*')
|
||||
b = message.find('*/')
|
||||
c = message.find('"')
|
||||
new_msg = message
|
||||
while (a >= 0 and b >= 0) and (a < c < b or b < c):
|
||||
new_msg = new_msg[:a] + new_msg[b + 2:]
|
||||
c = new_msg.find('"', a)
|
||||
b = new_msg.find('*/', a)
|
||||
a = new_msg.find('/*', a)
|
||||
# get key word
|
||||
word = new_msg[new_msg.find('(') + 1:new_msg.find(',')].strip()
|
||||
|
||||
# remove all comments after the value (= the string)
|
||||
a = new_msg.rfind('/*')
|
||||
b = new_msg.rfind('*/')
|
||||
d = new_msg.rfind('"')
|
||||
while (a >= 0 and b >= 0) and (a < d < b or a > d):
|
||||
new_msg = new_msg[:a]
|
||||
a = new_msg.rfind('/*')
|
||||
b = new_msg.rfind('*/')
|
||||
d = new_msg.rfind('"')
|
||||
# get value
|
||||
value = new_msg[c + 1:d]
|
||||
|
||||
return word, value
|
||||
|
||||
|
||||
def parse_messages(text):
|
||||
|
@ -52,11 +74,12 @@ def parse_messages(text):
|
|||
seen.add(key)
|
||||
else:
|
||||
print("Duplicate key: " + key)
|
||||
|
||||
return msg_list
|
||||
|
||||
|
||||
def update(messages, template, source_messages):
|
||||
new_translation = template
|
||||
translation = template
|
||||
template_messages = parse_messages(template)
|
||||
for tp_msg in template_messages:
|
||||
old_msg = tp_msg['msg']
|
||||
|
@ -66,16 +89,16 @@ def update(messages, template, source_messages):
|
|||
tl_msg_val = tl_msg_val.replace('"', '\\\"').replace('\n', '') # escape
|
||||
if tp_msg['key'].find('_QT_') < 0:
|
||||
tl_msg_val = c89_cut(tl_msg_val)
|
||||
# Replace last match, incase the key contains the value string
|
||||
# Replace last match, in case the key contains the value string
|
||||
new_msg = old_msg[::-1].replace(tp_msg_val[::-1], tl_msg_val[::-1], 1)[::-1]
|
||||
new_translation = new_translation.replace(old_msg, new_msg)
|
||||
# Remove English duplicates and non-translateable strings
|
||||
translation = translation.replace(old_msg, new_msg)
|
||||
# Remove English duplicates and non-translatable strings
|
||||
else:
|
||||
new_translation = new_translation.replace(old_msg + '\n', '')
|
||||
return new_translation
|
||||
translation = translation.replace(old_msg + '\n', '')
|
||||
return translation
|
||||
|
||||
|
||||
with open('msg_hash_us.h', 'r') as template_file:
|
||||
with open('msg_hash_us.h', 'r', encoding='utf-8') as template_file:
|
||||
template = template_file.read()
|
||||
with open('msg_hash_us.json', 'r+', encoding='utf-8') as source_json_file:
|
||||
source_messages = json.load(source_json_file)
|
||||
|
|
Loading…
Reference in New Issue
Block a user