Untitled
unknown
python
5 years ago
14 kB
5
Indexable
# -*- coding: UTF-8 -*-
import re
import os
from collections import OrderedDict
from pathlib import Path
from ruamel.yaml import YAML
from bidi.algorithm import get_display
from arabic_reshaper import ArabicReshaper
configuration = {
'delete_harakat': False,
'support_ligatures': True
}
Arabic_Reshaper = ArabicReshaper(configuration=configuration)
yaml = YAML()
#import yaml
def create_dir(dirPath):
if not os.path.exists(dirPath):
os.makedirs(dirPath)
ORIGINAL = 'EN_MSYT'
PO_DIR = r'locales_corrected\ar'
EXPORTED = r'locales_corrected\ar_msyt'
#PO_DIR = r'EN_POT'
#EXPORTED = r'EN_POT_MSYT'
FSI = '\u2068'
PDI = '\u2069'
COLORS_LIST = ['blue', 'red', 'grey', 'light_green4', 'orange', 'light_green1', 'light_grey']
PAUSES_LIST = ['longer', 'short', 'long']
COLORS_RESET = ['reset']
COLORS_DICT = {
'أزرق': 'blue',
'ازرق': 'blue',
'أحمر': 'red',
'احمر': 'red',
'رمادي': 'grey',
'أخضر4': 'light_green4',
'اخضر4': 'light_green4',
'برتقالي': 'orange',
'أخضر1': 'light_green1',
'اخضر1': 'light_green1',
'رمادي_فاتح': 'light_grey',
}
for color in COLORS_LIST:
COLORS_DICT[color] = color
PAUSES_DICT = {
'أطول': 'longer',
'اطول': 'longer',
'قصير': 'short',
'طويل': 'long',
}
for pause in PAUSES_LIST:
PAUSES_DICT[pause] = pause
COLORS_RESET.append('بلالون')
COLORS_RESET.append('أصلي')
COLORS_RESET.append('اصلي')
from babel.messages.catalog import Catalog
from babel.messages.pofile import write_po, read_po
from babel._compat import BytesIO
kinds_list = []
for dirPath, dnames, fnames in os.walk(ORIGINAL):
for f in fnames:
if not f.endswith(".msyt"):
continue
if not f.startswith('DontDamageFlower'):
continue
fileObj = open(os.path.join(dirPath, f), 'r', encoding='utf-8')
po_dir = dirPath.replace(ORIGINAL, PO_DIR)
create_dir(po_dir)
new_msyt_dir = dirPath.replace(ORIGINAL, EXPORTED)
create_dir(new_msyt_dir)
try:
with open(os.path.join(po_dir, os.path.splitext(f)[0] + '.po'), # change to po
'r', encoding='utf-8') as handle:
catalogEN_XX = read_po(handle)
except:
continue
#def get_control_code_object(data, index_p, kind_p):
#index = 1
#for block in data['contents']:
#if len(block.keys()) > 1:
#print(f'{f}: "{key}" content has more than one block!')
#quit()
#if 'control' in block:
#control_code = block['control']
#kind = control_code['kind']
#kind_abv = '_'.join([code[0]+code[1] for code in kind.split('_')])
#if str(index_p) == str(index) and str(kind_p) == str(kind_abv):
#print(block)
#return block
#index += 1
#return None
def get_property(kind, value_name = None, value = None):
if value_name and value:
return {'control': {'kind': kind, value_name: value}}
else:
return {'control': {'kind': kind}}
def get_control_code_object_new(data, control_code_to_parse):
#print(control_code_to_parse)
is_inside_color = None
is_inside_text_size = None
result_block = None
index = 1
for block in data['contents']:
if len(block.keys()) > 1:
print(f'{f}: "{key}" content has more than one block!')
quit()
if 'control' in block:
control_code = block['control']
kind = control_code['kind']
kind_abv = '_'.join([code[0]+code[1] for code in kind.split('_')])
# here we start checking
is_number_only = re.match('(\d*)', control_code_to_parse)
pause_frames = re.match('(\d*)f', control_code_to_parse)
auto_advance_frames = re.match('\+(\d*)', control_code_to_parse)
text_scale = re.match('(\d*)%', control_code_to_parse)
# normal index_p.kind_p
if '.' in control_code_to_parse:
index_p, kind_p = control_code_to_parse.split('.')
if str(index_p) == str(index) and str(kind_p) == str(kind_abv):
result_block = block
# pause names
elif control_code_to_parse in PAUSES_DICT.keys():
result_block = get_property(
'pause',
'length',
PAUSES_DICT[control_code_to_parse]
)
# pause frames
elif pause_frames:
result_block = get_property(
'pause',
'frames',
int(pause_frames.group(1))
)
# color names
elif control_code_to_parse in COLORS_DICT.keys():
result_block = get_property(
'set_colour',
'colour',
COLORS_DICT[control_code_to_parse]
)
is_inside_color = True
# color reset
elif control_code_to_parse in COLORS_RESET:
result_block = get_property(
'reset_colour'
)
is_inside_color = False
# auto advance
elif auto_advance_frames:
result_block = get_property(
'auto_advance',
'frames',
int(auto_advance_frames.group(1))
)
# text sizes
elif text_scale:
scale = int(text_scale.group(1))
result_block = get_property(
'text_size',
'percent',
scale
)
if scale == 100:
is_inside_text_size = False
else:
is_inside_text_size = True
elif control_code_to_parse == 'NO_STRING':
result_block = {'text': ''}
# just the index_p (can collide with others, last resort)
elif is_number_only:
if str(control_code_to_parse) == str(index):
result_block = block
# variables
else:
if kind == 'variable':
if control_code_to_parse == control_code['name']:
result_block = block
index += 1
return result_block, is_inside_color, is_inside_text_size
file_data = yaml.load(fileObj)#, Loader=yaml.FullLoader)
for key, data in file_data['entries'].items():
full_string = ''
help_text = []
for message in catalogEN_XX:
if message.context == str(key):
break
else:
message = None
translated_string = message.string
if not message.string:
translated_string = message.id
#if key != 'Npc_Zora003_F_004':
#continue
if not message:
print(f'{po_dir}: "{key}" has no message!')
quit()
#if message.context != 'Fail_1':
#continue
final_entry = []
lines = translated_string.split('\n')
for index, line in enumerate(lines):
#parts = re.split(f'(<{FSI}[^.]*\.[^>]*{PDI}>)', line)
parts = re.split(f'(<{FSI}?[^>{FSI}{PDI}]*{PDI}?>)', line)
#print(parts)
if len(parts) != 0:
final_line = []
text_part = []
color_part = []
text_size_part = []
is_inside_text = False
current_color_block = None
current_text_size_block = None
current_part = text_part
for part in parts:
if len(part) == 0:
continue
#control_code = re.match(f'<{FSI}([^.]*)\.([^>]*){PDI}>', part)
control_code = re.match(f'<{FSI}?([^>{FSI}{PDI}]*){PDI}?>', part)
if control_code:
#block = get_control_code_object(
#data,
#control_code.group(1),
#control_code.group(2)
#)
block, is_inside_color_ret, is_inside_text_size_ret = get_control_code_object_new(
data,
control_code.group(1)
)
if not block:
print(f'{po_dir}: "{key}" {part} is not found!')
quit()
#print(f'{block}-{is_inside_color_ret}-{is_inside_text_size_ret}')
if is_inside_color_ret:
current_part = color_part
current_color_block = block
if is_inside_text_size_ret:
current_part = text_size_part
current_text_size_block = block
if is_inside_color_ret is None and is_inside_text_size_ret is None:
if not is_inside_text:
final_line.append(block)
else:
current_part.append(block)
# we return here None so have to use == False explicitly
if is_inside_color_ret == False:
# add it to size text part if it's not None
current_part = text_size_part if current_text_size_block != None else text_part
color_part.insert(0, current_color_block)
color_part.append(block)
current_part[0:0] = color_part
#current_part.insert(0, color_part)
current_color_block = None
color_part = []
if is_inside_text_size_ret == False:
# add it to color part if it's not None
current_part = color_part if current_color_block != None else text_part
text_size_part.insert(0, current_text_size_block)
text_size_part.append(block)
current_part[0:0] = text_size_part
#current_part.insert(0, text_size_part)
current_text_size_block = None
text_size_part = []
# OLD
#final_line.append(
#block
#)
else:
text = part.replace('\n', '\\n')
text = get_display(Arabic_Reshaper.reshape(text))
is_inside_text = True
current_part.insert(0, {'text': text})
# OLD
#final_line.append(
#{'text': text}
#)
#print(text_part)
for each in text_part:
final_line.append(each)
#final_line.reverse()
for each in final_line:
final_entry.append(each)
if index == 0:
final_entry[0:0] = [{'control': {'kind': 'raw', 'two_hundred_one': {'one_field': [2, {'field_1': 0}]}}}]
final_entry[0:0] = [{'control': {'kind': 'raw', 'two_hundred_one': {'one_field': [3, {'field_1': 0}]}}}]
if index != len(lines) - 1:
final_entry.append(
{'text': '\u000A'})
final_dict = {}
for block in final_entry:
final_dict = {**final_dict, **block}
file_data['entries'][key]['contents'] = final_entry
yaml.dump(
file_data,
open(os.path.join(new_msyt_dir, os.path.splitext(f)[0] + '.msyt'), 'w', encoding='utf-8')
)
Editor is loading...