83 lines
3.1 KiB
Python
83 lines
3.1 KiB
Python
|
import os
|
||
|
import csv
|
||
|
import argparse
|
||
|
|
||
|
# Parse arguments
|
||
|
parser = argparse.ArgumentParser()
|
||
|
parser.add_argument("-jlpt", type=int, choices={5, 4, 3, 2, 1, 0}, nargs="+", help="Choose the JLPT levels you want to output. You can select multiple ones. [Mandatory]", required = True)
|
||
|
parser.add_argument("-ref", type=str, choices={"RTK", "KDLT"}, help="Choose whether you want the English (RTK) or French (KDLT) index. [Mandatory]", required = True)
|
||
|
parser.add_argument("-type", type=str, choices={"number", "kanji", "name"}, nargs="+", help="Choose the output data you want. You can select multiple ones and the order you want. [Mandatory]", required = True)
|
||
|
parser.add_argument("-output", type=str, choices={"file", "display"}, nargs="+", help="Output on the standard output or in a file. You can select both option. File will be saved to the build folder. [Mandatory]", required = True)
|
||
|
parser.add_argument("-separator", type=str, help="The default separator is \", \" for a CSV file. You can change it with this parameter. [Optional]")
|
||
|
args = parser.parse_args()
|
||
|
levels = args.jlpt
|
||
|
reference = args.ref
|
||
|
type = args.type
|
||
|
output = args.output
|
||
|
output.sort()
|
||
|
if args.separator:
|
||
|
separator = args.separator
|
||
|
else:
|
||
|
separator = ", "
|
||
|
|
||
|
# Parameters
|
||
|
csv_Kanji = "Kanji"
|
||
|
csv_RTKNum = "RTK Number"
|
||
|
csv_RTKName = "RTK Name"
|
||
|
csv_KDLTNum = "KDLT Number"
|
||
|
csv_KDLTName = "KDLT Name"
|
||
|
csv_JLPT = "JLPT Level"
|
||
|
csv_source = "source/kanji_list.csv"
|
||
|
level_formatted = "N" + "_N".join([str(i) for i in levels])
|
||
|
output_file_name = "build/" + reference + "_" + level_formatted + ".csv"
|
||
|
|
||
|
# Fill data from the database
|
||
|
def make_result():
|
||
|
result = []
|
||
|
csv_file = open(csv_source, 'r', encoding='utf-8-sig')
|
||
|
reader = csv.DictReader(csv_file)
|
||
|
|
||
|
for row in reader:
|
||
|
for lvl in levels:
|
||
|
if row[csv_JLPT] == str(lvl) and row[reference + " Number"] != "0":
|
||
|
result.append((int(row[reference + " Number"]), row["Kanji"], row[reference + " Name"]))
|
||
|
|
||
|
result.sort()
|
||
|
return result
|
||
|
|
||
|
# Format the result depending on the parameters
|
||
|
def format_result(result):
|
||
|
formatted_result = []
|
||
|
|
||
|
for item in result:
|
||
|
temp_str = ""
|
||
|
for index, value in enumerate(type):
|
||
|
if value == "number":
|
||
|
temp_str += (separator if index >= 1 else "") + str(item[0])
|
||
|
elif value == "kanji":
|
||
|
temp_str += (separator if index >= 1 else "") + str(item[1])
|
||
|
elif value == "name":
|
||
|
temp_str += (separator if index >= 1 else "") + str(item[2])
|
||
|
formatted_result.append(temp_str)
|
||
|
|
||
|
return formatted_result
|
||
|
|
||
|
# Output the result to a file
|
||
|
def output_result_to_file(result):
|
||
|
os.makedirs(os.path.dirname(output_file_name), exist_ok=True)
|
||
|
output_file = open(output_file_name, "w", encoding='utf-8-sig')
|
||
|
for row in result:
|
||
|
output_file.write(row + "\n")
|
||
|
|
||
|
# Displays the result
|
||
|
def display_result(result):
|
||
|
for row in result:
|
||
|
print(row)
|
||
|
|
||
|
# Program
|
||
|
for value in output:
|
||
|
if value == "display":
|
||
|
display_result(format_result(make_result()))
|
||
|
elif value == "file":
|
||
|
output_result_to_file(format_result(make_result()))
|
||
|
print("Saved to file", output_file_name + "!")
|