polish & rename sort_by_difficulty.py
This commit is contained in:
parent
b23761b587
commit
bc6d9da517
1 changed files with 26 additions and 6 deletions
|
@ -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 argparse
|
||||||
import sys
|
import sys
|
||||||
from typing import Optional, Sequence
|
from typing import Optional, Sequence
|
||||||
|
|
||||||
import simfile
|
import simfile
|
||||||
from simfile.dir import SimfilePack
|
import simfile.dir
|
||||||
|
|
||||||
|
|
||||||
class PrefixTitleWithMeterArgs:
|
class SortByDifficultyArgs:
|
||||||
"""Stores the command-line arguments for this script."""
|
"""Stores the command-line arguments for this script."""
|
||||||
|
|
||||||
pack: str
|
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
|
This saves the updated simfile to its original location
|
||||||
and writes a backup copy with a ~ appended to the filename.
|
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}",
|
input_filename=f"{simfile_path}",
|
||||||
backup_filename=f"{simfile_path}~",
|
backup_filename=f"{simfile_path}~",
|
||||||
) as sf:
|
) as sf:
|
||||||
|
print(f"Processing {simfile_path}")
|
||||||
|
|
||||||
# It's very unlikely for the title property to be blank or missing.
|
# It's very unlikely for the title property to be blank or missing.
|
||||||
# This is mostly to satisfy type-checkers.
|
# This is mostly to satisfy type-checkers.
|
||||||
current_title = sf.title or ""
|
current_title = sf.title or ""
|
||||||
|
@ -99,11 +119,11 @@ def prefix_title_with_meter(simfile_path: str, args: PrefixTitleWithMeterArgs):
|
||||||
|
|
||||||
def main(argv):
|
def main(argv):
|
||||||
# Parse command-line arguments
|
# 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
|
# Iterate over SimfileDirectory objects from the pack
|
||||||
# so that we can easily get the .sm and/or .ssc paths
|
# 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
|
# Try to update whichever formats exist
|
||||||
for simfile_path in [simfile_dir.sm_path, simfile_dir.ssc_path]:
|
for simfile_path in [simfile_dir.sm_path, simfile_dir.ssc_path]:
|
Loading…
Reference in a new issue