From 09047651202a886f85ca712aafc747c58189faa6 Mon Sep 17 00:00:00 2001 From: rdbende Date: Mon, 11 Dec 2023 21:19:57 +0100 Subject: [PATCH 01/11] uhh... things, ig? --- cozy/architecture/observable.py | 10 ++-- cozy/model/settings.py | 17 +++---- cozy/view_model/book_detail_view_model.py | 36 +++++-------- cozy/view_model/headerbar_view_model.py | 2 +- cozy/view_model/library_view_model.py | 50 +++++++++---------- .../view_model/playback_control_view_model.py | 2 +- cozy/view_model/playback_speed_view_model.py | 4 +- cozy/view_model/sleep_timer_view_model.py | 16 ++---- 8 files changed, 57 insertions(+), 80 deletions(-) diff --git a/cozy/architecture/observable.py b/cozy/architecture/observable.py index eb27831f..5aa6e833 100644 --- a/cozy/architecture/observable.py +++ b/cozy/architecture/observable.py @@ -38,16 +38,18 @@ def remove_bind(self, prop: str, callback: Callable): log.info("Prop not found in observers. Skipping remove bind...") def _notify(self, prop: str): + if prop not in self._observers: + return + try: - if prop in self._observers: - for callback in self._observers[prop]: - callback() + for callback in self._observers[prop]: + callback() except Exception as e: log.error(e) reporter.exception("observable", e) def _notify_main_thread(self, prop: str): - GLib.MainContext.default().invoke_full(GLib.PRIORITY_DEFAULT_IDLE, self._notify, (prop)) + GLib.MainContext.default().invoke_full(GLib.PRIORITY_DEFAULT_IDLE, self._notify, prop) def _destroy_observers(self): self._observers = {} diff --git a/cozy/model/settings.py b/cozy/model/settings.py index 432415bb..f08f2ec6 100644 --- a/cozy/model/settings.py +++ b/cozy/model/settings.py @@ -48,11 +48,10 @@ def last_played_book(self, new_value): self._db_object.save(only=self._db_object.dirty_fields) @property - def default_location(self): - return next(location - for location - in self.storage_locations - if location.default) + def default_location(self) -> bool: + for location in self.storage_locations: + if location.default: + return True @property def storage_locations(self): @@ -78,14 +77,12 @@ def _load_all_storage_locations(self): try: self._storages.append(Storage(self._db, storage_db_obj.id)) except InvalidPath: - log.error("Invalid path found in database, skipping: {}".format(storage_db_obj.path)) + log.error(f"Invalid path found in database, skipping: {storage_db_obj.path}") self._ensure_default_storage_present() def _ensure_default_storage_present(self): - default_storage_present = any(storage.default - for storage - in self._storages) + default_storage_present = any(storage.default for storage in self._storages) - if not default_storage_present and len(self._storages) > 0: + if not default_storage_present and self._storages: self._storages[0].default = True diff --git a/cozy/view_model/book_detail_view_model.py b/cozy/view_model/book_detail_view_model.py index ede76aec..04d7f8f8 100644 --- a/cozy/view_model/book_detail_view_model.py +++ b/cozy/view_model/book_detail_view_model.py @@ -95,15 +95,14 @@ def remaining_text(self) -> Optional[str]: if not self._book: return None - remaining = self._book.duration / self._book.playback_speed - self._book.progress / self._book.playback_speed - return tools.seconds_to_human_readable(remaining) + remaining = self._book.duration - self._book.progress + return tools.seconds_to_human_readable(remaining / self._book.playback_speed) @property def progress_percent(self) -> Optional[float]: if not self._book: return None - - if self._book.duration < 1: + elif self._book.duration < 1: return 1.0 return self._book.progress / self._book.duration @@ -113,9 +112,7 @@ def disk_count(self) -> int: if not self._book: return 0 - return len({chapter.disk - for chapter - in self._book.chapters}) + return len({chapter.disk for chapter in self._book.chapters}) @property def is_book_available(self) -> bool: @@ -127,10 +124,10 @@ def is_book_available(self) -> bool: @property def is_book_external(self) -> bool: first_chapter_path = self._book.chapters[0].file - return any(storage.path - in first_chapter_path - for storage - in self._settings.external_storage_locations) + for storage in self._settings.external_storage_locations: + if storage.path in first_chapter_path: + return True + return False @property def lock_ui(self) -> bool: @@ -167,9 +164,9 @@ def _on_player_event(self, event, message): if not self.book: return - if event == "play" or event == "pause": + if event in {"play", "pause"}: self._notify("playing") - elif event == "position" or event == "book-finished": + elif event in {"position", "book-finished"}: self._notify("progress_percent") self._notify("remaining_text") @@ -177,9 +174,7 @@ def _on_fs_monitor_event(self, event, _): if not self._book: return - if event == "storage-online": - self._notify("is_book_available") - elif event == "storage-offline": + if event in {"storage-online", "storage-offline"}: self._notify("is_book_available") def _on_book_current_chapter_changed(self): @@ -203,15 +198,10 @@ def _on_playback_speed_changed(self): self._notify("total_text") def _on_offline_cache_event(self, event, message): - try: - if message.id != self._book.id: - return - except Exception as e: + if not self._book or self._book.id != message.id: return - if event == "book-offline-removed": - self._notify("downloaded") - elif event == "book-offline": + if event in {"book-offline-removed", "book-offline"}: self._notify("downloaded") def _on_app_setting_changed(self, event, _): diff --git a/cozy/view_model/headerbar_view_model.py b/cozy/view_model/headerbar_view_model.py index dc20d622..f2ec365c 100644 --- a/cozy/view_model/headerbar_view_model.py +++ b/cozy/view_model/headerbar_view_model.py @@ -38,7 +38,7 @@ def __init__(self): @property def lock_ui(self) -> bool: - return self._view == View.NO_MEDIA or self._view == View.EMPTY_STATE or self._view == View.PREPARING_LIBRARY + return self._view in {View.NO_MEDIA, View.EMPTY_STATE, View.PREPARING_LIBRARY} @property def state(self) -> HeaderBarState: diff --git a/cozy/view_model/library_view_model.py b/cozy/view_model/library_view_model.py index 612724e8..eac3280a 100644 --- a/cozy/view_model/library_view_model.py +++ b/cozy/view_model/library_view_model.py @@ -86,8 +86,11 @@ def selected_filter(self, value): self._notify("selected_filter") @property - def is_any_book_in_progress(self): - return any(book.position > 0 for book in self.books) + def is_any_book_in_progress(self) -> bool: + for book in self.books: + if book.position > 0: + return True + return False @property def authors(self): @@ -135,24 +138,20 @@ def remove_book(self, book: Book): def display_book_filter(self, book_element: BookElement): book = book_element.book - author = book.author - reader = book.reader hide_offline_books = self._application_settings.hide_offline book_is_online = self._fs_monitor.get_book_online(book) - if hide_offline_books and not book_is_online and not book.downloaded: - return False - - if self.library_view_mode == LibraryViewMode.CURRENT: - return True if book.last_played > 0 else False - if self.selected_filter == _("All"): return True + elif hide_offline_books and not book_is_online and not book.downloaded: + return False + elif self.library_view_mode == LibraryViewMode.CURRENT: + return book.last_played > 0 elif self.library_view_mode == LibraryViewMode.AUTHOR: - return True if self.selected_filter in author else False + return self.selected_filter in book.author elif self.library_view_mode == LibraryViewMode.READER: - return True if self.selected_filter in reader else False + return self.selected_filter in book.reader def display_book_sort(self, book_element1, book_element2): if self._library_view_mode == LibraryViewMode.CURRENT: @@ -164,7 +163,10 @@ def open_library(self): self._notify("library_view_mode") def book_files_exist(self, book: Book) -> bool: - return any(os.path.exists(chapter.file) for chapter in book.chapters) + for chapter in book.chapters: + if os.path.exists(chapter.file): + return True + return False def _on_fs_monitor_event(self, event, _): if event == "storage-online": @@ -175,10 +177,6 @@ def _on_fs_monitor_event(self, event, _): self._notify("authors") self._notify("readers") self._notify("books-filter") - elif event == "external-storage-added": - pass - elif event == "external-storage-removed": - pass def _on_application_setting_changed(self, event, _): if event == "hide-offline": @@ -200,17 +198,14 @@ def _on_importer_event(self, event, message): self._notify("books") self._notify("books-filter") self._notify("library_view_mode") - if event == "import-failed": + elif event == "import-failed": ImportFailedDialog(message).show() def _on_player_event(self, event, message): - if event == "play": - book = message - - if book: - self._notify("current_book_in_playback") - self._notify("playing") - self._notify("books-filter") + if event == "play" and message: + self._notify("current_book_in_playback") + self._notify("playing") + self._notify("books-filter") elif event == "pause": self._notify("playing") elif event == "chapter-changed": @@ -219,7 +214,7 @@ def _on_player_event(self, event, message): elif event == "stop": self._notify("playing") self._notify("current_book_in_playback") - elif event == "position" or event == "book-finished": + elif event in {"position", "book-finished"}: self._notify("book-progress") def _on_settings_event(self, event: str, message): @@ -252,11 +247,12 @@ def delete_book_files(self, book: Book): for chapter in book.chapters: try: os.remove(chapter.file) - log.info("Deleted file: {}".format(chapter.file)) except Exception as e: log.error("Failed to delete file: {}".format(chapter.file)) log.debug(e) reporter.warning("library_view_model", "Failed to delete a file.") + else: + log.info("Deleted file: {}".format(chapter.file)) def play_book(self, book: Book): self._player.play_pause_book(book) diff --git a/cozy/view_model/playback_control_view_model.py b/cozy/view_model/playback_control_view_model.py index 3214b9bf..4f24f9b6 100644 --- a/cozy/view_model/playback_control_view_model.py +++ b/cozy/view_model/playback_control_view_model.py @@ -94,7 +94,7 @@ def open_book_detail(self): self.emit_event(OpenView.BOOK, self.book) def _on_player_event(self, event, message): - if event == "play" or event == "pause": + if event in {"play", "pause"}: if self.book: self._notify("playing") elif event == "position": diff --git a/cozy/view_model/playback_speed_view_model.py b/cozy/view_model/playback_speed_view_model.py index be8fb31d..0de22a83 100644 --- a/cozy/view_model/playback_speed_view_model.py +++ b/cozy/view_model/playback_speed_view_model.py @@ -20,8 +20,8 @@ def __init__(self): def playback_speed(self) -> float: if self._book: return self._book.playback_speed - else: - return 1.0 + + return 1.0 @playback_speed.setter def playback_speed(self, new_value: float): diff --git a/cozy/view_model/sleep_timer_view_model.py b/cozy/view_model/sleep_timer_view_model.py index 99d8b388..296ef8c1 100644 --- a/cozy/view_model/sleep_timer_view_model.py +++ b/cozy/view_model/sleep_timer_view_model.py @@ -76,13 +76,7 @@ def destroy(self): self._stop_timer() def _start_timer(self): - if self._sleep_timer: - return - - if self.remaining_seconds < 1: - return - - if not self._player.playing: + if self._sleep_timer or self.remaining_seconds < 1 or not self._player.playing: return log.info("Start Timer") @@ -114,12 +108,10 @@ def _on_timer_tick(self): self._notify_main_thread("remaining_seconds") def _get_fadeout(self) -> int: - fadeout = 0 - if self._app_settings.sleep_timer_fadeout: - fadeout = self._app_settings.sleep_timer_fadeout_duration + return self._app_settings.sleep_timer_fadeout_duration - return fadeout + return 0 def _stop_playback(self): fadeout = self._get_fadeout() @@ -150,7 +142,7 @@ def _on_player_changed(self, event, _): self._notify("stop_after_chapter") elif event == "play": self._start_timer() - elif event == "pause" or event == "stop": + elif event in {"pause", "stop"}: self._stop_timer() elif event == "fadeout-finished" and self._wait_for_fadeout_end: self._wait_for_fadeout_end = False From 65aaabb0c7039ccf07fab3d25df08c0c8b0fbae2 Mon Sep 17 00:00:00 2001 From: rdbende Date: Mon, 11 Dec 2023 21:21:28 +0100 Subject: [PATCH 02/11] Update type annotations in random places --- cozy/model/settings.py | 11 +++++------ cozy/view_model/book_detail_view_model.py | 16 +++++++--------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/cozy/model/settings.py b/cozy/model/settings.py index f08f2ec6..34501162 100644 --- a/cozy/model/settings.py +++ b/cozy/model/settings.py @@ -1,5 +1,4 @@ import logging -from typing import List, Optional import peewee @@ -16,8 +15,8 @@ class Settings: - _storages: List[Storage] = [] - _db = cache = inject.attr(SqliteDatabase) + _storages: list[Storage] = [] + _db = inject.attr(SqliteDatabase) def __init__(self): self._db_object: SettingsModel = SettingsModel.get() @@ -27,7 +26,7 @@ def first_start(self) -> bool: return self._db_object.first_start @property - def last_played_book(self) -> Optional[Book]: + def last_played_book(self) -> Book | None: try: return self._db_object.last_played_book except peewee.DoesNotExist: @@ -54,14 +53,14 @@ def default_location(self) -> bool: return True @property - def storage_locations(self): + def storage_locations(self) -> list[Storage]: if not self._storages: self._load_all_storage_locations() return self._storages @property - def external_storage_locations(self): + def external_storage_locations(self) -> list[Storage]: if not self._storages: self._load_all_storage_locations() diff --git a/cozy/view_model/book_detail_view_model.py b/cozy/view_model/book_detail_view_model.py index 04d7f8f8..e3d2973a 100644 --- a/cozy/view_model/book_detail_view_model.py +++ b/cozy/view_model/book_detail_view_model.py @@ -1,5 +1,3 @@ -from typing import Optional - from cozy import tools from cozy.application_settings import ApplicationSettings from cozy.architecture.event_sender import EventSender @@ -29,7 +27,7 @@ def __init__(self): self._play = False self._current_chapter = None - self._book: Book = None + self._book: Book | None = None self._lock_ui: bool = False self._player.add_listener(self._on_player_event) @@ -45,14 +43,14 @@ def playing(self) -> bool: return self._player.playing @property - def current_chapter(self) -> Optional[Chapter]: + def current_chapter(self) -> Chapter | None: if not self.book: return None return self.book.current_chapter @property - def book(self) -> Optional[Book]: + def book(self) -> Book | None: return self._book @book.setter @@ -77,21 +75,21 @@ def book(self, value: Book): self._notify("book") @property - def last_played_text(self) -> Optional[str]: + def last_played_text(self) -> str | None: if not self._book: return None return tools.past_date_to_human_readable(self._book.last_played) @property - def total_text(self) -> Optional[str]: + def total_text(self) -> str | None: if not self._book: return None return tools.seconds_to_human_readable(self._book.duration / self._book.playback_speed) @property - def remaining_text(self) -> Optional[str]: + def remaining_text(self) -> str | None: if not self._book: return None @@ -99,7 +97,7 @@ def remaining_text(self) -> Optional[str]: return tools.seconds_to_human_readable(remaining / self._book.playback_speed) @property - def progress_percent(self) -> Optional[float]: + def progress_percent(self) -> float | None: if not self._book: return None elif self._book.duration < 1: From 401138004696b77328cc2fb210b1c9e683a88a8a Mon Sep 17 00:00:00 2001 From: rdbende Date: Mon, 11 Dec 2023 21:21:49 +0100 Subject: [PATCH 03/11] Delete this file, as it is not used --- cozy/model/single_file_chapter.py | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 cozy/model/single_file_chapter.py diff --git a/cozy/model/single_file_chapter.py b/cozy/model/single_file_chapter.py deleted file mode 100644 index be387575..00000000 --- a/cozy/model/single_file_chapter.py +++ /dev/null @@ -1,31 +0,0 @@ -from cozy.model.chapter import Chapter - - -class SingleFileChapter(Chapter): - @property - def name(self): - pass - - @property - def number(self): - pass - - @property - def disk(self): - pass - - @property - def position(self): - pass - - @property - def file(self): - pass - - @property - def length(self): - pass - - @property - def modified(self): - pass From da4c2cfc01dc2881e685c5ebba84ce065cdea085 Mon Sep 17 00:00:00 2001 From: rdbende Date: Mon, 11 Dec 2023 21:22:06 +0100 Subject: [PATCH 04/11] Clean up imports --- cozy/ui/library_view.py | 7 +++---- cozy/view_model/library_view_model.py | 8 -------- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/cozy/ui/library_view.py b/cozy/ui/library_view.py index 3e530919..b97f0261 100644 --- a/cozy/ui/library_view.py +++ b/cozy/ui/library_view.py @@ -1,14 +1,13 @@ import functools from typing import Optional -from gi.repository import Gtk, Adw -from gi.repository.Gtk import Builder +from gi.repository import Adw, Gtk from cozy.ext import inject from cozy.ui.widgets.book_element import BookElement from cozy.ui.delete_book_view import DeleteBookView from cozy.ui.widgets.filter_list_box import FilterListBox -from cozy.view_model.library_view_model import LibraryViewModel, LibraryViewMode, LibraryPage +from cozy.view_model.library_view_model import LibraryViewModel, LibraryViewMode READER_PAGE = "reader" AUTHOR_PAGE = "author" @@ -23,7 +22,7 @@ class LibraryView: _view_model: LibraryViewModel = inject.attr(LibraryViewModel) - def __init__(self, builder: Builder): + def __init__(self, builder: Gtk.Builder): self._builder = builder self._connected_book_element: Optional[BookElement] = None diff --git a/cozy/view_model/library_view_model.py b/cozy/view_model/library_view_model.py index eac3280a..71079633 100644 --- a/cozy/view_model/library_view_model.py +++ b/cozy/view_model/library_view_model.py @@ -3,8 +3,6 @@ from enum import Enum, auto from typing import Optional -from gi.repository import Gtk - import cozy.ext.inject as inject from cozy.application_settings import ApplicationSettings from cozy.architecture.event_sender import EventSender @@ -31,12 +29,6 @@ class LibraryViewMode(Enum): READER = auto() -class LibraryPage(Enum): - NONE = auto() - FILTER = auto() - BOOKS = auto() - - class LibraryViewModel(Observable, EventSender): _application_settings: ApplicationSettings = inject.attr(ApplicationSettings) _fs_monitor: FilesystemMonitor = inject.attr("FilesystemMonitor") From 8abcb626177a3176d1ca1568a88a665f5116ac99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20D=C3=A9v=C3=A9nyi?= Date: Fri, 15 Dec 2023 13:15:40 +0100 Subject: [PATCH 05/11] Apply changes suggested by @naglis Co-authored-by: Naglis Jonaitis <827324+naglis@users.noreply.github.com> --- cozy/model/settings.py | 1 + 1 file changed, 1 insertion(+) diff --git a/cozy/model/settings.py b/cozy/model/settings.py index 34501162..08a557fd 100644 --- a/cozy/model/settings.py +++ b/cozy/model/settings.py @@ -51,6 +51,7 @@ def default_location(self) -> bool: for location in self.storage_locations: if location.default: return True + return False @property def storage_locations(self) -> list[Storage]: From c1aec7590924d33cc2f3d6a29d8c792900925fda Mon Sep 17 00:00:00 2001 From: rdbende Date: Fri, 15 Dec 2023 13:35:49 +0100 Subject: [PATCH 06/11] Improve string formatting for logs --- cozy/architecture/observable.py | 2 +- cozy/control/artwork_cache.py | 2 +- cozy/control/db.py | 2 +- cozy/control/db_updater.py | 4 ++-- cozy/control/filesystem_monitor.py | 4 ++-- cozy/control/mpris.py | 7 ++++--- cozy/media/files.py | 4 ++-- cozy/media/gst_player.py | 7 ++++--- cozy/media/importer.py | 2 +- cozy/media/media_detector.py | 2 +- cozy/media/player.py | 2 +- cozy/model/book.py | 2 +- cozy/model/database_importer.py | 2 +- cozy/model/track.py | 2 +- cozy/report/report_to_loki.py | 2 +- cozy/view_model/library_view_model.py | 4 ++-- cozy/view_model/settings_view_model.py | 2 +- 17 files changed, 27 insertions(+), 25 deletions(-) diff --git a/cozy/architecture/observable.py b/cozy/architecture/observable.py index 5aa6e833..9380d4f7 100644 --- a/cozy/architecture/observable.py +++ b/cozy/architecture/observable.py @@ -33,7 +33,7 @@ def remove_bind(self, prop: str, callback: Callable): if callback in self._observers[prop]: self._observers[prop].remove(callback) else: - log.info("Callback not found in prop's {} observers. Skipping remove bind...".format(prop)) + log.info("Callback not found in prop's %s observers. Skipping remove bind...", prop) else: log.info("Prop not found in observers. Skipping remove bind...") diff --git a/cozy/control/artwork_cache.py b/cozy/control/artwork_cache.py index 462e7ddd..e2bbdf23 100644 --- a/cozy/control/artwork_cache.py +++ b/cozy/control/artwork_cache.py @@ -129,7 +129,7 @@ def _load_pixbuf_from_cache(self, book, size): else: pixbuf = None except Exception as e: - log.warning("Failed to load pixbuf from path: {}. Deleting file.".format(path)) + log.warning("Failed to load pixbuf from path: %s. Deleting file.", path) log.debug(e) os.remove(path) return None diff --git a/cozy/control/db.py b/cozy/control/db.py index 935c7168..247dc618 100644 --- a/cozy/control/db.py +++ b/cozy/control/db.py @@ -25,7 +25,7 @@ def init_db(): _connect_db(_db) sqlite_version = ".".join([str(num) for num in _db.server_version]) - log.info("SQLite version: {}".format(sqlite_version)) + log.info("SQLite version: %s", sqlite_version) if Settings.table_exists(): update_db() diff --git a/cozy/control/db_updater.py b/cozy/control/db_updater.py index bd973110..ec66d02c 100644 --- a/cozy/control/db_updater.py +++ b/cozy/control/db_updater.py @@ -136,7 +136,7 @@ def _update_db_9(db): file = next((f for f in files if f.path == path), None) if File.select().where(File.path == path).count() > 0: - log.info("Path already existing in db: {}".format(path)) + log.info("Path already existing in db: %s", path) file = File.select().where(File.path == path).get() elif not file: file = File(path=path, modified=track.modified, id=file_id) @@ -144,7 +144,7 @@ def _update_db_9(db): file_id += 1 if TrackToFile.select().join(Track).where(TrackToFile.track.id == track.id).count() > 0: - log.info("TrackToFile already existing in db: {}".format(path)) + log.info("TrackToFile already existing in db: %s", path) continue track_to_file = TrackToFile(track=track.id, file=file, start_at=0) diff --git a/cozy/control/filesystem_monitor.py b/cozy/control/filesystem_monitor.py index bc826f9c..cfdf7a1f 100644 --- a/cozy/control/filesystem_monitor.py +++ b/cozy/control/filesystem_monitor.py @@ -100,10 +100,10 @@ def is_external(self, directory: str) -> bool: return False if path in directory and mount.can_unmount(): - log.info("Storage location {} is external".format(directory)) + log.info("Storage location %s is external", directory) return True - log.info("Storage location {} is not external".format(directory)) + log.info("Storage location %s is not external", directory) return False def __on_mount_added(self, monitor, mount): diff --git a/cozy/control/mpris.py b/cozy/control/mpris.py index 42e0746d..28cce9d3 100644 --- a/cozy/control/mpris.py +++ b/cozy/control/mpris.py @@ -106,17 +106,17 @@ def on_method_call( result = getattr(self, snake_method)(*args) except AttributeError: invocation.return_dbus_error( - "{}.Error.NotSupported".format(interface_name), "Unsupported property" + f"{interface_name}.Error.NotSupported", "Unsupported property" ) except Exception as e: log.error(e) reporter.exception("mpris", e) reporter.error( "mpris", - "MPRIS method call failed with method name: {}".format(method_name), + f"MPRIS method call failed with method name: {method_name}", ) invocation.return_dbus_error( - "{}.Error.Failed".format(interface_name), "Internal exception occurred" + f"{interface_name}.Error.Failed", "Internal exception occurred" ) else: # out_args is at least (signature1). @@ -409,3 +409,4 @@ def _on_current_changed(self) -> None: def _on_status_changed(self, status: str) -> None: properties = {"PlaybackStatus": GLib.Variant("s", status)} self.properties_changed(self.MEDIA_PLAYER2_PLAYER_INTERFACE, properties, []) + diff --git a/cozy/media/files.py b/cozy/media/files.py index f31efb69..c4d9092a 100644 --- a/cozy/media/files.py +++ b/cozy/media/files.py @@ -52,7 +52,7 @@ def _copy_all(self, sources, destination: str): self._copy_file(path, file_copy_destination) def _copy_file(self, source_path: str, dest_path: str): - log.info("Copy file {} to {}".format(source_path, dest_path)) + log.info("Copy file %s to %s", source_path, dest_path) source = Gio.File.new_for_path(source_path) destination = Gio.File.new_for_path(dest_path) @@ -72,7 +72,7 @@ def _copy_file(self, source_path: str, dest_path: str): else: reporter.exception("files", e) - log.error("Failed to copy file: {}".format(e)) + log.error("Failed to copy file: %s", e) self._file_progess += 1 def _copy_directory(self, path, destination): diff --git a/cozy/media/gst_player.py b/cozy/media/gst_player.py index 27396c40..75083a1a 100644 --- a/cozy/media/gst_player.py +++ b/cozy/media/gst_player.py @@ -94,12 +94,13 @@ def state(self) -> GstPlayerState: return GstPlayerState.STOPPED _, state, __ = self._player.get_state(Gst.CLOCK_TIME_NONE) + print(type(state)) if state == Gst.State.PLAYING: return GstPlayerState.PLAYING elif state == Gst.State.PAUSED: return GstPlayerState.PAUSED else: - log.debug("GST player state was not playing or paused but {}.".format(state)) + log.debug("GST player state was not playing or paused but %s", state) return GstPlayerState.STOPPED @property @@ -268,7 +269,7 @@ def _on_gst_message(self, _, message: Gst.Message): reporter.warning("gst_player", "gst: Resource not found. Stopping player.") return - reporter.error("player", "{}: {}".format(error.code, error)) - log.error("{}: {}".format(error.code, error)) + reporter.error("player", f"{error.code}: {error}") + log.error(f"{error.code}: {error}") log.debug(debug_msg) self.emit_event("error", error) diff --git a/cozy/media/importer.py b/cozy/media/importer.py index d5429372..884c55e6 100644 --- a/cozy/media/importer.py +++ b/cozy/media/importer.py @@ -198,7 +198,7 @@ def _filter_unchanged_files(self, files: List[str]) -> List[str]: yield file except Exception as e: log.debug(e) - log.info("Could not get modified timestamp for file {}".format(file)) + log.info("Could not get modified timestamp for file %s", file) continue continue diff --git a/cozy/media/media_detector.py b/cozy/media/media_detector.py index c5f3fb19..cfb1a6d4 100644 --- a/cozy/media/media_detector.py +++ b/cozy/media/media_detector.py @@ -34,7 +34,7 @@ def get_media_data(self) -> MediaFile: try: discoverer_info: GstPbutils.DiscovererInfo = self.discoverer.discover_uri(self.uri) except Exception as e: - log.info("Skipping file because it couldn't be detected: {}".format(self.uri)) + log.info("Skipping file because it couldn't be detected: %s", self.uri) raise AudioFileCouldNotBeDiscovered(self.uri) is_valid_audio_file = self._is_valid_audio_file(discoverer_info) diff --git a/cozy/media/player.py b/cozy/media/player.py index e018a4b0..472c5563 100644 --- a/cozy/media/player.py +++ b/cozy/media/player.py @@ -380,7 +380,7 @@ def _emit_tick(self): position_for_ui = self.position - self.loaded_chapter.start_position self.emit_event_main_thread("position", position_for_ui) except Exception as e: - log.warning("Could not emit position event: {}".format(e)) + log.warning("Could not emit position event: %s", e) def _fadeout_playback(self): duration = self._app_settings.sleep_timer_fadeout_duration * 20 diff --git a/cozy/model/book.py b/cozy/model/book.py index 9e4d5bc2..a5b62775 100644 --- a/cozy/model/book.py +++ b/cozy/model/book.py @@ -216,7 +216,7 @@ def _fetch_chapters(self): except TrackInconsistentData: log.warning("Skipping inconsistent model") except Exception as e: - log.error("Could not create chapter object: {}".format(e)) + log.error("Could not create chapter object: %s", e) for chapter in self._chapters: chapter.add_listener(self._on_chapter_event) diff --git a/cozy/model/database_importer.py b/cozy/model/database_importer.py index 8629b14d..29aa10d8 100644 --- a/cozy/model/database_importer.py +++ b/cozy/model/database_importer.py @@ -81,7 +81,7 @@ def _prepare_track_db_objects(self, media_files: Set[MediaFile]) -> Set[TrackIns book = next((book for book in book_db_objects if is_same_book(book.name, media_file.book_name)), None) file_query = File.select().where(File.path == media_file.path) if not file_query.exists(): - log.error("No file object with path present: {}".format(media_file.path)) + log.error("No file object with path present: %s", media_file.path) continue file = file_query.get() diff --git a/cozy/model/track.py b/cozy/model/track.py index 971be6b0..983fccbe 100644 --- a/cozy/model/track.py +++ b/cozy/model/track.py @@ -35,7 +35,7 @@ def name(self): if self._db_object.name: return self._db_object.name - return "{} {}".format(_("Chapter"), self.number) + return f"{_('Chapter')} {self.number}" @name.setter def name(self, new_name: str): diff --git a/cozy/report/report_to_loki.py b/cozy/report/report_to_loki.py index 6b779065..6720a100 100644 --- a/cozy/report/report_to_loki.py +++ b/cozy/report/report_to_loki.py @@ -61,7 +61,7 @@ def report(component: str, type: LogLevel, message: str, exception: Exception): labels = __append_label(labels, "distro_version", distro.version()) labels = __append_label(labels, "desktop_environment", os.environ.get('DESKTOP_SESSION')) - line = "[{}] {}".format(LOG_LEVEL_MAP[type], message) + line = f"[{LOG_LEVEL_MAP[type]}] {message}" headers = { 'Content-type': 'application/json' diff --git a/cozy/view_model/library_view_model.py b/cozy/view_model/library_view_model.py index 71079633..05eb56b2 100644 --- a/cozy/view_model/library_view_model.py +++ b/cozy/view_model/library_view_model.py @@ -240,11 +240,11 @@ def delete_book_files(self, book: Book): try: os.remove(chapter.file) except Exception as e: - log.error("Failed to delete file: {}".format(chapter.file)) + log.error("Failed to delete file: %s", chapter.file) log.debug(e) reporter.warning("library_view_model", "Failed to delete a file.") else: - log.info("Deleted file: {}".format(chapter.file)) + log.info("Deleted file: %s", chapter.file) def play_book(self, book: Book): self._player.play_pause_book(book) diff --git a/cozy/view_model/settings_view_model.py b/cozy/view_model/settings_view_model.py index 28251f8a..716aa524 100644 --- a/cozy/view_model/settings_view_model.py +++ b/cozy/view_model/settings_view_model.py @@ -62,7 +62,7 @@ def add_storage_location(self): def remove_storage_location(self, model: Storage): if model.default: - log.error("deleting the default storage location {} is not possible".format(model.path)) + log.error("deleting the default storage location %s is not possible", model.path) reporter.error("settings_view_model", "deleting the default storage location is not possible") return From 1b727485664ff3e3de3b3df7b36b331ea17b3940 Mon Sep 17 00:00:00 2001 From: rdbende Date: Mon, 18 Dec 2023 13:09:08 +0100 Subject: [PATCH 07/11] Remove whoopsie hoopsie --- cozy/media/gst_player.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cozy/media/gst_player.py b/cozy/media/gst_player.py index 75083a1a..fb41fa21 100644 --- a/cozy/media/gst_player.py +++ b/cozy/media/gst_player.py @@ -94,7 +94,6 @@ def state(self) -> GstPlayerState: return GstPlayerState.STOPPED _, state, __ = self._player.get_state(Gst.CLOCK_TIME_NONE) - print(type(state)) if state == Gst.State.PLAYING: return GstPlayerState.PLAYING elif state == Gst.State.PAUSED: From 11c987df482ceab6ecab3710d5c9b06803a0ec56 Mon Sep 17 00:00:00 2001 From: rdbende Date: Thu, 21 Dec 2023 19:48:09 +0100 Subject: [PATCH 08/11] Simplify one part, fix another --- cozy/model/library.py | 4 +--- cozy/view_model/book_detail_view_model.py | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/cozy/model/library.py b/cozy/model/library.py index e25dcb8c..d377dece 100644 --- a/cozy/model/library.py +++ b/cozy/model/library.py @@ -93,10 +93,8 @@ def rebase_path(self, old_path: str, new_path: str): self.emit_event_main_thread("rebase-started") chapter_count = len(self.chapters) - progress = 0 - for chapter in self.chapters: + for progress, chapter in enumerate(self.chapters, 1): if chapter.file.startswith(old_path): - progress += 1 chapter.file = chapter.file.replace(old_path, new_path) self.emit_event_main_thread("rebase-progress", progress / chapter_count) diff --git a/cozy/view_model/book_detail_view_model.py b/cozy/view_model/book_detail_view_model.py index e3d2973a..4eb7e9c9 100644 --- a/cozy/view_model/book_detail_view_model.py +++ b/cozy/view_model/book_detail_view_model.py @@ -196,7 +196,7 @@ def _on_playback_speed_changed(self): self._notify("total_text") def _on_offline_cache_event(self, event, message): - if not self._book or self._book.id != message.id: + if not (message and self._book) or self._book.id != message.id: return if event in {"book-offline-removed", "book-offline"}: From 2f26ce2c2fb0b96715c4cb9663ee7475cdca7406 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20D=C3=A9v=C3=A9nyi?= Date: Sat, 6 Jan 2024 17:28:02 +0100 Subject: [PATCH 09/11] Use `os.path.isfile` to check if a file exists Co-authored-by: Naglis Jonaitis <827324+naglis@users.noreply.github.com> --- cozy/view_model/library_view_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cozy/view_model/library_view_model.py b/cozy/view_model/library_view_model.py index 05eb56b2..180ac467 100644 --- a/cozy/view_model/library_view_model.py +++ b/cozy/view_model/library_view_model.py @@ -156,7 +156,7 @@ def open_library(self): def book_files_exist(self, book: Book) -> bool: for chapter in book.chapters: - if os.path.exists(chapter.file): + if os.path.isfile(chapter.file): return True return False From 18b60427200890a0d02e840923c1d065d8cd8aa4 Mon Sep 17 00:00:00 2001 From: rdbende Date: Sat, 6 Jan 2024 17:34:02 +0100 Subject: [PATCH 10/11] Fix missed printf formatted log strings --- cozy/media/gst_player.py | 2 +- cozy/model/settings.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cozy/media/gst_player.py b/cozy/media/gst_player.py index fb41fa21..6d57d553 100644 --- a/cozy/media/gst_player.py +++ b/cozy/media/gst_player.py @@ -269,6 +269,6 @@ def _on_gst_message(self, _, message: Gst.Message): return reporter.error("player", f"{error.code}: {error}") - log.error(f"{error.code}: {error}") + log.error("%s: %s", error.code, error) log.debug(debug_msg) self.emit_event("error", error) diff --git a/cozy/model/settings.py b/cozy/model/settings.py index 08a557fd..c9202233 100644 --- a/cozy/model/settings.py +++ b/cozy/model/settings.py @@ -77,7 +77,7 @@ def _load_all_storage_locations(self): try: self._storages.append(Storage(self._db, storage_db_obj.id)) except InvalidPath: - log.error(f"Invalid path found in database, skipping: {storage_db_obj.path}") + log.error("Invalid path found in database, skipping: %s", storage_db_obj.path) self._ensure_default_storage_present() From d70907cf9e8f4871388a574fdad95f8588eb56c2 Mon Sep 17 00:00:00 2001 From: rdbende Date: Sat, 6 Jan 2024 17:34:51 +0100 Subject: [PATCH 11/11] Remove newline --- cozy/control/mpris.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cozy/control/mpris.py b/cozy/control/mpris.py index 28cce9d3..da206fa9 100644 --- a/cozy/control/mpris.py +++ b/cozy/control/mpris.py @@ -409,4 +409,3 @@ def _on_current_changed(self) -> None: def _on_status_changed(self, status: str) -> None: properties = {"PlaybackStatus": GLib.Variant("s", status)} self.properties_changed(self.MEDIA_PLAYER2_PLAYER_INTERFACE, properties, []) -