From f9eda09deaa2247070ab472f4786f48a2eddb9c0 Mon Sep 17 00:00:00 2001 From: dusk Date: Wed, 21 Feb 2024 17:47:29 +0100 Subject: [PATCH] Use csv file for intermediary parsing --- src/orig.py | 1 + src/parser.py | 76 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 52 insertions(+), 25 deletions(-) diff --git a/src/orig.py b/src/orig.py index b46cf9d..1ddd67a 100755 --- a/src/orig.py +++ b/src/orig.py @@ -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: diff --git a/src/parser.py b/src/parser.py index 8f9afef..fb88804 100755 --- a/src/parser.py +++ b/src/parser.py @@ -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') - tokens = get_functions_from_file( - os.path.join(scripts_path, filename) - ) + with open(csvname, 'w') as csvfile: - structure = [] + csv_writer = csv.writer( + csvfile, + delimiter=delchar, + quoting=csv.QUOTE_ALL, + quotechar=escapechar, + ) - for token in tokens: - function_name = token[0] + tokens = get_functions_from_file( + os.path.join(scripts_path, filename) + ) - if function_name == "OutputLine": - structure.append(OutputLine( - text_jp=token[2][1:-1].replace('\\', ''), - text_en=token[4][1:-1].replace('\\', ''), - type=token[-1], - )) + for token in tokens: + function_name = token[0] - elif ( - function_name == "OutputLineAll" - and "Line_ContinueAfterTyping" == token[-1] - and "\\n" in token[2] - ): - pass - #count = token[2].count("\\n") - # structure.append(["LineBreak", count]) + if function_name == "OutputLine": + #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]) + + +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