Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
andgineer committed Mar 30, 2024
1 parent 4120e54 commit 979a02a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/audiobook_tags/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""CLI for fixing mp3 tags for iOS audiobooks."""
import sys
from argparse import ArgumentParser, Namespace
from typing import Tuple
Expand Down Expand Up @@ -35,7 +36,10 @@ def get_opts() -> Tuple[Namespace, ArgumentParser]:
"-e",
default=DEFAULT_ENCODING,
dest="encoding",
help=f'mp3 tags encoding. "{OPT_ENCODING_NO_ENCODING}" if you do not need mp3 tags encoding fix. By default "{DEFAULT_ENCODING}".',
help=(
f'mp3 tags encoding. "{OPT_ENCODING_NO_ENCODING}" if you do not need mp3 tags encoding fix. '
f'By default "{DEFAULT_ENCODING}".'
),
)
parser.add_argument(
"--tag",
Expand All @@ -53,7 +57,8 @@ def get_opts() -> Tuple[Namespace, ArgumentParser]:
help=(
f"""Sort files and set mp3 tag `track_num`:
--num="{OPT_TRACK_NUM_BY_FILE_NAMES}" - sort by names;
--num="{OPT_TRACK_NUM_BY_TAG_TITLE}<TAG>" - sort by mp3 tag with name <TAG>. For example to sort by title tag use --num="{OPT_TRACK_NUM_BY_TAG_TITLE}title"."""
--num="{OPT_TRACK_NUM_BY_TAG_TITLE}<TAG>" - sort by mp3 tag with name <TAG>.
For example to sort by title tag use --num="{OPT_TRACK_NUM_BY_TAG_TITLE}title"."""
),
)
parser.add_argument(
Expand Down
17 changes: 12 additions & 5 deletions src/audiobook_tags/tags.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Fix audiobook tags in mp3 files."""
import pathlib
from argparse import Namespace
from typing import List, Optional
Expand All @@ -19,10 +20,12 @@ def process_files(opts: Namespace) -> List[AudioFile]:
try:
audio_file = fix_file_tags(file_path, opts, track)
result.append(audio_file)
except Exception as e:
except Exception as e: # pylint: disable=broad-except
print(f"Error processing {file_path}: {e}")
if not result:
print(f"(!) No files were found in folder `{opts.folder}` with suffix {opts.suffix}.")
print(
f"(!) No files were found in folder `{opts.folder}` with suffix {opts.suffix}."
)
elif any(isinstance(file, Exception) for file in result):
print("(!) Errors occurred. No files were changed.")
elif opts.dry:
Expand Down Expand Up @@ -72,7 +75,9 @@ def fix_file_tags(file_path: pathlib.Path, opts: Namespace, track: int) -> Audio
return audio_file


def get_files_list(folder: str, suffix: str, track_num: Optional[str]) -> List[pathlib.Path]:
def get_files_list(
folder: str, suffix: str, track_num: Optional[str]
) -> List[pathlib.Path]:
"""Load audio files filtered by extension.
Sort files according `track_num` option.
Expand All @@ -83,13 +88,15 @@ def get_files_list(folder: str, suffix: str, track_num: Optional[str]) -> List[p
paths = sorted(paths)
elif track_num.startswith(OPT_TRACK_NUM_BY_TAG_TITLE):
tag_name = track_num[len(OPT_TRACK_NUM_BY_TAG_TITLE) :]
paths = sorted(paths, key=lambda path: getattr(eyed3.load(path).tag, tag_name))
paths = sorted(
paths, key=lambda path: getattr(eyed3.load(path).tag, tag_name)
)
else:
raise ValueError(f"Unknown track_num option: {track_num}")
return paths


def fix_encoding(text: str, fix_encoding: str) -> str:
def fix_encoding(text: str, fix_encoding: str) -> str: # pylint: disable=redefined-outer-name
"""Decode from specified in CLI encoding."""
if fix_encoding.lower() != OPT_ENCODING_NO_ENCODING.lower():
try:
Expand Down

0 comments on commit 979a02a

Please sign in to comment.