Clean up script I/O to make easier to call from tests
This commit is contained in:
parent
417bfe59eb
commit
2a24790d59
@ -1,5 +1,6 @@
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
|
||||
from tja2fumen.parsers import parseTJA
|
||||
from tja2fumen.writers import writeFumen
|
||||
@ -7,7 +8,8 @@ from tja2fumen.converters import convertTJAToFumen
|
||||
from tja2fumen.constants import COURSE_IDS
|
||||
|
||||
|
||||
def main():
|
||||
def main(argv):
|
||||
# Parse args
|
||||
parser = argparse.ArgumentParser(
|
||||
description="tja2fumen"
|
||||
)
|
||||
@ -15,26 +17,26 @@ def main():
|
||||
"file.tja",
|
||||
help="Path to a Taiko no Tatsujin TJA file.",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
args = parser.parse_args(argv)
|
||||
fnameTJA = getattr(args, "file.tja")
|
||||
|
||||
try:
|
||||
inputFile = open(fnameTJA, "r", encoding="utf-8-sig")
|
||||
parsedSongsTJA = parseTJA(inputFile)
|
||||
except UnicodeDecodeError:
|
||||
inputFile = open(fnameTJA, "r", encoding="shift-jis")
|
||||
parsedSongsTJA = parseTJA(inputFile)
|
||||
# Convert TJA data to fumen data
|
||||
parsedSongsTJA = parseTJA(fnameTJA)
|
||||
parsedSongsFumen = {course: convertTJAToFumen(tjaData)
|
||||
for course, tjaData in parsedSongsTJA.items()}
|
||||
|
||||
for course, song in parsedSongsTJA.items():
|
||||
convertedTJA = convertTJAToFumen(song)
|
||||
outputName = os.path.splitext(fnameTJA)[0]
|
||||
if len(parsedSongsTJA) == 1:
|
||||
outputName += ".bin"
|
||||
else:
|
||||
outputName += f"_{COURSE_IDS[course]}.bin"
|
||||
outputFile = open(outputName, "wb")
|
||||
writeFumen(outputFile, convertedTJA)
|
||||
# Generate output filenames
|
||||
baseName = os.path.splitext(fnameTJA)[0]
|
||||
outputFilenames = [baseName + f"_{COURSE_IDS[course]}.bin" if len(parsedSongsTJA) > 1
|
||||
else baseName + ".bin"
|
||||
for course in parsedSongsFumen.keys()]
|
||||
|
||||
# Write fumen data to files
|
||||
for fumenData, outputName in zip(parsedSongsFumen.values(), outputFilenames):
|
||||
writeFumen(open(outputName, "wb"), fumenData)
|
||||
|
||||
return parsedSongsTJA, parsedSongsFumen, outputFilenames
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main(argv=sys.argv[1:])
|
||||
|
@ -15,7 +15,12 @@ from tja2fumen.constants import (
|
||||
########################################################################################################################
|
||||
|
||||
|
||||
def parseTJA(tja):
|
||||
def parseTJA(fnameTJA):
|
||||
try:
|
||||
tja = open(fnameTJA, "r", encoding="utf-8-sig")
|
||||
except UnicodeDecodeError:
|
||||
tja = open(fnameTJA, "r", encoding="shift-jis")
|
||||
|
||||
# Split into lines
|
||||
lines = tja.read().splitlines()
|
||||
lines = [line for line in lines if line.strip()] # Discard empty lines
|
||||
|
Loading…
x
Reference in New Issue
Block a user