From 29719cba6ea58067edf77392c5443459d3a0f756 Mon Sep 17 00:00:00 2001 From: jgrogan Date: Sun, 29 Sep 2024 18:26:57 +0100 Subject: [PATCH] Apply come formatting --- src/jgutils/converters.py | 28 ++++++++++++++++------------ src/jgutils/filesystem/filesystem.py | 5 ++--- src/jgutils/main_cli.py | 12 +++++------- src/jgutils/music/convert.py | 7 ++----- src/jgutils/tasks/__init__.py | 2 +- src/jgutils/tasks/tasks.py | 6 +++--- 6 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/jgutils/converters.py b/src/jgutils/converters.py index efdeeb8..6faf711 100644 --- a/src/jgutils/converters.py +++ b/src/jgutils/converters.py @@ -6,12 +6,14 @@ from .tasks import Task logger = logging.getLogger(__name__) + class ConversionConfig(NamedTuple): input_dir: Path output_dir: Path output_ext: str input_ext: str + def _get_converted_path(input_path: Path, config: ConversionConfig): """ Return the path you would obtain if you moved the file in the input @@ -24,32 +26,34 @@ def _get_converted_path(input_path: Path, config: ConversionConfig): output_path = config.output_dir / relative_path.parent / output_filename return output_path -def _build_conversion_tasks(input_files: list[Path], - output_files: list[Path], - config: ConversionConfig, - conversion_func: Callable) -> list[Tasks]: + +def _build_conversion_tasks( + input_files: list[Path], + output_files: list[Path], + config: ConversionConfig, + conversion_func: Callable, +) -> list[Tasks]: tasks = [] for input_path, output_path in zip(input_files, output_files): cmd = conversion_func(input_path, config.output_dir, config.output_ext) tasks.append(Task(cmd, output_tmp, output_path)) return tasks -def get_unconverted_files(input_files: list[Path], - config: ConversionConfig) -> list[Path]: + +def get_unconverted_files( + input_files: list[Path], config: ConversionConfig +) -> list[Path]: output_files = [_get_converted_path(f, config) for f in input_files] return [f for f in output_files if not f.exists()] + def convert(config: ConversionConfig, conversion_func: Callable): logger.info("Converting files in %s", config.input_dir) logger.info("Writing output to: %s", config.output_dir) - input_files = get_files_recursive( - config.input_dir.resolve(), config.input_ext) + input_files = get_files_recursive(config.input_dir.resolve(), config.input_ext) output_files = get_uncoverted_files(input_files, config) - tasks = _build_conversion_tasks(input_files, - output_files, - config, - conversion_func) + tasks = _build_conversion_tasks(input_files, output_files, config, conversion_func) run_tasks(tasks) diff --git a/src/jgutils/filesystem/filesystem.py b/src/jgutils/filesystem/filesystem.py index a82bd0f..538c6a4 100644 --- a/src/jgutils/filesystem/filesystem.py +++ b/src/jgutils/filesystem/filesystem.py @@ -1,6 +1,5 @@ from pathlib import Path -def get_files_recursive(search_path: Path, - extension: str) -> list[Path]: - return list(search_path.rglob(f"*.{extension}")) +def get_files_recursive(search_path: Path, extension: str) -> list[Path]: + return list(search_path.rglob(f"*.{extension}")) diff --git a/src/jgutils/main_cli.py b/src/jgutils/main_cli.py index 1afc94a..45d3947 100644 --- a/src/jgutils/main_cli.py +++ b/src/jgutils/main_cli.py @@ -5,16 +5,15 @@ from jgutils import music logger = logging.getLogger(__name__) + def cli_music_convert(args): config = music.CovertionConfig( - args.input_dir.resolve(), - args.output_dir.resolve(), - "mp3", - "flac" + args.input_dir.resolve(), args.output_dir.resolve(), "mp3", "flac" ) music.convert(config) + def main_cli(): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(required=True) @@ -31,9 +30,7 @@ def main_cli(): ) music_convert_parser.add_argument( - "--output_dir", - type=Path, default=Path(), - help="Directory for converted files" + "--output_dir", type=Path, default=Path(), help="Directory for converted files" ) music_convert_parser.set_defaults(func=cli_replace_in_files) @@ -43,5 +40,6 @@ def main_cli(): args = parser.parse_args() args.func(args) + if __name__ == "__main__": main_cli() diff --git a/src/jgutils/music/convert.py b/src/jgutils/music/convert.py index 74ae8a5..906ad93 100644 --- a/src/jgutils/music/convert.py +++ b/src/jgutils/music/convert.py @@ -6,9 +6,8 @@ import uuid logger = logging.getLogger(__name__) -def ffmpeg_convert(input_path: Path, - output_dir: Path, - output_ext: str) -> str: + +def ffmpeg_convert(input_path: Path, output_dir: Path, output_ext: str) -> str: output_tmp = output_dir / (str(uuid.uuid4()) + f".{output_ext}") cmd = f"ffmpeg -i '{input_path}' -ab 320k -map_metadata 0 -id3v2_version 3 '{output_tmp}'" @@ -25,5 +24,3 @@ def move(input_dir: Path, output_dir: Path): output_path = output_dir / relative_path os.makedirs(output_path.parent, exist_ok=True) shutil.move(path, output_path) - - diff --git a/src/jgutils/tasks/__init__.py b/src/jgutils/tasks/__init__.py index 6804af1..8e6fcd9 100644 --- a/src/jgutils/tasks/__init__.py +++ b/src/jgutils/tasks/__init__.py @@ -1 +1 @@ -from .tasks import * # NOQA +from .tasks import * # NOQA diff --git a/src/jgutils/tasks/tasks.py b/src/jgutils/tasks/tasks.py index f2fd8b1..66350cb 100644 --- a/src/jgutils/tasks/tasks.py +++ b/src/jgutils/tasks/tasks.py @@ -22,7 +22,7 @@ def _run_task(args): os.makedirs(task.output_path.parent, exist_ok=True) shutil.move(task.output_tmp, task.output_path) -def run_tasks(tasks, pool_size: 10): - with Pool(10) as p: - p.map(_run_task, tasks) +def run_tasks(tasks, pool_size: 10): + with Pool(10) as p: + p.map(_run_task, tasks)