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')
with open(csvname, 'w') as csvfile:
csv_writer = csv.writer(
csvfile,
delimiter=delchar,
quoting=csv.QUOTE_ALL,
quotechar=escapechar,
)
tokens = get_functions_from_file( tokens = get_functions_from_file(
os.path.join(scripts_path, filename) os.path.join(scripts_path, filename)
) )
structure = []
for token in tokens: for token in tokens:
function_name = token[0] function_name = token[0]
if function_name == "OutputLine": if function_name == "OutputLine":
structure.append(OutputLine( #NOTE: [1:-1] Removes quotes
text_jp=token[2][1:-1].replace('\\', ''), text_jp = token[2][1:-1].replace('\\', '').replace('\n', '')
text_en=token[4][1:-1].replace('\\', ''), text_en = token[4][1:-1].replace('\\', '').replace('\n', '')
type=token[-1], csv_writer.writerow([text_jp, text_en])
))
elif (
function_name == "OutputLineAll" def parse_to_structure(filename: str) -> list[OutputLine]:
and "Line_ContinueAfterTyping" == token[-1] out_path = config.get('output_path')
and "\\n" in token[2] csvname = os.path.join(out_path, filename + ".csv")
): escapechar = config.get('csv_escapechar')
pass delchar = config.get('csv_delchar')
#count = token[2].count("\\n")
# structure.append(["LineBreak", count]) 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