Use csv file for intermediary parsing

This commit is contained in:
Dusk 2024-02-21 17:47:29 +01:00
parent 4fe24d8e85
commit f9eda09dea
2 changed files with 52 additions and 25 deletions

View File

@ -131,6 +131,7 @@ def get_symbols(line: str) -> (str, list[str]):
def write_translated(outfile, origfile, translation_file_paths):
for transfilepath in translation_file_paths:
print(f'- reading "{transfilepath}"')
parser.parse_to_csv(transfilepath)
structure = parser.parse_to_structure(transfilepath)
for line in origfile:

View File

@ -1,15 +1,16 @@
#!/usr/bin/env python3
import csv
import os
import config
class OutputLine():
def __init__(self, text_jp: str, text_en: str, type: str):
def __init__(self, text_jp: str, text_en: str):
self.text_jp = text_jp
self.text_en = text_en
self.type = type
# self.type = type
def get_functions_from_file(filepath: str):
def get_functions_from_file(filepath: str) -> list[str]:
"""
Gets the function calls from a file and returns
a list of lists with the following structure:
@ -89,33 +90,58 @@ def get_functions_from_file(filepath: str):
return tokens
def parse_to_csv(filename: str):
out_path = config.get('output_path')
scripts_path = config.get('scripts_path')
def parse_to_structure(filename: str):
scripts_path = config.get("scripts_path")
csvname = os.path.join(out_path, filename + ".csv")
escapechar = config.get('csv_escapechar')
delchar = config.get('csv_delchar')
with open(csvname, 'w') as csvfile:
csv_writer = csv.writer(
csvfile,
delimiter=delchar,
quoting=csv.QUOTE_ALL,
quotechar=escapechar,
)
tokens = get_functions_from_file(
os.path.join(scripts_path, filename)
)
structure = []
for token in tokens:
function_name = token[0]
if function_name == "OutputLine":
structure.append(OutputLine(
text_jp=token[2][1:-1].replace('\\', ''),
text_en=token[4][1:-1].replace('\\', ''),
type=token[-1],
))
#NOTE: [1:-1] Removes quotes
text_jp = token[2][1:-1].replace('\\', '').replace('\n', '')
text_en = token[4][1:-1].replace('\\', '').replace('\n', '')
csv_writer.writerow([text_jp, text_en])
elif (
function_name == "OutputLineAll"
and "Line_ContinueAfterTyping" == token[-1]
and "\\n" in token[2]
):
pass
#count = token[2].count("\\n")
# structure.append(["LineBreak", count])
def parse_to_structure(filename: str) -> list[OutputLine]:
out_path = config.get('output_path')
csvname = os.path.join(out_path, filename + ".csv")
escapechar = config.get('csv_escapechar')
delchar = config.get('csv_delchar')
with open(csvname, 'r') as csvfile:
csv_reader = csv.reader(
csvfile,
delimiter=delchar,
quotechar=escapechar,
)
structure = []
for row in csv_reader:
structure.append(
OutputLine(
text_jp=row[0],
text_en=row[1]
)
)
return structure