Skip to content

Commit

Permalink
Refactor file globbing
Browse files Browse the repository at this point in the history
  • Loading branch information
infojunkie committed May 26, 2024
1 parent de25102 commit 49d9a14
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
readme = "README.md"
Expand Down
24 changes: 12 additions & 12 deletions src/discogs_tag/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
'albumartist'
]

AUDIO_EXTENSIONS = ['flac', 'mp3']

def tag(
release,
dir='./',
Expand All @@ -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(
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
Empty file added tests/glob/01.mp3
Empty file.
Empty file added tests/glob/02.flac
Empty file.
Empty file added tests/glob/03.txt
Empty file.
Empty file added tests/glob/04.flac
Empty file.
Empty file added tests/glob/05.mp3
Empty file.
Empty file added tests/glob/sub1/01.flac
Empty file.
Empty file added tests/glob/sub2/01.mp3
Empty file.
13 changes: 12 additions & 1 deletion tests/test_discogs_tag.py
Original file line number Diff line number Diff line change
@@ -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({
Expand Down

0 comments on commit 49d9a14

Please sign in to comment.