polish & rename sort_by_difficulty.py

This commit is contained in:
ashastral 2022-08-20 17:53:58 -07:00
parent b23761b587
commit bc6d9da517

View file

@ -1,12 +1,30 @@
R"""
Change the title of every simfile in a pack
so that they are sorted by difficulty in StepMania.
This script finds the hardest chart of a given stepstype (dance-single by default)
and puts its meter (difficulty number) between brackets at the start of the title.
Usage examples:
# Sort a pack by difficulty
python sort_by_difficulty.py "C:\StepMania\Songs\My Pack"
# Unsort by difficulty (remove the title prefixes)
python sort_by_difficulty.py -r "C:\StepMania\Songs\My Pack"
# Customize stepstype and digits
python sort_by_difficulty.py -s dance-double -d 3 "C:\StepMania\My Pack"
"""
import argparse
import sys
from typing import Optional, Sequence
import simfile
from simfile.dir import SimfilePack
import simfile.dir
class PrefixTitleWithMeterArgs:
class SortByDifficultyArgs:
"""Stores the command-line arguments for this script."""
pack: str
@ -51,9 +69,9 @@ def hardest_chart(
)
def prefix_title_with_meter(simfile_path: str, args: PrefixTitleWithMeterArgs):
def prefix_title_with_meter(simfile_path: str, args: SortByDifficultyArgs):
"""
Add (or remove) a prefix to the simfile's title.
Add (or remove) a numeric prefix to the simfile's title.
This saves the updated simfile to its original location
and writes a backup copy with a ~ appended to the filename.
@ -63,6 +81,8 @@ def prefix_title_with_meter(simfile_path: str, args: PrefixTitleWithMeterArgs):
input_filename=f"{simfile_path}",
backup_filename=f"{simfile_path}~",
) as sf:
print(f"Processing {simfile_path}")
# It's very unlikely for the title property to be blank or missing.
# This is mostly to satisfy type-checkers.
current_title = sf.title or ""
@ -99,11 +119,11 @@ def prefix_title_with_meter(simfile_path: str, args: PrefixTitleWithMeterArgs):
def main(argv):
# Parse command-line arguments
args = argparser().parse_args(argv[1:], namespace=PrefixTitleWithMeterArgs())
args = argparser().parse_args(argv[1:], namespace=SortByDifficultyArgs())
# Iterate over SimfileDirectory objects from the pack
# so that we can easily get the .sm and/or .ssc paths
for simfile_dir in SimfilePack(args.pack).simfile_dirs():
for simfile_dir in simfile.dir.SimfilePack(args.pack).simfile_dirs():
# Try to update whichever formats exist
for simfile_path in [simfile_dir.sm_path, simfile_dir.ssc_path]: