From 060b90c51221e2602f11a65cdc441f22943aeca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20D=C3=A9v=C3=A9nyi?= Date: Sun, 22 Sep 2024 23:02:09 +0200 Subject: [PATCH 1/9] Cleanup logs in main.py --- main.py | 111 +++++++++++++++++++++++++++----------------------------- 1 file changed, 54 insertions(+), 57 deletions(-) diff --git a/main.py b/main.py index 7319e4e7..6e8835b2 100755 --- a/main.py +++ b/main.py @@ -29,69 +29,77 @@ import gi -gi.require_version('Gtk', '4.0') -gi.require_version('Gdk', '4.0') -gi.require_version('Adw', '1') -gi.require_version('Gst', '1.0') -gi.require_version('GstController', '1.0') -gi.require_version('GstPbutils', '1.0') +gi.require_version("Gtk", "4.0") +gi.require_version("Gdk", "4.0") +gi.require_version("Adw", "1") +gi.require_version("Gst", "1.0") +gi.require_version("GstController", "1.0") +gi.require_version("GstPbutils", "1.0") from gi.repository import Gio, GLib -pkgdatadir = '@DATA_DIR@' -localedir = '@LOCALE_DIR@' +pkgdatadir = "@DATA_DIR@" +localedir = "@LOCALE_DIR@" # We need to call `locale.*textdomain` to get the strings in UI files translated -locale.bindtextdomain('com.github.geigi.cozy', localedir) -locale.textdomain('com.github.geigi.cozy') +locale.bindtextdomain("com.github.geigi.cozy", localedir) +locale.textdomain("com.github.geigi.cozy") # But also `gettext.*textdomain`, to make `_("foo")` in Python work as well -gettext.bindtextdomain('com.github.geigi.cozy', localedir) -gettext.textdomain('com.github.geigi.cozy') +gettext.bindtextdomain("com.github.geigi.cozy", localedir) +gettext.textdomain("com.github.geigi.cozy") -gettext.install('com.github.geigi.cozy', localedir) +gettext.install("com.github.geigi.cozy", localedir) -# gresource must be registered before importing any Gtk.Template annotated classes -resource = Gio.Resource.load(os.path.join(pkgdatadir, 'com.github.geigi.cozy.gresource')) -resource._register() +log = logging.getLogger("main") +data_dir = Path(GLib.get_user_data_dir()) / "cozy" +if not data_dir.is_dir(): + data_dir.mkdir(parents=True) -old_except_hook = None +# setup log files +log_file = data_dir / "cozy.log" +old_log_file = data_dir / "cozy.old.log" + +if log_file.exists(): + log_file.replace(old_log_file) -log = logging.getLogger("main") -data_dir = os.path.join(GLib.get_user_data_dir(), "cozy") -if not os.path.exists(data_dir): - os.makedirs(data_dir) LOG_FORMAT = "%(asctime)s [%(threadName)-12.12s] [%(name)-10.10s] [%(levelname)-5.5s] %(message)s" LOG_DATE_FORMAT = "%H:%M:%S" -# setup log files -log1 = os.path.join(data_dir, "cozy_1.log") -log0 = os.path.join(data_dir, "cozy.log") -if os.path.exists(log1): - os.remove(log1) -if os.path.exists(os.path.join(data_dir, "cozy.log")): - os.rename(log0, log1) - def __on_command_line(): """ - Handle command line + Handle command line """ parser = argparse.ArgumentParser() parser.add_argument("-d", "--debug", action="store_true", dest="debug") args = parser.parse_args(sys.argv[1:]) - if args.debug: - logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT, datefmt=LOG_DATE_FORMAT, handlers=[ - logging.FileHandler(log0), - logging.StreamHandler(sys.stdout) - ]) - else: - logging.basicConfig(level=logging.INFO, format=LOG_FORMAT, datefmt=LOG_DATE_FORMAT, handlers=[ - logging.FileHandler(log0), - logging.StreamHandler(sys.stdout) - ]) + log_level = logging.DEBUG if args.debug else logging.INFO + + logging.basicConfig( + level=log_level, + format=LOG_FORMAT, + datefmt=LOG_DATE_FORMAT, + handlers=[logging.FileHandler(str(log_file)), logging.StreamHandler(sys.stdout)], + ) + + +def debug(sig, frame): + """Interrupt running process, and provide a python prompt for interactive debugging.""" + d = {"_frame": frame} # Allow access to frame object. + d.update(frame.f_globals) # Unless shadowed by global + d.update(frame.f_locals) + + i = code.InteractiveConsole(d) + message = "Signal received : entering python shell.\nTraceback:\n" + message += "".join(traceback.format_stack(frame)) + i.interact(message) + + +def listen(): + signal.signal(signal.SIGUSR1, debug) # Register handler def main(): @@ -113,26 +121,15 @@ def main(): sys.exit(ret) -def debug(sig, frame): - """Interrupt running process, and provide a python prompt for - interactive debugging.""" - d = {'_frame': frame} # Allow access to frame object. - d.update(frame.f_globals) # Unless shadowed by global - d.update(frame.f_locals) - - i = code.InteractiveConsole(d) - message = "Signal received : entering python shell.\nTraceback:\n" - message += ''.join(traceback.format_stack(frame)) - i.interact(message) - - -def listen(): - signal.signal(signal.SIGUSR1, debug) # Register handler +# gresource must be registered before importing any Gtk.Template annotated classes +resource = Gio.Resource.load(os.path.join(pkgdatadir, "com.github.geigi.cozy.gresource")) +resource._register() -if __name__ == '__main__': +if __name__ == "__main__": import multiprocessing as mp - mp.set_start_method('spawn') + + mp.set_start_method("spawn") # All cozy imports are happening here because multiprocessing needs to be setup first # Some modules import multiprocessing which would lead to an exception From c7ae5098cc643355c2d5d50a1386175d99c7dc05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20D=C3=A9v=C3=A9nyi?= Date: Sun, 22 Sep 2024 23:24:06 +0200 Subject: [PATCH 2/9] Move power manager out of its own file --- cozy/app_controller.py | 4 ---- cozy/media/player.py | 26 +++++++++++++++++++++++++- cozy/power_manager.py | 33 --------------------------------- main.py | 1 + 4 files changed, 26 insertions(+), 38 deletions(-) delete mode 100644 cozy/power_manager.py diff --git a/cozy/app_controller.py b/cozy/app_controller.py index 67dfed58..1e7f8a22 100644 --- a/cozy/app_controller.py +++ b/cozy/app_controller.py @@ -14,7 +14,6 @@ from cozy.model.library import Library from cozy.model.settings import Settings from cozy.open_view import OpenView -from cozy.power_manager import PowerManager from cozy.report import reporter from cozy.ui.app_view import AppView from cozy.ui.headerbar import Headerbar @@ -74,8 +73,6 @@ def __init__(self, gtk_app, main_window_builder, main_window): self.main_window.add_listener(self._on_main_window_event) - self.power_manager = inject.instance(PowerManager) - def configure_inject(self, binder): binder.bind_to_provider(SqliteDatabase, get_db) binder.bind("MainWindow", self.main_window) @@ -98,7 +95,6 @@ def configure_inject(self, binder): binder.bind_to_constructor(PlaybackSpeedViewModel, lambda: PlaybackSpeedViewModel()) binder.bind_to_constructor(SleepTimerViewModel, lambda: SleepTimerViewModel()) binder.bind_to_constructor(GstPlayer, lambda: GstPlayer()) - binder.bind_to_constructor(PowerManager, lambda: PowerManager()) binder.bind_to_constructor(ToastNotifier, lambda: ToastNotifier()) binder.bind_to_constructor(AppViewModel, lambda: AppViewModel()) binder.bind_to_constructor(SettingsViewModel, lambda: SettingsViewModel()) diff --git a/cozy/media/player.py b/cozy/media/player.py index 5bec8fb4..3619aa22 100644 --- a/cozy/media/player.py +++ b/cozy/media/player.py @@ -4,7 +4,7 @@ from typing import Optional import inject -from gi.repository import GLib, Gst, GstController +from gi.repository import GLib, Gst, GstController, Gtk from cozy.application_settings import ApplicationSettings from cozy.architecture.event_sender import EventSender @@ -315,6 +315,29 @@ def _on_gst_message(self, _, message: Gst.Message): self.emit_event("error", error) +class PowerManager: + _gtk_app = inject.attr("GtkApp") + + def __init__(self): + self._inhibit_cookie = None + + def _on_player_changed(self, event: str, data): + if event in ["pause", "stop"]: + if self._inhibit_cookie: + log.info("Uninhibited standby.") + self._gtk_app.uninhibit(self._inhibit_cookie) + self._inhibit_cookie = None + + elif event == "play": + if self._inhibit_cookie: + return + + self._inhibit_cookie = self._gtk_app.inhibit( + None, Gtk.ApplicationInhibitFlags.SUSPEND, "Playback of audiobook" + ) + log.info("Inhibited standby.") + + class Player(EventSender): _library: Library = inject.attr(Library) _app_settings: ApplicationSettings = inject.attr(ApplicationSettings) @@ -329,6 +352,7 @@ def __init__(self): self._book: Optional[Book] = None self._play_next_chapter: bool = True + self.add_listener(PowerManager()._on_player_changed) self._importer.add_listener(self._on_importer_event) self._gst_player.add_listener(self._on_gst_player_event) diff --git a/cozy/power_manager.py b/cozy/power_manager.py deleted file mode 100644 index 09ac40d5..00000000 --- a/cozy/power_manager.py +++ /dev/null @@ -1,33 +0,0 @@ -import logging - -import inject -from gi.repository import Gtk - -from cozy.media.player import Player - -log = logging.getLogger("power_mgr") - - -class PowerManager: - _player: Player = inject.attr(Player) - _gtk_app = inject.attr("GtkApp") - - def __init__(self): - self._inhibit_cookie = None - - self._player.add_listener(self._on_player_changed) - - def _on_player_changed(self, event: str, data): - if event in ["pause", "stop"]: - if self._inhibit_cookie: - log.info("Uninhibited standby.") - self._gtk_app.uninhibit(self._inhibit_cookie) - self._inhibit_cookie = None - - elif event == "play": - if self._inhibit_cookie: - return - - self._inhibit_cookie = self._gtk_app.inhibit(None, Gtk.ApplicationInhibitFlags.SUSPEND, - "Playback of audiobook") - log.info("Inhibited standby.") diff --git a/main.py b/main.py index 6e8835b2..ef4f3005 100755 --- a/main.py +++ b/main.py @@ -26,6 +26,7 @@ import signal import sys import traceback +from pathlib import Path import gi From fd8bc36725410cc70f0cc699c0fea34f8d95b7df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20D=C3=A9v=C3=A9nyi?= Date: Sun, 22 Sep 2024 23:29:02 +0200 Subject: [PATCH 3/9] Move enums to a common file --- cozy/app_controller.py | 3 +-- cozy/open_view.py | 9 --------- cozy/ui/app_view.py | 2 +- cozy/view.py | 11 ----------- cozy/view_model/app_view_model.py | 2 +- cozy/view_model/book_detail_view_model.py | 2 +- cozy/view_model/headerbar_view_model.py | 2 +- cozy/view_model/library_view_model.py | 2 +- cozy/view_model/playback_control_view_model.py | 2 +- cozy/view_model/search_view_model.py | 2 +- 10 files changed, 8 insertions(+), 29 deletions(-) delete mode 100644 cozy/open_view.py diff --git a/cozy/app_controller.py b/cozy/app_controller.py index 1e7f8a22..fe37e678 100644 --- a/cozy/app_controller.py +++ b/cozy/app_controller.py @@ -7,13 +7,13 @@ from cozy.control.db import get_db from cozy.control.filesystem_monitor import FilesystemMonitor from cozy.control.offline_cache import OfflineCache +from cozy.enums import OpenView, View from cozy.media.files import Files from cozy.media.player import GstPlayer, Player from cozy.model.book import Book from cozy.model.database_importer import DatabaseImporter from cozy.model.library import Library from cozy.model.settings import Settings -from cozy.open_view import OpenView from cozy.report import reporter from cozy.ui.app_view import AppView from cozy.ui.headerbar import Headerbar @@ -22,7 +22,6 @@ from cozy.ui.media_controller import MediaController from cozy.ui.search_view import SearchView from cozy.ui.toaster import ToastNotifier -from cozy.view import View from cozy.view_model.app_view_model import AppViewModel from cozy.view_model.book_detail_view_model import BookDetailViewModel from cozy.view_model.headerbar_view_model import HeaderbarViewModel diff --git a/cozy/open_view.py b/cozy/open_view.py deleted file mode 100644 index 5634acdd..00000000 --- a/cozy/open_view.py +++ /dev/null @@ -1,9 +0,0 @@ -from enum import Enum, auto - - -class OpenView(Enum): - AUTHOR = auto() - READER = auto() - BOOK = auto() - LIBRARY = auto() - BACK = auto() diff --git a/cozy/ui/app_view.py b/cozy/ui/app_view.py index 8faac506..c3cb8af5 100644 --- a/cozy/ui/app_view.py +++ b/cozy/ui/app_view.py @@ -1,7 +1,7 @@ import inject from gi.repository import Adw, Gtk -from cozy.view import View +from cozy.enums import View from cozy.view_model.app_view_model import AppViewModel LIBRARY = "main" diff --git a/cozy/view.py b/cozy/view.py index 3b6f05f6..e69de29b 100644 --- a/cozy/view.py +++ b/cozy/view.py @@ -1,11 +0,0 @@ -from enum import Enum, auto - - -class View(Enum): - EMPTY_STATE = auto() - PREPARING_LIBRARY = auto() - LIBRARY = auto() - LIBRARY_FILTER = auto() - LIBRARY_BOOKS = auto() - BOOK_DETAIL = auto() - NO_MEDIA = auto() \ No newline at end of file diff --git a/cozy/view_model/app_view_model.py b/cozy/view_model/app_view_model.py index 6b41ab2a..103d5c60 100644 --- a/cozy/view_model/app_view_model.py +++ b/cozy/view_model/app_view_model.py @@ -1,6 +1,6 @@ from cozy.architecture.event_sender import EventSender from cozy.architecture.observable import Observable -from cozy.view import View +from cozy.enums import View class AppViewModel(Observable, EventSender): diff --git a/cozy/view_model/book_detail_view_model.py b/cozy/view_model/book_detail_view_model.py index 0555126b..0c11ef3c 100644 --- a/cozy/view_model/book_detail_view_model.py +++ b/cozy/view_model/book_detail_view_model.py @@ -6,12 +6,12 @@ from cozy.control import time_format from cozy.control.filesystem_monitor import FilesystemMonitor from cozy.control.offline_cache import OfflineCache +from cozy.enums import OpenView from cozy.media.player import Player from cozy.model.book import Book from cozy.model.chapter import Chapter from cozy.model.library import Library from cozy.model.settings import Settings -from cozy.open_view import OpenView class BookDetailViewModel(Observable, EventSender): diff --git a/cozy/view_model/headerbar_view_model.py b/cozy/view_model/headerbar_view_model.py index 31efbafc..253eb614 100644 --- a/cozy/view_model/headerbar_view_model.py +++ b/cozy/view_model/headerbar_view_model.py @@ -5,10 +5,10 @@ from cozy.architecture.event_sender import EventSender from cozy.architecture.observable import Observable from cozy.control.offline_cache import OfflineCache +from cozy.enums import View from cozy.media.files import Files from cozy.media.importer import Importer, ScanStatus from cozy.model.library import Library -from cozy.view import View class HeaderBarState(Enum): diff --git a/cozy/view_model/library_view_model.py b/cozy/view_model/library_view_model.py index 5cb06904..f6ebe4e5 100644 --- a/cozy/view_model/library_view_model.py +++ b/cozy/view_model/library_view_model.py @@ -9,12 +9,12 @@ from cozy.architecture.event_sender import EventSender from cozy.architecture.observable import Observable from cozy.control.filesystem_monitor import FilesystemMonitor +from cozy.enums import OpenView from cozy.extensions.set import split_strings_to_set from cozy.media.importer import Importer, ScanStatus from cozy.media.player import Player from cozy.model.book import Book from cozy.model.library import Library -from cozy.open_view import OpenView from cozy.report import reporter from cozy.ui.import_failed_dialog import ImportFailedDialog from cozy.ui.widgets.book_card import BookCard diff --git a/cozy/view_model/playback_control_view_model.py b/cozy/view_model/playback_control_view_model.py index fcb76a6e..857705ad 100644 --- a/cozy/view_model/playback_control_view_model.py +++ b/cozy/view_model/playback_control_view_model.py @@ -3,9 +3,9 @@ from cozy.architecture.event_sender import EventSender from cozy.architecture.observable import Observable +from cozy.enums import OpenView from cozy.media.player import Player from cozy.model.book import Book -from cozy.open_view import OpenView class PlaybackControlViewModel(Observable, EventSender): diff --git a/cozy/view_model/search_view_model.py b/cozy/view_model/search_view_model.py index f0294e61..9dd7f52f 100644 --- a/cozy/view_model/search_view_model.py +++ b/cozy/view_model/search_view_model.py @@ -7,10 +7,10 @@ from cozy.architecture.event_sender import EventSender from cozy.architecture.observable import Observable from cozy.control.filesystem_monitor import FilesystemMonitor +from cozy.enums import OpenView from cozy.extensions.set import split_strings_to_set from cozy.model.book import Book from cozy.model.library import Library -from cozy.open_view import OpenView class SearchViewModel(Observable, EventSender): From 161b516923905aee84c0f84c3776a6a12c574a8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20D=C3=A9v=C3=A9nyi?= Date: Sun, 22 Sep 2024 23:54:17 +0200 Subject: [PATCH 4/9] I could never get my head around these filenames --- cozy/app_controller.py | 2 +- cozy/control/artwork_cache.py | 2 +- cozy/control/mpris.py | 2 +- cozy/enums.py | 19 +++++++++++++++++++ cozy/media/player.py | 2 +- cozy/model/book.py | 2 +- cozy/report/report_to_loki.py | 2 +- cozy/{application_settings.py => settings.py} | 0 cozy/ui/main_view.py | 2 +- cozy/ui/widgets/error_reporting.py | 2 +- cozy/view_model/book_detail_view_model.py | 2 +- cozy/view_model/library_view_model.py | 2 +- cozy/view_model/search_view_model.py | 2 +- cozy/view_model/settings_view_model.py | 2 +- cozy/view_model/sleep_timer_view_model.py | 2 +- cozy/view_model/storages_view_model.py | 2 +- test/cozy/media/test_player.py | 2 +- test/cozy/model/test_book.py | 2 +- test/cozy/model/test_library.py | 2 +- 19 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 cozy/enums.py rename cozy/{application_settings.py => settings.py} (100%) diff --git a/cozy/app_controller.py b/cozy/app_controller.py index fe37e678..45802f15 100644 --- a/cozy/app_controller.py +++ b/cozy/app_controller.py @@ -2,7 +2,6 @@ from gi.repository import Gio from peewee import SqliteDatabase -from cozy.application_settings import ApplicationSettings from cozy.architecture.singleton import Singleton from cozy.control.db import get_db from cozy.control.filesystem_monitor import FilesystemMonitor @@ -15,6 +14,7 @@ from cozy.model.library import Library from cozy.model.settings import Settings from cozy.report import reporter +from cozy.settings import ApplicationSettings from cozy.ui.app_view import AppView from cozy.ui.headerbar import Headerbar from cozy.ui.library_view import LibraryView diff --git a/cozy/control/artwork_cache.py b/cozy/control/artwork_cache.py index 61575bdc..0c904bbe 100644 --- a/cozy/control/artwork_cache.py +++ b/cozy/control/artwork_cache.py @@ -7,11 +7,11 @@ import inject from gi.repository import Gdk, GdkPixbuf -from cozy.application_settings import ApplicationSettings from cozy.control.application_directories import get_cache_dir from cozy.db.artwork_cache import ArtworkCache as ArtworkCacheModel from cozy.media.importer import Importer, ScanStatus from cozy.report import reporter +from cozy.settings import ApplicationSettings log = logging.getLogger("artwork_cache") diff --git a/cozy/control/mpris.py b/cozy/control/mpris.py index f51836cd..1f64b21a 100644 --- a/cozy/control/mpris.py +++ b/cozy/control/mpris.py @@ -15,11 +15,11 @@ import inject from gi.repository import Gio, GLib -from cozy.application_settings import ApplicationSettings from cozy.control.artwork_cache import ArtworkCache from cozy.media.player import Player from cozy.model.book import Book from cozy.report import reporter +from cozy.settings import ApplicationSettings log = logging.getLogger("mpris") diff --git a/cozy/enums.py b/cozy/enums.py new file mode 100644 index 00000000..92db9293 --- /dev/null +++ b/cozy/enums.py @@ -0,0 +1,19 @@ +from enum import Enum, auto + + +class OpenView(Enum): + AUTHOR = auto() + READER = auto() + BOOK = auto() + LIBRARY = auto() + BACK = auto() + + +class View(Enum): + EMPTY_STATE = auto() + PREPARING_LIBRARY = auto() + LIBRARY = auto() + LIBRARY_FILTER = auto() + LIBRARY_BOOKS = auto() + BOOK_DETAIL = auto() + NO_MEDIA = auto() diff --git a/cozy/media/player.py b/cozy/media/player.py index 3619aa22..0e26647b 100644 --- a/cozy/media/player.py +++ b/cozy/media/player.py @@ -6,7 +6,6 @@ import inject from gi.repository import GLib, Gst, GstController, Gtk -from cozy.application_settings import ApplicationSettings from cozy.architecture.event_sender import EventSender from cozy.control.offline_cache import OfflineCache from cozy.media.importer import Importer, ScanStatus @@ -14,6 +13,7 @@ from cozy.model.chapter import Chapter from cozy.model.library import Library from cozy.report import reporter +from cozy.settings import ApplicationSettings from cozy.tools import IntervalTimer from cozy.ui.file_not_found_dialog import FileNotFoundDialog from cozy.ui.toaster import ToastNotifier diff --git a/cozy/model/book.py b/cozy/model/book.py index 44b6c572..863432b7 100644 --- a/cozy/model/book.py +++ b/cozy/model/book.py @@ -4,7 +4,6 @@ import inject from peewee import DoesNotExist, SqliteDatabase -from cozy.application_settings import ApplicationSettings from cozy.architecture.event_sender import EventSender from cozy.architecture.observable import Observable from cozy.db.book import Book as BookModel @@ -14,6 +13,7 @@ from cozy.model.chapter import Chapter from cozy.model.settings import Settings from cozy.model.track import Track, TrackInconsistentData +from cozy.settings import ApplicationSettings log = logging.getLogger("BookModel") diff --git a/cozy/report/report_to_loki.py b/cozy/report/report_to_loki.py index 43fdeabd..f30fa74c 100644 --- a/cozy/report/report_to_loki.py +++ b/cozy/report/report_to_loki.py @@ -12,8 +12,8 @@ from peewee import __version__ as PeeweeVersion from cozy import __version__ as CozyVersion -from cozy.application_settings import ApplicationSettings from cozy.report.log_level import LogLevel +from cozy.settings import ApplicationSettings URL = 'https://errors.cozy.sh:3100/api/prom/push' ENABLE = '@INSTALLED@' diff --git a/cozy/application_settings.py b/cozy/settings.py similarity index 100% rename from cozy/application_settings.py rename to cozy/settings.py diff --git a/cozy/ui/main_view.py b/cozy/ui/main_view.py index c926b688..8a0b0811 100644 --- a/cozy/ui/main_view.py +++ b/cozy/ui/main_view.py @@ -7,7 +7,6 @@ import cozy.control.filesystem_monitor as fs_monitor import cozy.report.reporter as report -from cozy.application_settings import ApplicationSettings from cozy.architecture.event_sender import EventSender from cozy.architecture.singleton import Singleton from cozy.control.db import books, close_db @@ -15,6 +14,7 @@ from cozy.media.importer import Importer, ScanStatus from cozy.media.player import Player from cozy.model.settings import Settings as SettingsModel +from cozy.settings import ApplicationSettings from cozy.ui.about_window import AboutWindow from cozy.ui.book_detail_view import BookDetailView from cozy.ui.library_view import LibraryView diff --git a/cozy/ui/widgets/error_reporting.py b/cozy/ui/widgets/error_reporting.py index 8d48ba63..44177562 100644 --- a/cozy/ui/widgets/error_reporting.py +++ b/cozy/ui/widgets/error_reporting.py @@ -3,7 +3,7 @@ import inject from gi.repository import Gtk -from cozy.application_settings import ApplicationSettings +from cozy.settings import ApplicationSettings LEVELS = [ _("Disabled"), diff --git a/cozy/view_model/book_detail_view_model.py b/cozy/view_model/book_detail_view_model.py index 0c11ef3c..bf02a229 100644 --- a/cozy/view_model/book_detail_view_model.py +++ b/cozy/view_model/book_detail_view_model.py @@ -1,6 +1,5 @@ import inject -from cozy.application_settings import ApplicationSettings from cozy.architecture.event_sender import EventSender from cozy.architecture.observable import Observable from cozy.control import time_format @@ -12,6 +11,7 @@ from cozy.model.chapter import Chapter from cozy.model.library import Library from cozy.model.settings import Settings +from cozy.settings import ApplicationSettings class BookDetailViewModel(Observable, EventSender): diff --git a/cozy/view_model/library_view_model.py b/cozy/view_model/library_view_model.py index f6ebe4e5..9ffc9a2d 100644 --- a/cozy/view_model/library_view_model.py +++ b/cozy/view_model/library_view_model.py @@ -5,7 +5,6 @@ import inject -from cozy.application_settings import ApplicationSettings from cozy.architecture.event_sender import EventSender from cozy.architecture.observable import Observable from cozy.control.filesystem_monitor import FilesystemMonitor @@ -16,6 +15,7 @@ from cozy.model.book import Book from cozy.model.library import Library from cozy.report import reporter +from cozy.settings import ApplicationSettings from cozy.ui.import_failed_dialog import ImportFailedDialog from cozy.ui.widgets.book_card import BookCard from cozy.view_model.storages_view_model import StoragesViewModel diff --git a/cozy/view_model/search_view_model.py b/cozy/view_model/search_view_model.py index 9dd7f52f..1c7b04f0 100644 --- a/cozy/view_model/search_view_model.py +++ b/cozy/view_model/search_view_model.py @@ -3,7 +3,6 @@ import inject from gi.repository import GLib -from cozy.application_settings import ApplicationSettings from cozy.architecture.event_sender import EventSender from cozy.architecture.observable import Observable from cozy.control.filesystem_monitor import FilesystemMonitor @@ -11,6 +10,7 @@ from cozy.extensions.set import split_strings_to_set from cozy.model.book import Book from cozy.model.library import Library +from cozy.settings import ApplicationSettings class SearchViewModel(Observable, EventSender): diff --git a/cozy/view_model/settings_view_model.py b/cozy/view_model/settings_view_model.py index f7cb94f8..4a8e9a93 100644 --- a/cozy/view_model/settings_view_model.py +++ b/cozy/view_model/settings_view_model.py @@ -2,11 +2,11 @@ import inject -from cozy.application_settings import ApplicationSettings from cozy.architecture.event_sender import EventSender from cozy.architecture.observable import Observable from cozy.media.importer import Importer from cozy.model.settings import Settings +from cozy.settings import ApplicationSettings log = logging.getLogger("settings_view_model") diff --git a/cozy/view_model/sleep_timer_view_model.py b/cozy/view_model/sleep_timer_view_model.py index 787e71f9..0812903d 100644 --- a/cozy/view_model/sleep_timer_view_model.py +++ b/cozy/view_model/sleep_timer_view_model.py @@ -6,9 +6,9 @@ import inject -from cozy.application_settings import ApplicationSettings from cozy.architecture.observable import Observable from cozy.media.player import Player +from cozy.settings import ApplicationSettings from cozy.tools import IntervalTimer log = logging.getLogger("sleep_timer_view_model") diff --git a/cozy/view_model/storages_view_model.py b/cozy/view_model/storages_view_model.py index 9d297e66..dbc083f3 100644 --- a/cozy/view_model/storages_view_model.py +++ b/cozy/view_model/storages_view_model.py @@ -4,7 +4,6 @@ import inject from peewee import SqliteDatabase -from cozy.application_settings import ApplicationSettings from cozy.architecture.event_sender import EventSender from cozy.architecture.observable import Observable from cozy.control.filesystem_monitor import FilesystemMonitor @@ -12,6 +11,7 @@ from cozy.model.library import Library from cozy.model.settings import Settings from cozy.model.storage import Storage +from cozy.settings import ApplicationSettings log = logging.getLogger("storages_view_model") diff --git a/test/cozy/media/test_player.py b/test/cozy/media/test_player.py index 49bb2de6..f9d1c8d5 100644 --- a/test/cozy/media/test_player.py +++ b/test/cozy/media/test_player.py @@ -4,7 +4,7 @@ import pytest from peewee import SqliteDatabase -from cozy.application_settings import ApplicationSettings +from cozy.settings import ApplicationSettings from cozy.media.player import GstPlayer from cozy.model.library import Library from cozy.model.settings import Settings diff --git a/test/cozy/model/test_book.py b/test/cozy/model/test_book.py index 30a01394..0958c785 100644 --- a/test/cozy/model/test_book.py +++ b/test/cozy/model/test_book.py @@ -4,7 +4,7 @@ import pytest from peewee import SqliteDatabase -from cozy.application_settings import ApplicationSettings +from cozy.settings import ApplicationSettings @pytest.fixture(autouse=True) diff --git a/test/cozy/model/test_library.py b/test/cozy/model/test_library.py index ac22b247..81a21f5b 100644 --- a/test/cozy/model/test_library.py +++ b/test/cozy/model/test_library.py @@ -4,7 +4,7 @@ import pytest from peewee import SqliteDatabase -from cozy.application_settings import ApplicationSettings +from cozy.settings import ApplicationSettings from cozy.extensions.set import split_strings_to_set from cozy.model.settings import Settings From 63fa4b85d122dfdb67aafd10d89de715bd881389 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20D=C3=A9v=C3=A9nyi?= Date: Mon, 23 Sep 2024 00:05:57 +0200 Subject: [PATCH 5/9] Missed a file again, thanks, Ruff --- cozy/view_model/search_view_model.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/cozy/view_model/search_view_model.py b/cozy/view_model/search_view_model.py index f99fb7f9..f46d5344 100644 --- a/cozy/view_model/search_view_model.py +++ b/cozy/view_model/search_view_model.py @@ -6,20 +6,12 @@ from cozy.architecture.event_sender import EventSender from cozy.architecture.observable import Observable from cozy.control.filesystem_monitor import FilesystemMonitor - -<<<<<<< HEAD from cozy.enums import OpenView from cozy.extensions.set import split_strings_to_set from cozy.model.book import Book -from cozy.model.library import Library -from cozy.settings import ApplicationSettings - -======= -from cozy.model.book import Book from cozy.model.library import Library, split_strings_to_set from cozy.open_view import OpenView - ->>>>>>> master +from cozy.settings import ApplicationSettings class SearchViewModel(Observable, EventSender): From eed884a7acb31c4088b93702dd31079eb82d1ef1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20D=C3=A9v=C3=A9nyi?= Date: Mon, 23 Sep 2024 00:11:38 +0200 Subject: [PATCH 6/9] Seems like I chose a bad way to clean up the imports --- cozy/view_model/library_view_model.py | 2 -- cozy/view_model/search_view_model.py | 2 -- 2 files changed, 4 deletions(-) diff --git a/cozy/view_model/library_view_model.py b/cozy/view_model/library_view_model.py index 4018af84..2c3581d8 100644 --- a/cozy/view_model/library_view_model.py +++ b/cozy/view_model/library_view_model.py @@ -9,12 +9,10 @@ from cozy.architecture.observable import Observable from cozy.control.filesystem_monitor import FilesystemMonitor from cozy.enums import OpenView -from cozy.extensions.set import split_strings_to_set from cozy.media.importer import Importer, ScanStatus from cozy.media.player import Player from cozy.model.book import Book from cozy.model.library import Library, split_strings_to_set -from cozy.open_view import OpenView from cozy.report import reporter from cozy.settings import ApplicationSettings from cozy.ui.import_failed_dialog import ImportFailedDialog diff --git a/cozy/view_model/search_view_model.py b/cozy/view_model/search_view_model.py index f46d5344..579d2082 100644 --- a/cozy/view_model/search_view_model.py +++ b/cozy/view_model/search_view_model.py @@ -7,10 +7,8 @@ from cozy.architecture.observable import Observable from cozy.control.filesystem_monitor import FilesystemMonitor from cozy.enums import OpenView -from cozy.extensions.set import split_strings_to_set from cozy.model.book import Book from cozy.model.library import Library, split_strings_to_set -from cozy.open_view import OpenView from cozy.settings import ApplicationSettings From fe3ea1e02b2c036622b2488e20c91c8c184c9c94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20D=C3=A9v=C3=A9nyi?= Date: Mon, 23 Sep 2024 21:55:25 +0200 Subject: [PATCH 7/9] Start using pathlib in more places --- cozy/control/application_directories.py | 28 ++++++++++++++++--------- cozy/control/artwork_cache.py | 12 ++++------- cozy/control/db_updater.py | 5 ++++- cozy/control/offline_cache.py | 17 ++++++++------- cozy/db/model_base.py | 8 +++---- cozy/media/player.py | 4 ++-- main.py | 6 +++--- 7 files changed, 43 insertions(+), 37 deletions(-) diff --git a/cozy/control/application_directories.py b/cozy/control/application_directories.py index 7569ca38..5327bbff 100644 --- a/cozy/control/application_directories.py +++ b/cozy/control/application_directories.py @@ -1,20 +1,28 @@ -import os +from pathlib import Path from gi.repository import GLib -def get_artwork_cache_dir(): - return os.path.join(get_cache_dir(), "artwork") +def get_path_relative_to_chache_folder(*args) -> Path: + dir = Path(GLib.get_user_cache_dir(), "cozy", *args) + dir.mkdir(parents=True, exist_ok=True) + return dir -def get_cache_dir(): - cache_dir = os.path.join(GLib.get_user_cache_dir(), "cozy") +def get_artwork_cache_dir() -> Path: + return get_path_relative_to_chache_folder("artwork") - if not os.path.exists(cache_dir): - os.makedirs(cache_dir) - return cache_dir +def get_offline_cache_dir() -> Path: + return get_path_relative_to_chache_folder("offline") -def get_data_dir(): - return os.path.join(GLib.get_user_data_dir(), "cozy") +def get_cache_dir() -> Path: + return get_path_relative_to_chache_folder() + + +def get_data_dir() -> Path: + dir = Path(GLib.get_user_data_dir(), "cozy") + dir.mkdir(parents=True, exist_ok=True) + + return dir diff --git a/cozy/control/artwork_cache.py b/cozy/control/artwork_cache.py index 0c904bbe..9032f39e 100644 --- a/cozy/control/artwork_cache.py +++ b/cozy/control/artwork_cache.py @@ -7,7 +7,7 @@ import inject from gi.repository import Gdk, GdkPixbuf -from cozy.control.application_directories import get_cache_dir +from cozy.control.application_directories import get_artwork_cache_dir from cozy.db.artwork_cache import ArtworkCache as ArtworkCacheModel from cozy.media.importer import Importer, ScanStatus from cozy.report import reporter @@ -23,10 +23,6 @@ def __init__(self): _app_settings = inject.instance(ApplicationSettings) _app_settings.add_listener(self._on_app_setting_changed) - @property - def artwork_cache_dir(self): - return Path(get_cache_dir()) / "artwork" - def get_cover_paintable(self, book, scale, size=0) -> Gdk.Texture | None: pixbuf = None size *= scale @@ -51,7 +47,7 @@ def delete_artwork_cache(self): """ Deletes the artwork cache completely. """ - shutil.rmtree(self.artwork_cache_dir, ignore_errors=True) + shutil.rmtree(get_artwork_cache_dir(), ignore_errors=True) ArtworkCacheModel.delete().execute() def _on_importer_event(self, event, data): @@ -75,7 +71,7 @@ def _create_artwork_cache(self, book, pixbuf, size): uuid = str(uuid4()) ArtworkCacheModel.create(book=book.id, uuid=uuid) - cache_dir = self.artwork_cache_dir / uuid + cache_dir = get_artwork_cache_dir() / uuid cache_dir.mkdir(exist_ok=True, parents=True) file_path = (cache_dir / str(size)).with_suffix(".jpg") @@ -104,7 +100,7 @@ def get_album_art_path(self, book, size) -> str: ) return None - cache_dir = self.artwork_cache_dir / uuid + cache_dir = get_artwork_cache_dir() / uuid if cache_dir.is_dir(): file_path = (cache_dir / str(size)).with_suffix(".jpg") diff --git a/cozy/control/db_updater.py b/cozy/control/db_updater.py index 962a659f..659aa32e 100644 --- a/cozy/control/db_updater.py +++ b/cozy/control/db_updater.py @@ -8,8 +8,9 @@ from playhouse.reflection import generate_models from cozy.control.application_directories import get_cache_dir +from cozy.control.application_directories import get_data_dir as get_data_dir_path from cozy.db.file import File -from cozy.db.model_base import get_data_dir, get_sqlite_database +from cozy.db.model_base import get_sqlite_database from cozy.db.offline_cache import OfflineCache from cozy.db.settings import Settings from cozy.db.storage import Storage @@ -20,6 +21,8 @@ log = logging.getLogger("db_updater") +get_data_dir = lambda: str(get_data_dir_path()) # i don't want to rewrite this file for pathlib + def __update_db_1(db): migrator = SqliteMigrator(db) diff --git a/cozy/control/offline_cache.py b/cozy/control/offline_cache.py index 3349b64f..e77e4887 100644 --- a/cozy/control/offline_cache.py +++ b/cozy/control/offline_cache.py @@ -1,13 +1,14 @@ import logging import os import uuid +from pathlib import Path import inject from gi.repository import Gio import cozy.tools as tools from cozy.architecture.event_sender import EventSender -from cozy.control.application_directories import get_cache_dir +from cozy.control.application_directories import get_offline_cache_dir from cozy.db.file import File from cozy.db.offline_cache import OfflineCache as OfflineCacheModel from cozy.db.track_to_file import TrackToFile @@ -25,6 +26,7 @@ class OfflineCache(EventSender): This includes operations like copying to the cache and adding or removing files from the cache. """ + queue = [] total_batch_count = 0 current_batch_count = 0 @@ -38,16 +40,16 @@ def __init__(self): super().__init__() from cozy.media.importer import Importer + self._importer = inject.instance(Importer) from cozy.model.library import Library + self._library = inject.instance(Library) self._importer.add_listener(self._on_importer_event) - self.cache_dir = os.path.join(get_cache_dir(), "offline") - if not os.path.exists(self.cache_dir): - os.makedirs(self.cache_dir) + self.cache_dir = get_offline_cache_dir() self._start_processing() @@ -136,14 +138,13 @@ def delete_cache(self): Deletes the entire offline cache files. Doesn't delete anything from the cozy.db. """ - cache_dir = os.path.join(get_cache_dir(), "offline") import shutil - shutil.rmtree(cache_dir) + + shutil.rmtree(self.cache_dir) def _stop_processing(self): - """ - """ + """ """ if not self._is_processing() or not self.thread: return diff --git a/cozy/db/model_base.py b/cozy/db/model_base.py index 4c3ff4be..18a57600 100644 --- a/cozy/db/model_base.py +++ b/cozy/db/model_base.py @@ -1,5 +1,4 @@ import logging -import os from peewee import Model from playhouse.sqliteq import SqliteQueueDatabase @@ -17,14 +16,13 @@ def get_sqlite_database(): def database_file_exists(): - return os.path.exists(os.path.join(get_data_dir(), "cozy.db")) + return (get_data_dir(), "cozy.db").is_file() def __open_database(): global _db - if not os.path.exists(get_data_dir()): - os.makedirs(get_data_dir()) - _db = SqliteQueueDatabase(os.path.join(get_data_dir(), "cozy.db"), queue_max_size=128, results_timeout=15.0, + + _db = SqliteQueueDatabase(str(get_data_dir() / "cozy.db"), queue_max_size=128, results_timeout=15.0, timeout=15.0, pragmas=[('cache_size', -1024 * 32), ('journal_mode', 'wal')]) diff --git a/cozy/media/player.py b/cozy/media/player.py index 8b13023d..ac79a47a 100644 --- a/cozy/media/player.py +++ b/cozy/media/player.py @@ -538,8 +538,8 @@ def _get_playback_path(self, chapter: Chapter): if self._book.offline and self._book.downloaded: path = self._offline_cache.get_cached_path(chapter) - if path and os.path.exists(path): - return path + if path and path.exists(): + return str(path) return chapter.file diff --git a/main.py b/main.py index ef4f3005..9fd8d9ef 100755 --- a/main.py +++ b/main.py @@ -39,6 +39,8 @@ from gi.repository import Gio, GLib +from cozy.control.application_directories import get_data_dir + pkgdatadir = "@DATA_DIR@" localedir = "@LOCALE_DIR@" @@ -54,9 +56,7 @@ log = logging.getLogger("main") -data_dir = Path(GLib.get_user_data_dir()) / "cozy" -if not data_dir.is_dir(): - data_dir.mkdir(parents=True) +data_dir = get_data_dir() # setup log files log_file = data_dir / "cozy.log" From 2a86caa94d88e33d2a639c66654dd0e7e3b70e2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20D=C3=A9v=C3=A9nyi?= Date: Tue, 24 Sep 2024 16:37:30 +0200 Subject: [PATCH 8/9] Fix issues ruff warned about --- cozy/control/offline_cache.py | 8 ++++---- main.py | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/cozy/control/offline_cache.py b/cozy/control/offline_cache.py index e77e4887..10f8bacd 100644 --- a/cozy/control/offline_cache.py +++ b/cozy/control/offline_cache.py @@ -106,21 +106,21 @@ def remove(self, book: Book): def remove_all_for_storage(self, storage): for element in OfflineCacheModel.select().join(File).where( storage.path in OfflineCacheModel.original_file.path): - file_path = os.path.join(self.cache_dir, element.cached_file) + file_path = self.cache_dir / element.cached_file if file_path == self.cache_dir: continue - file = Gio.File.new_for_path(file_path) + file = Gio.File.new_for_path(str(file_path)) if file.query_exists(): file.delete() OfflineCacheModel.delete().where(storage.path in OfflineCacheModel.original_file.path).execute() - def get_cached_path(self, chapter: Chapter): + def get_cached_path(self, chapter: Chapter) -> Path: query = OfflineCacheModel.select().where(OfflineCacheModel.original_file == chapter.file_id, OfflineCacheModel.copied) if query.count() > 0: - return os.path.join(self.cache_dir, query.get().cached_file) + return self.cache_dir / query.get().cached_file else: return None diff --git a/main.py b/main.py index 9fd8d9ef..614eda15 100755 --- a/main.py +++ b/main.py @@ -26,7 +26,6 @@ import signal import sys import traceback -from pathlib import Path import gi @@ -37,7 +36,7 @@ gi.require_version("GstController", "1.0") gi.require_version("GstPbutils", "1.0") -from gi.repository import Gio, GLib +from gi.repository import Gio from cozy.control.application_directories import get_data_dir From 5b3429fa43e5ac0b73e802f7d8c9ea4b7a721b69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20D=C3=A9v=C3=A9nyi?= Date: Thu, 26 Sep 2024 19:50:46 +0200 Subject: [PATCH 9/9] Update cozy/db/model_base.py --- cozy/db/model_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cozy/db/model_base.py b/cozy/db/model_base.py index 18a57600..3dd9be77 100644 --- a/cozy/db/model_base.py +++ b/cozy/db/model_base.py @@ -16,7 +16,7 @@ def get_sqlite_database(): def database_file_exists(): - return (get_data_dir(), "cozy.db").is_file() + return (get_data_dir() / "cozy.db").is_file() def __open_database():