From 155ed55e000dc7605858a0dbb5c3eeef4eac92cf Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Tue, 27 Sep 2022 13:18:28 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- FFRipper | 5 ++- ffripper/__init__.py | 12 ++++--- ffripper/disc_parser.py | 65 +++++++++++++++++++++++++++--------- ffripper/image.py | 12 ++++--- ffripper/player.py | 4 +-- ffripper/process_launcher.py | 14 +++----- ffripper/progress.py | 12 +++---- ffripper/ui.py | 14 ++++---- ffripper/ui_elements.py | 4 +-- 9 files changed, 87 insertions(+), 55 deletions(-) diff --git a/FFRipper b/FFRipper index 4f638ed..fe2fe98 100644 --- a/FFRipper +++ b/FFRipper @@ -27,6 +27,9 @@ from gi.repository import Gtk from ffripper.ui import main builder = Gtk.Builder() -builder.add_from_file("{}/data/ffripper.glade".format(os.path.dirname(os.path.realpath(__file__)))) +builder.add_from_file( + f"{os.path.dirname(os.path.realpath(__file__))}/data/ffripper.glade" +) + main(builder, "../data/") diff --git a/ffripper/__init__.py b/ffripper/__init__.py index 42c53fb..623d688 100644 --- a/ffripper/__init__.py +++ b/ffripper/__init__.py @@ -24,6 +24,7 @@ A fast, powerful and simple-to-use graphical CD ripper """ + __major__ = 0 __minor__ = 1 __release__ = 0 @@ -31,11 +32,12 @@ # package information __name__ = "ffripper" -if __release__ == 0: - __version__ = "{0}.{1}".format(__major__, __minor__) -else: - __version__ = "{0}.{1}.{2}".format(__major__, __minor__, __release__) -__version__ += __prerelease__ +__version__ = ( + "{0}.{1}".format(__major__, __minor__) + if __release__ == 0 + else "{0}.{1}.{2}".format(__major__, __minor__, __release__) +) + __prerelease__ + __description__ = "Fast audio-cd ripper" __author__ = "Stefan Garlonta" __author_email__ = "stefan.garlonta@gmail.com" diff --git a/ffripper/disc_parser.py b/ffripper/disc_parser.py index fa58ece..d6b655b 100644 --- a/ffripper/disc_parser.py +++ b/ffripper/disc_parser.py @@ -68,22 +68,57 @@ def parse_for_tracks(self): if self.dict['disc']['release-list'][0]['medium-list'][i]['disc-list'][f]['id'] == self.disc_id: self._mb_id = self.dict['disc']['release-list'][0]['id'] self._release = self.dict['disc']['release-list'][0]['medium-list'][i] - for j in range(len(self.dict['disc']['release-list'][0]['medium-list'][i]['track-list'])): - tracks.append( - TrackInfo(self.dict['disc']['release-list'][0]['medium-list'][i]['track-list'][j] - ['recording']["title"], - self.dict['disc']['release-list'][0]['medium-list'][i]['track-list'][j][ - 'length'], - self.dict['disc']['release-list'][0]['date'], - self.dict['disc']['release-list'][0]['medium-list'][i]['track-list'] - [j]['recording']['artist-credit'][0]['artist']['name'])) + tracks.extend( + TrackInfo( + self.dict['disc']['release-list'][0][ + 'medium-list' + ][i]['track-list'][j]['recording']["title"], + self.dict['disc']['release-list'][0][ + 'medium-list' + ][i]['track-list'][j]['length'], + self.dict['disc']['release-list'][0]['date'], + self.dict['disc']['release-list'][0][ + 'medium-list' + ][i]['track-list'][j]['recording'][ + 'artist-credit' + ][ + 0 + ][ + 'artist' + ][ + 'name' + ], + ) + for j in range( + len( + self.dict['disc']['release-list'][0][ + 'medium-list' + ][i]['track-list'] + ) + ) + ) + except IndexError: - for i in range(len(self.dict['disc']['release-list'][0]['medium-list'][0]['track-list'])): - tracks.append( - TrackInfo(self.dict['disc']['release-list'][0]['medium-list'][0]['track-list'][i]['recording'][ - "title"], - self.dict['disc']['release-list'][0]['medium-list'][0]['track-list'][i]['length'], - self.dict['disc']['release-list'][0]['date'], None)) + tracks.extend( + TrackInfo( + self.dict['disc']['release-list'][0]['medium-list'][0][ + 'track-list' + ][i]['recording']["title"], + self.dict['disc']['release-list'][0]['medium-list'][0][ + 'track-list' + ][i]['length'], + self.dict['disc']['release-list'][0]['date'], + None, + ) + for i in range( + len( + self.dict['disc']['release-list'][0]['medium-list'][0][ + 'track-list' + ] + ) + ) + ) + return tracks def parse_for_cover(self): diff --git a/ffripper/image.py b/ffripper/image.py index 088ab0f..f6aa26c 100644 --- a/ffripper/image.py +++ b/ffripper/image.py @@ -53,7 +53,7 @@ def bytes2pixbuf(data, return _loader.get_pixbuf() @staticmethod - def bytes2png(data, output_dir, filename): # type: (bytes, str, str) -> str + def bytes2png(data, output_dir, filename): # type: (bytes, str, str) -> str """ writes raw bytes to .png file :param data: raw bytes @@ -61,11 +61,13 @@ def bytes2png(data, output_dir, filename): # type: (bytes, str, str) -> str :param filename: name of the file (file extension is added automatically) :return: the full path to image """ - _result_file = output_dir + "/" + filename + _result_file = f"{output_dir}/{filename}" try: _image = PilImage.open(io.BytesIO(data)) - _image.save(_result_file + '.png', 'PNG') - return _result_file + ".png" + _image.save(f'{_result_file}.png', 'PNG') + return f"{_result_file}.png" except PermissionError as e: - raise RipperError(Reason.PERMISIONERROR, "Unable to write to {}".format(output_dir)) from e + raise RipperError( + Reason.PERMISIONERROR, f"Unable to write to {output_dir}" + ) from e diff --git a/ffripper/player.py b/ffripper/player.py index 10039d8..eb6c2f5 100644 --- a/ffripper/player.py +++ b/ffripper/player.py @@ -53,7 +53,7 @@ def set_file(self, filepath: str): :param filepath: str (path to audio file) """ self.filepath = filepath - self._player.set_property("uri", "file://" + self.filepath) + self._player.set_property("uri", f"file://{self.filepath}") def set_volume(self, volume): """ @@ -97,7 +97,7 @@ def __on_message(self, bus, message): elif t == Gst.MessageType.ERROR: self._player.set_state(Gst.State.NULL) err, debug = message.parse_error() - print("Error: %s" % err, debug) + print(f"Error: {err}", debug) def get_duration(self): """ diff --git a/ffripper/process_launcher.py b/ffripper/process_launcher.py index 7c3c433..b22a96d 100644 --- a/ffripper/process_launcher.py +++ b/ffripper/process_launcher.py @@ -60,17 +60,13 @@ def __init__(self, input_location, output_location, file_format, listener, metad def create_album_directory(self): if self.album_directory: self.output_location = os.path.join(self.output_location, self.meta.get_album()) - logger.info("Creating Path {}".format( - self.output_location - )) + logger.info(f"Creating Path {self.output_location}") self.mkdir(self.output_location) def create_artist_directory(self): if self.artist_directory: self.output_location = os.path.join(self.output_location, self.meta.get_artist()) - logger.info("Creating Path {}".format( - self.output_location - )) + logger.info(f"Creating Path {self.output_location}") self.mkdir(self.output_location) @staticmethod @@ -96,14 +92,14 @@ def filter_files_by_extension(listed_dir, extension): ] def run(self): - logger.info("Started at {}".format(time.time())) + logger.info(f"Started at {time.time()}") start_time = [] self.create_dirs() for i in range(len(self.audio_files)): if not self.should_continue: return start_time.append(time.time()) - logger.debug("{}: starting subprocess {}".format(time.time(), i)) + logger.debug(f"{time.time()}: starting subprocess {i}") cover_art_stream = ( "" if self.cover is None else "-i \"{0}\"".format(self.cover) ) @@ -147,6 +143,6 @@ def create_dirs(self): self.create_album_directory() def rm_cover(self): - path = os.path.join(self.base_location + "/cover.png") + path = os.path.join(f"{self.base_location}/cover.png") if os.path.exists(path): # Else it has been deleted from outside os.remove(path) diff --git a/ffripper/progress.py b/ffripper/progress.py index 8657657..b9bff8b 100644 --- a/ffripper/progress.py +++ b/ffripper/progress.py @@ -52,14 +52,10 @@ def parse_output(self): break myline += out if out in ('\r', '\n'): - m = re.search("Duration: ([0-9:.]+)", myline) - if m: - total = duration_in_seconds(m.group(1)) - n = re.search("time=([0-9:]+)", myline) - # time can be of format 'time=hh:mm:ss.ts' or 'time=ss.ts' - # depending on ffmpeg version - if n: - time = n.group(1) + if m := re.search("Duration: ([0-9:.]+)", myline): + total = duration_in_seconds(m[1]) + if n := re.search("time=([0-9:]+)", myline): + time = n[1] if ':' in time: time = duration_in_seconds(time) now_sec = int(float(time)) diff --git a/ffripper/ui.py b/ffripper/ui.py index b14237b..20a54cb 100644 --- a/ffripper/ui.py +++ b/ffripper/ui.py @@ -80,8 +80,8 @@ def on_copy_item(count): @staticmethod def __main_thread_updater(count): percentage = count / 100 - logger.debug("Current fraction: {}%".format(window.progressbar.get_fraction() * 100)) - logger.debug("Fraction: {}".format(percentage)) + logger.debug(f"Current fraction: {window.progressbar.get_fraction() * 100}%") + logger.debug(f"Fraction: {percentage}") window.progressbar.set_fraction(window.progressbar.get_fraction() + percentage) @staticmethod @@ -177,7 +177,7 @@ def __create_year_column(self): def set_player(self): if self.is_disc(): - self.player.set_file(self.disc.mount_point + "/" + self.disc_tracks[0]) + self.player.set_file(f"{self.disc.mount_point}/{self.disc_tracks[0]}") def set_cover_image(self): try: @@ -384,10 +384,10 @@ def refresh_button(self, player): self.player_button.set_image(self.play_image) def on_tree_view_columns_changed(self, widget, row, column): - logger.debug("Changed row: {}".format(row)) + logger.debug(f"Changed row: {row}") self.player.reset() index = int(row.get_indices()[0]) - self.player.set_file(self.disc.mount_point + "/" + self.disc_tracks[index]) + self.player.set_file(f"{self.disc.mount_point}/{self.disc_tracks[index]}") self.player_button.set_image(self.pause_image) self.player.play() @@ -466,9 +466,9 @@ def is_disc(self): def main(builder, data): global window - window = RipperWindow(builder, data + "settings.yaml") + window = RipperWindow(builder, f"{data}settings.yaml") load = Loader() load.load_formats(formats) - load.load_settings(data + "settings.yaml") + load.load_settings(f"{data}settings.yaml") logger.info("Started FFRipper") Gtk.main() diff --git a/ffripper/ui_elements.py b/ffripper/ui_elements.py index fd91a79..62991c2 100644 --- a/ffripper/ui_elements.py +++ b/ffripper/ui_elements.py @@ -62,10 +62,8 @@ def chooser(): Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK ) dialog.set_default_size(800, 400) - folder = None response = dialog.run() - if response == Gtk.ResponseType.OK: - folder = dialog.get_filename() + folder = dialog.get_filename() if response == Gtk.ResponseType.OK else None dialog.destroy() return folder