From 49d9a1430b4b7896e08862712fee067259143685 Mon Sep 17 00:00:00 2001 From: infojunkie Date: Sat, 25 May 2024 20:33:01 -0700 Subject: [PATCH] Refactor file globbing --- pyproject.toml | 2 +- src/discogs_tag/cli.py | 24 ++++++++++++------------ tests/glob/01.mp3 | 0 tests/glob/02.flac | 0 tests/glob/03.txt | 0 tests/glob/04.flac | 0 tests/glob/05.mp3 | 0 tests/glob/sub1/01.flac | 0 tests/glob/sub2/01.mp3 | 0 tests/test_discogs_tag.py | 13 ++++++++++++- 10 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 tests/glob/01.mp3 create mode 100644 tests/glob/02.flac create mode 100644 tests/glob/03.txt create mode 100644 tests/glob/04.flac create mode 100644 tests/glob/05.mp3 create mode 100644 tests/glob/sub1/01.flac create mode 100644 tests/glob/sub2/01.mp3 diff --git a/pyproject.toml b/pyproject.toml index 74c7e82..6177e18 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "discogs-tag" -version = "0.3.0" +version = "0.3.1" description = "A rudimentary audio tagger based on Discogs metadata." authors = ["infojunkie "] readme = "README.md" diff --git a/src/discogs_tag/cli.py b/src/discogs_tag/cli.py index cf92474..27233bd 100644 --- a/src/discogs_tag/cli.py +++ b/src/discogs_tag/cli.py @@ -22,6 +22,8 @@ 'albumartist' ] +AUDIO_EXTENSIONS = ['flac', 'mp3'] + def tag( release, dir='./', @@ -36,10 +38,7 @@ def tag( }) with urllib.request.urlopen(request) as response: data = json.load(response) - files = sorted( - glob.glob(os.path.join(glob.escape(dir), '**', '*.flac'), recursive=True) + - glob.glob(os.path.join(glob.escape(dir), '**', '*.mp3'), recursive=True) - ) + files = get_files(dir) apply_metadata(data, files, options) def copy( @@ -51,18 +50,14 @@ def copy( ): """Copy the audio tags from source to destination folders.""" options = parse_options(locals()) - src_files = sorted( - glob.glob(os.path.join(glob.escape(src), '**', '*.flac'), recursive=True) + - glob.glob(os.path.join(glob.escape(src), '**', '*.mp3'), recursive=True) - ) + src_files = get_files(src) + pprint(src_files) + return if not src_files: raise Exception(f'No source files found at {src}. Aborting.') - dst_files = sorted( - glob.glob(os.path.join(glob.escape(dir), '**', '*.flac'), recursive=True) + - glob.glob(os.path.join(glob.escape(dir), '**', '*.mp3'), recursive=True) - ) data = read_metadata(src_files, options) + dst_files = get_files(dir) if options['dry']: pprint(data) else: @@ -137,6 +132,11 @@ def reduce_track(tracks, track): if not options['dry']: print(f'Processed {len(files)} audio files.') +def get_files(dir): + return sorted(reduce(lambda xs, ys: xs + ys, [ + glob.glob(os.path.join(glob.escape(dir), '**', f"*.{ext}"), recursive=True) for ext in AUDIO_EXTENSIONS + ])) + def parse_options(options): for skip in SKIP_KEYS: options['skip_' + skip] = False diff --git a/tests/glob/01.mp3 b/tests/glob/01.mp3 new file mode 100644 index 0000000..e69de29 diff --git a/tests/glob/02.flac b/tests/glob/02.flac new file mode 100644 index 0000000..e69de29 diff --git a/tests/glob/03.txt b/tests/glob/03.txt new file mode 100644 index 0000000..e69de29 diff --git a/tests/glob/04.flac b/tests/glob/04.flac new file mode 100644 index 0000000..e69de29 diff --git a/tests/glob/05.mp3 b/tests/glob/05.mp3 new file mode 100644 index 0000000..e69de29 diff --git a/tests/glob/sub1/01.flac b/tests/glob/sub1/01.flac new file mode 100644 index 0000000..e69de29 diff --git a/tests/glob/sub2/01.mp3 b/tests/glob/sub2/01.mp3 new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_discogs_tag.py b/tests/test_discogs_tag.py index 53f9f28..ef31514 100644 --- a/tests/test_discogs_tag.py +++ b/tests/test_discogs_tag.py @@ -1,7 +1,18 @@ -from discogs_tag.cli import merge_metadata, apply_metadata, parse_options +from discogs_tag.cli import merge_metadata, apply_metadata, parse_options, get_files import pytest import json +def test_get_files(): + files = get_files('tests/glob') + assert files == [ + 'tests/glob/01.mp3', + 'tests/glob/02.flac', + 'tests/glob/04.flac', + 'tests/glob/05.mp3', + 'tests/glob/sub1/01.flac', + 'tests/glob/sub2/01.mp3' + ] + def test_merge_metadata(): audio = {} merge_metadata({