From a6409c9c35a70097205f62b62abb446f5b1f84b8 Mon Sep 17 00:00:00 2001 From: Lynne Date: Mon, 26 Oct 2020 22:35:27 +1000 Subject: [PATCH] mypy integration --- bcao.py | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/bcao.py b/bcao.py index 156cdd5..09cdfe1 100755 --- a/bcao.py +++ b/bcao.py @@ -57,14 +57,17 @@ class SongInfo: self.format = path.splitext(file_name)[1][1:] self.fallback = False - fallbacks = re.match(r"^(.+) - (.+) - (\d{2,}) (.+)\.(?:ogg|flac|alac|aiff|wav|mp3|m4a)$", self.file_name) + fallbacks = re.match(r"^(?P.+) - (?P.+) - (?P\d{2,}) (?P.+)\.(?: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 self.tags: Dict[str, str] = { - "track": fallbacks.group(3), - "artist": fallbacks.group(1), - "title": fallbacks.group(4), - "album": fallbacks.group(2), - "album_artist": fallbacks.group(1) + "track": fallbacks.group("track"), + "artist": fallbacks.group("artist"), + "title": fallbacks.group("title"), + "album": fallbacks.group("album"), + "album_artist": fallbacks.group("artist") } # set list_tags to the default tags in list form # i.e. for every tag, set list_tags[x] = [tags[x]] @@ -121,17 +124,17 @@ class SongInfo: 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 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": self.m_file.clear_pictures() - self.m_file.add_picture(embed_cover) + self.m_file.add_picture(to_embed) elif self.format == "mp3": - self.m_tags.add(embed_cover) + self.m_tags.add(to_embed) elif self.format == "m4a": - self.m_tags['covr'] = [embed_cover] + self.m_tags['covr'] = [to_embed] 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) args.process_cover = 'n' +if cover is None: + die("Unable to find cover image!") + # return # needed for mypy + if args.process_cover != 'n': log("Resizing album art to embed in songs...") 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"{len(artists) + 1}) Custom...") - choice = "1" if args.quiet else input("> ") - if choice.isdecimal(): - choice = int(choice) + user_choice: str = "1" if args.quiet else input("> ") + if user_choice.isdecimal(): + choice: int = int(user_choice) if choice == len(artists) + 1: log("Enter the name to use:") artist = input("> ") @@ -398,8 +405,8 @@ log(f"Moving files to \"{destination}\"...") os.makedirs(destination, exist_ok=True) for source_name, dest_name in songs.items(): - shutil.move(Path(tmp, source_name), Path(destination, dest_name)) -shutil.move(Path(tmp, cover), Path(destination, cover)) + shutil.move(str(Path(tmp, source_name)), str(Path(destination, dest_name))) +shutil.move(str(Path(tmp, cover)), str(Path(destination, cover))) tmp_dir.cleanup() log("Done!")