From bc6d9da517dacb6fc871044cf6afd1549c4ac0ae Mon Sep 17 00:00:00 2001 From: ashastral Date: Sat, 20 Aug 2022 17:53:58 -0700 Subject: [PATCH] polish & rename sort_by_difficulty.py --- ...tle_with_meter.py => sort_by_difficulty.py | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) rename prefix_title_with_meter.py => sort_by_difficulty.py (77%) diff --git a/prefix_title_with_meter.py b/sort_by_difficulty.py similarity index 77% rename from prefix_title_with_meter.py rename to sort_by_difficulty.py index 5b09153..1f10327 100644 --- a/prefix_title_with_meter.py +++ b/sort_by_difficulty.py @@ -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]: