Skip to content

Commit

Permalink
Add encoding argument, force utf-8 output and other changes
Browse files Browse the repository at this point in the history
  • Loading branch information
donmai-me committed Jul 6, 2021
1 parent f828731 commit bf8af70
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
44 changes: 34 additions & 10 deletions maiconverter/maiconverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -145,15 +145,17 @@ 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:
out.write(sdt.export())


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)

Expand All @@ -164,15 +166,17 @@ 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:
out.write(ma2.export())


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}...")
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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)


Expand All @@ -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)

Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions maiconverter/maima2/maima2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions maiconverter/maisdt/maisdt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions maiconverter/simai/simai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit bf8af70

Please sign in to comment.