From a6d01f092f280e06f9c1c441bb48c3e0e9dc0385 Mon Sep 17 00:00:00 2001 From: ChaotiCryptidz Date: Sun, 6 Feb 2022 16:35:39 +0000 Subject: [PATCH] add a more natural sort to transcode presets list --- musicutil/commands/copy_command.py | 25 ++++++++++++++++++------- musicutil/transcode_presets.py | 25 +++++++++++++++++++------ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/musicutil/commands/copy_command.py b/musicutil/commands/copy_command.py index e64e284..69e70fa 100644 --- a/musicutil/commands/copy_command.py +++ b/musicutil/commands/copy_command.py @@ -126,7 +126,11 @@ class CopyCommand(): exists = path_exists(dest) - if (self.args.skip_existing and not exists) or not self.args.skip_existing: + should_skip = False + if self.args.skip_existing and exists: + should_skip = True + + if should_skip: print("Copying", src, "to", dest) copy_file( src, @@ -150,12 +154,19 @@ class CopyCommand(): new_file = deep_copy(file) new_file.extension = trans_config.file_extension - dest_filepath = new_file.join_filename( - ) if self.args.single_directory else new_file.join_path_from_src() + dest_filepath = "" + + if self.args.single_directory: + dest_filepath = new_file.join_filename() + else: + dest_filepath = new_file.join_path_from_src() + dest_filepath = self.args.dest + "/" + dest_filepath - if (self.args.skip_existing and path_exists( - dest_filepath)): + should_skip_transcode = self.args.skip_existing and path_exists( + dest_filepath) + + if should_skip_transcode: print("Skipping transcoding", dest_filepath) self.state.transcoded_files.append(new_file) return @@ -170,6 +181,8 @@ class CopyCommand(): print("Transcoding Files") if not self.args.single_directory: + # set can't contain duplicates + # so it only creates a dir once directories = set() for file in self.state.files: directories.add(file.path_from_src) @@ -195,8 +208,6 @@ class CopyCommand(): trans_config = get_transcode_config( self.args.transcode_preset) - print(trans_config) - for file in self.state.files: self._transcode_with_config( file, trans_config) diff --git a/musicutil/transcode_presets.py b/musicutil/transcode_presets.py index 5599e59..0ff4147 100644 --- a/musicutil/transcode_presets.py +++ b/musicutil/transcode_presets.py @@ -1,5 +1,18 @@ from functools import reduce +import re +def atoi(text) -> int or str: + try: + ret = int(text) + return ret + except: + return text + +def natural_keys(text: str): + return [ atoi(c) for c in re.split(r'(-?\d+)', text) ] + +def sort_natural(list: list[str]): + list.sort(key=natural_keys) def add_to_arr( arr: list[str], @@ -75,7 +88,7 @@ mp3_presets = { } preset_transcode_presets = preset_transcode_presets | mp3_presets -add_to_arr(transcode_presets["opus"], mp3_presets.keys()) +add_to_arr(transcode_presets["mp3"], mp3_presets.keys()) opus_presets = { "opus-low": "opus-32k", @@ -88,11 +101,11 @@ opus_presets = { preset_transcode_presets = preset_transcode_presets | opus_presets add_to_arr(transcode_presets["opus"], opus_presets.keys()) -transcode_presets["mp3"].sort() -transcode_presets["opus"].sort() -transcode_presets["vorbis"].sort() -transcode_presets["speex"].sort() -transcode_presets["g726"].sort() +sort_natural(transcode_presets["mp3"]) +sort_natural(transcode_presets["opus"]) +sort_natural(transcode_presets["vorbis"]) +sort_natural(transcode_presets["speex"]) +sort_natural(transcode_presets["g726"]) def print_transcode_presets():