mypy integration

This commit is contained in:
Lynne Megido 2020-10-26 22:35:27 +10:00
parent 5b95996d8c
commit a6409c9c35
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90

39
bcao.py
View file

@ -57,14 +57,17 @@ class SongInfo:
self.format = path.splitext(file_name)[1][1:] self.format = path.splitext(file_name)[1][1:]
self.fallback = False self.fallback = False
fallbacks = re.match(r"^(.+) - (.+) - (\d{2,}) (.+)\.(?:ogg|flac|alac|aiff|wav|mp3|m4a)$", self.file_name) fallbacks = re.match(r"^(?P<artist>.+) - (?P<album>.+) - (?P<track>\d{2,}) (?P<title>.+)\.(?:ogg|flac|alac|aiff|wav|mp3|m4a)$", self.file_name)
if fallbacks is None:
die("Couldn't determine fallback tags!")
return # needed for mypy
# set default values for the tags, in case the file is missing any (or all!) of them # set default values for the tags, in case the file is missing any (or all!) of them
self.tags: Dict[str, str] = { self.tags: Dict[str, str] = {
"track": fallbacks.group(3), "track": fallbacks.group("track"),
"artist": fallbacks.group(1), "artist": fallbacks.group("artist"),
"title": fallbacks.group(4), "title": fallbacks.group("title"),
"album": fallbacks.group(2), "album": fallbacks.group("album"),
"album_artist": fallbacks.group(1) "album_artist": fallbacks.group("artist")
} }
# set list_tags to the default tags in list form # set list_tags to the default tags in list form
# i.e. for every tag, set list_tags[x] = [tags[x]] # i.e. for every tag, set list_tags[x] = [tags[x]]
@ -121,17 +124,17 @@ class SongInfo:
raise NotImplementedError("Song format not yet implemented.") raise NotImplementedError("Song format not yet implemented.")
def set_cover(self, embed_cover: Union[Picture, APIC, MP4Cover]): def set_cover(self, to_embed: Union[Picture, APIC, MP4Cover]):
# embed cover art # embed cover art
if self.format == "ogg": if self.format == "ogg":
self.m_tags["metadata_block_picture"] = [b64encode(embed_cover.write()).decode("ascii")] self.m_tags["metadata_block_picture"] = [b64encode(to_embed.write()).decode("ascii")]
elif self.format == "flac": elif self.format == "flac":
self.m_file.clear_pictures() self.m_file.clear_pictures()
self.m_file.add_picture(embed_cover) self.m_file.add_picture(to_embed)
elif self.format == "mp3": elif self.format == "mp3":
self.m_tags.add(embed_cover) self.m_tags.add(to_embed)
elif self.format == "m4a": elif self.format == "m4a":
self.m_tags['covr'] = [embed_cover] self.m_tags['covr'] = [to_embed]
self.m_file.save() self.m_file.save()
@ -261,6 +264,10 @@ if song_format not in fully_supported:
log(f"Format {song_format} is not fully supported - cover images will not be modified", 1) log(f"Format {song_format} is not fully supported - cover images will not be modified", 1)
args.process_cover = 'n' args.process_cover = 'n'
if cover is None:
die("Unable to find cover image!")
# return # needed for mypy
if args.process_cover != 'n': if args.process_cover != 'n':
log("Resizing album art to embed in songs...") log("Resizing album art to embed in songs...")
with Image.open(str(Path(tmp, cover))) as image: with Image.open(str(Path(tmp, cover))) as image:
@ -379,9 +386,9 @@ while artist is None:
log(f"{i+1}) {artist_name}") log(f"{i+1}) {artist_name}")
log(f"{len(artists) + 1}) Custom...") log(f"{len(artists) + 1}) Custom...")
choice = "1" if args.quiet else input("> ") user_choice: str = "1" if args.quiet else input("> ")
if choice.isdecimal(): if user_choice.isdecimal():
choice = int(choice) choice: int = int(user_choice)
if choice == len(artists) + 1: if choice == len(artists) + 1:
log("Enter the name to use:") log("Enter the name to use:")
artist = input("> ") artist = input("> ")
@ -398,8 +405,8 @@ log(f"Moving files to \"{destination}\"...")
os.makedirs(destination, exist_ok=True) os.makedirs(destination, exist_ok=True)
for source_name, dest_name in songs.items(): for source_name, dest_name in songs.items():
shutil.move(Path(tmp, source_name), Path(destination, dest_name)) shutil.move(str(Path(tmp, source_name)), str(Path(destination, dest_name)))
shutil.move(Path(tmp, cover), Path(destination, cover)) shutil.move(str(Path(tmp, cover)), str(Path(destination, cover)))
tmp_dir.cleanup() tmp_dir.cleanup()
log("Done!") log("Done!")