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): def write_translated(outfile, origfile, translation_file_paths):
for transfilepath in translation_file_paths: for transfilepath in translation_file_paths:
print(f'- reading "{transfilepath}"') print(f'- reading "{transfilepath}"')
parser.parse_to_csv(transfilepath)
structure = parser.parse_to_structure(transfilepath) structure = parser.parse_to_structure(transfilepath)
for line in origfile: for line in origfile:

View File

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