From bf8af7060484b2bc8449246f6127e67ac7e42ad6 Mon Sep 17 00:00:00 2001 From: donmai-me <71143298+donmai-me@users.noreply.github.com> Date: Tue, 6 Jul 2021 23:59:42 +0800 Subject: [PATCH] Add encoding argument, force utf-8 output and other changes --- maiconverter/maiconverter.py | 44 +++++++++++++++++++++++++++-------- maiconverter/maima2/maima2.py | 4 ++-- maiconverter/maisdt/maisdt.py | 4 ++-- maiconverter/simai/simai.py | 3 +-- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/maiconverter/maiconverter.py b/maiconverter/maiconverter.py index 8012f77..f50ab61 100644 --- a/maiconverter/maiconverter.py +++ b/maiconverter/maiconverter.py @@ -134,7 +134,7 @@ def chart_convert(args, output): def handle_ma2(file, name, output_path, args): - ma2 = MaiMa2.open(file) + ma2 = MaiMa2.open(file, encoding=args.encoding) if len(args.delay) != 0: ma2.offset(args.delay) @@ -145,7 +145,9 @@ def handle_ma2(file, name, output_path, args): simai = ma2_to_simai(ma2) ext = ".txt" - with open(os.path.join(output_path, name + ext), "w+", newline="\r\n") as out: + with open( + os.path.join(output_path, name + ext), "w+", newline="\r\n", encoding="utf-8" + ) as out: if args.command == "ma2tosimai": out.write(simai.export(max_den=args.max_divisor)) else: @@ -153,7 +155,7 @@ def handle_ma2(file, name, output_path, args): def handle_sdt(file, name, output_path, args): - sdt = MaiSDT.open(file) + sdt = MaiSDT.open(file, encoding=args.encoding) if len(args.delay) != 0: sdt.offset(args.delay) @@ -164,7 +166,9 @@ def handle_sdt(file, name, output_path, args): simai = sdt_to_simai(sdt, initial_bpm=args.bpm) ext = ".txt" - with open(os.path.join(output_path, name + ext), "w+", newline="\r\n") as out: + with open( + os.path.join(output_path, name + ext), "w+", newline="\r\n", encoding="utf-8" + ) as out: if args.command == "sdttosimai": out.write(simai.export(max_den=args.max_divisor)) else: @@ -172,7 +176,7 @@ def handle_sdt(file, name, output_path, args): def handle_simai_chart(file, name, output_path, args): - with open(file, "r") as f: + with open(file, "r", encoding=args.encoding) as f: chart_text = f.read() simai = SimaiChart.from_str(chart_text, message=f"Parsing Simai chart at {file}...") @@ -186,12 +190,14 @@ def handle_simai_chart(file, name, output_path, args): ext = ".ma2" converted = simai_to_ma2(simai, res=args.resolution) - with open(os.path.join(output_path, name + ext), "w+", newline="\r\n") as out: + with open( + os.path.join(output_path, name + ext), "w+", newline="\r\n", encoding="utf-8" + ) as out: out.write(converted.export()) def handle_simai_file(file, name, output_path, args): - title, charts = parse_file(file) + title, charts = parse_file(file, encoding=args.encoding) for i, chart in enumerate(charts): diff, simai_chart = chart if len(args.delay) != 0: @@ -207,7 +213,10 @@ def handle_simai_file(file, name, output_path, args): name = title + f"_{diff}" with open( - os.path.join(output_path, name + ext), "w+", newline="\r\n" + os.path.join(output_path, name + ext), + "w+", + newline="\r\n", + encoding="utf-8", ) as out: out.write(converted.export()) except Exception as e: @@ -234,7 +243,12 @@ def handle_file(input_path, output_dir, command, key): return plain_text = finale_chart_decrypt(key, input_path) - with open(os.path.join(output_dir, file_name + file_ext), "x") as f: + with open( + os.path.join(output_dir, file_name + file_ext), + "x", + newline="\r\n", + encoding="utf-8", + ) as f: f.write(plain_text) @@ -258,7 +272,10 @@ def handle_db(input_path, output_dir, command, key): plain_text = finale_db_decrypt(key, input_path) with open( - os.path.join(output_dir, file_name + file_ext), "x", newline="\r\n" + os.path.join(output_dir, file_name + file_ext), + "x", + newline="\r\n", + encoding="utf-8", ) as f: f.write(plain_text) @@ -334,6 +351,13 @@ def main(): type=dir_path, help="Path to output. Defaults to /path/to/input/output", ) + parser.add_argument( + "-e", + "--encoding", + type=str, + default="utf-8", + help="Specify encoding of source file. Defaults to utf-8", + ) args = parser.parse_args() print(f"MaiConverter {maiconverter.__version__} by donmai") diff --git a/maiconverter/maima2/maima2.py b/maiconverter/maima2/maima2.py index 34998d5..dfdddf4 100644 --- a/maiconverter/maima2/maima2.py +++ b/maiconverter/maima2/maima2.py @@ -103,9 +103,9 @@ def __init__( } @classmethod - def open(cls, path: str) -> MaiMa2: + def open(cls, path: str, encoding: str = "utf-8") -> MaiMa2: ma2 = cls() - with open(path, "r") as in_f: + with open(path, "r", encoding=encoding) as in_f: for line in in_f: if line in ["\n", "\r\n"]: continue diff --git a/maiconverter/maisdt/maisdt.py b/maiconverter/maisdt/maisdt.py index a78b386..bacd5e9 100644 --- a/maiconverter/maisdt/maisdt.py +++ b/maiconverter/maisdt/maisdt.py @@ -24,9 +24,9 @@ def __init__(self) -> None: self.slide_count = 1 @classmethod - def open(cls, path: str) -> MaiSDT: + def open(cls, path: str, encoding: str = "utf-8") -> MaiSDT: sdt = cls() - with open(path, "r") as file: + with open(path, "r", encoding=encoding) as file: for line in file: if line in ["\n", "\r\n"]: continue diff --git a/maiconverter/simai/simai.py b/maiconverter/simai/simai.py index c27e469..018d232 100644 --- a/maiconverter/simai/simai.py +++ b/maiconverter/simai/simai.py @@ -565,8 +565,7 @@ def export(self, max_den: int = 1000) -> str: measures += [1.0] measures.sort() - measure_wholes = [int(i) for i in measures] - measures += measure_wholes + measures += [int(i) for i in measures] measures = list(set(measures)) measures.sort()