From 6cda5f3f908b6d3c89d9e6e9bc1f57c183527008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20D=C3=A9v=C3=A9nyi?= Date: Fri, 29 Nov 2024 15:19:31 +0100 Subject: [PATCH] Clean up files and remove small useless ones (#965) * Cleanup logs in main.py * Move power manager out of its own file * Move enums to a common file * I could never get my head around these filenames * Missed a file again, thanks, Ruff * Seems like I chose a bad way to clean up the imports * Start using pathlib in more places * Fix issues ruff warned about * Update cozy/db/model_base.py --- cozy/app_controller.py | 9 +- cozy/control/application_directories.py | 28 +++-- cozy/control/artwork_cache.py | 14 +-- cozy/control/db_updater.py | 5 +- cozy/control/mpris.py | 2 +- cozy/control/offline_cache.py | 25 ++-- cozy/db/model_base.py | 8 +- cozy/enums.py | 19 +++ cozy/media/player.py | 32 ++++- cozy/model/book.py | 2 +- cozy/open_view.py | 9 -- cozy/power_manager.py | 33 ----- cozy/report/report_to_loki.py | 2 +- cozy/{application_settings.py => settings.py} | 0 cozy/ui/app_view.py | 2 +- cozy/ui/main_view.py | 2 +- cozy/ui/widgets/error_reporting.py | 2 +- cozy/view.py | 11 -- cozy/view_model/app_view_model.py | 2 +- cozy/view_model/book_detail_view_model.py | 4 +- cozy/view_model/headerbar_view_model.py | 2 +- cozy/view_model/library_view_model.py | 4 +- .../view_model/playback_control_view_model.py | 2 +- cozy/view_model/search_view_model.py | 4 +- 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 +- main.py | 113 +++++++++--------- test/conftest.py | 16 +-- test/cozy/media/test_importer.py | 4 +- test/cozy/media/test_player.py | 2 +- test/cozy/model/storage_block_list.py | 2 +- test/cozy/model/test_book.py | 56 ++++----- test/cozy/model/test_database_importer.py | 76 ++++++------ test/cozy/model/test_library.py | 6 +- test/cozy/model/test_settings.py | 16 +-- test/cozy/model/test_storage.py | 2 +- test/cozy/model/test_track.py | 36 +++--- 38 files changed, 273 insertions(+), 285 deletions(-) create mode 100644 cozy/enums.py delete mode 100644 cozy/open_view.py delete mode 100644 cozy/power_manager.py rename cozy/{application_settings.py => settings.py} (100%) diff --git a/cozy/app_controller.py b/cozy/app_controller.py index 67dfed58..45802f15 100644 --- a/cozy/app_controller.py +++ b/cozy/app_controller.py @@ -2,20 +2,19 @@ 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 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.power_manager import PowerManager 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 @@ -23,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 @@ -74,8 +72,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 +94,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/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 61575bdc..9032f39e 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.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 +from cozy.settings import ApplicationSettings log = logging.getLogger("artwork_cache") @@ -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/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/control/offline_cache.py b/cozy/control/offline_cache.py index 3349b64f..10f8bacd 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() @@ -104,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 @@ -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..3dd9be77 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/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 3b3bfac4..ac79a47a 100644 --- a/cozy/media/player.py +++ b/cozy/media/player.py @@ -4,9 +4,8 @@ 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 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 @@ -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) @@ -514,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/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/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/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/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/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/ui/main_view.py b/cozy/ui/main_view.py index eed7196c..d5ca71c4 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.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..bf02a229 100644 --- a/cozy/view_model/book_detail_view_model.py +++ b/cozy/view_model/book_detail_view_model.py @@ -1,17 +1,17 @@ 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 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 +from cozy.settings import ApplicationSettings 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 dcded2c7..2c3581d8 100644 --- a/cozy/view_model/library_view_model.py +++ b/cozy/view_model/library_view_model.py @@ -5,16 +5,16 @@ 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 +from cozy.enums import OpenView 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 from cozy.ui.widgets.book_card import BookCard from cozy.view_model.storages_view_model import StoragesViewModel diff --git a/cozy/view_model/playback_control_view_model.py b/cozy/view_model/playback_control_view_model.py index 027094e3..a46b5085 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 40967d1c..579d2082 100644 --- a/cozy/view_model/search_view_model.py +++ b/cozy/view_model/search_view_model.py @@ -3,13 +3,13 @@ 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 +from cozy.enums import OpenView 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 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/main.py b/main.py index 7319e4e7..614eda15 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 +from gi.repository import Gio -pkgdatadir = '@DATA_DIR@' -localedir = '@LOCALE_DIR@' +from cozy.control.application_directories import get_data_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 = get_data_dir() + +# setup log files +log_file = data_dir / "cozy.log" +old_log_file = data_dir / "cozy.old.log" -old_except_hook = None +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 diff --git a/test/conftest.py b/test/conftest.py index 2aa48d09..0b4ef9ea 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -1,9 +1,8 @@ import json import os -import pytest - import gi +import pytest gi.require_version('Gtk', '4.0') gi.require_version('Gdk', '4.0') @@ -26,12 +25,12 @@ def chunks(lst, n): @pytest.fixture(scope="function") def peewee_database(): - from cozy.db.track import Track from cozy.db.book import Book + from cozy.db.file import File from cozy.db.settings import Settings - from cozy.db.storage_blacklist import StorageBlackList from cozy.db.storage import Storage - from cozy.db.file import File + from cozy.db.storage_blacklist import StorageBlackList + from cozy.db.track import Track from cozy.db.track_to_file import TrackToFile db_path, models, test_db = prepare_db() @@ -77,8 +76,8 @@ def peewee_database(): @pytest.fixture(scope="function") def peewee_database_storage(): - from cozy.db.storage import Storage from cozy.db.settings import Settings + from cozy.db.storage import Storage from cozy.db.storage_blacklist import StorageBlackList db_path, models, test_db = prepare_db() @@ -106,16 +105,17 @@ def teardown_db(db_path, models, test_db): def prepare_db(): from playhouse.pool import PooledSqliteDatabase + from cozy.db.artwork_cache import ArtworkCache from cozy.db.book import Book + from cozy.db.collation import collate_natural + from cozy.db.file import File from cozy.db.offline_cache import OfflineCache from cozy.db.settings import Settings from cozy.db.storage import Storage from cozy.db.storage_blacklist import StorageBlackList from cozy.db.track import Track - from cozy.db.file import File from cozy.db.track_to_file import TrackToFile - from cozy.db.collation import collate_natural models = [Track, Book, File, TrackToFile, Settings, ArtworkCache, Storage, StorageBlackList, OfflineCache] diff --git a/test/cozy/media/test_importer.py b/test/cozy/media/test_importer.py index 054cc583..692a568d 100644 --- a/test/cozy/media/test_importer.py +++ b/test/cozy/media/test_importer.py @@ -20,8 +20,8 @@ def setup_inject(peewee_database_storage): def test_external_paths_are_excluded_when_offline(mocker): - from cozy.media.importer import Importer from cozy.db.storage import Storage + from cozy.media.importer import Importer mocker.patch("os.path.exists", return_value=True) mocker.patch("cozy.control.filesystem_monitor.FilesystemMonitor.is_storage_online", autospec=True, @@ -48,8 +48,8 @@ def test_paths_not_existing_are_excluded(mocker): def test_all_existing_paths_are_included(mocker): - from cozy.media.importer import Importer from cozy.db.storage import Storage + from cozy.media.importer import Importer mocker.patch("os.path.exists", return_value=True) diff --git a/test/cozy/media/test_player.py b/test/cozy/media/test_player.py index 49bb2de6..85b0acfa 100644 --- a/test/cozy/media/test_player.py +++ b/test/cozy/media/test_player.py @@ -4,10 +4,10 @@ import pytest from peewee import SqliteDatabase -from cozy.application_settings import ApplicationSettings from cozy.media.player import GstPlayer from cozy.model.library import Library from cozy.model.settings import Settings +from cozy.settings import ApplicationSettings @pytest.fixture(autouse=True) diff --git a/test/cozy/model/storage_block_list.py b/test/cozy/model/storage_block_list.py index 0ccf3f60..e4e3abdc 100644 --- a/test/cozy/model/storage_block_list.py +++ b/test/cozy/model/storage_block_list.py @@ -11,8 +11,8 @@ def setup_inject(peewee_database_storage): def test_rebase_path(): - from cozy.model.storage_block_list import StorageBlockList from cozy.db.storage_blacklist import StorageBlackList + from cozy.model.storage_block_list import StorageBlockList model = StorageBlockList() diff --git a/test/cozy/model/test_book.py b/test/cozy/model/test_book.py index 30a01394..b8b9bd27 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) @@ -23,8 +23,8 @@ def test_db_created(peewee_database): def test_name_returns_correct_value(peewee_database): - from cozy.model.book import Book from cozy.db.book import Book as BookDB + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(1)) assert book.name == "Test Book" @@ -41,8 +41,8 @@ def test_setting_name_updates_in_book_object_and_database(peewee_database): def test_author_returns_correct_value(peewee_database): - from cozy.model.book import Book from cozy.db.book import Book as BookDB + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(1)) assert book.author == "Test Author" @@ -59,8 +59,8 @@ def test_setting_author_updates_in_book_object_and_database(peewee_database): def test_reader_returns_correct_value(peewee_database): - from cozy.model.book import Book from cozy.db.book import Book as BookDB + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(1)) assert book.reader == "Test Reader" @@ -77,8 +77,8 @@ def test_setting_reader_updates_in_book_object_and_database(peewee_database): def test_position_returns_default_value(peewee_database): - from cozy.model.book import Book from cozy.db.book import Book as BookDB + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(1)) assert book.position == 0 @@ -95,8 +95,8 @@ def test_setting_position_updates_in_book_object_and_database(peewee_database): def test_rating_returns_default_value(peewee_database): - from cozy.model.book import Book from cozy.db.book import Book as BookDB + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(1)) assert book.rating == 0 @@ -113,8 +113,8 @@ def test_setting_rating_updates_in_book_object_and_database(peewee_database): def test_cover_returns_default_value(peewee_database): - from cozy.model.book import Book from cozy.db.book import Book as BookDB + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(1)) assert book.cover is None @@ -131,8 +131,8 @@ def test_setting_cover_updates_in_book_object_and_database(peewee_database): def test_playback_speed_returns_default_value(peewee_database): - from cozy.model.book import Book from cozy.db.book import Book as BookDB + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(1)) assert book.playback_speed == 1.0 @@ -149,8 +149,8 @@ def test_setting_playback_speed_updates_in_book_object_and_database(peewee_datab def test_last_played_returns_default_value(peewee_database): - from cozy.model.book import Book from cozy.db.book import Book as BookDB + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(1)) assert book.last_played == 0 @@ -167,8 +167,8 @@ def test_setting_last_played_updates_in_book_object_and_database(peewee_database def test_offline_returns_default_value(peewee_database): - from cozy.model.book import Book from cozy.db.book import Book as BookDB + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(1)) assert not book.offline @@ -185,8 +185,8 @@ def test_setting_offline_updates_in_book_object_and_database(peewee_database): def test_downloaded_returns_default_value(peewee_database): - from cozy.model.book import Book from cozy.db.book import Book as BookDB + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(1)) assert not book.downloaded @@ -203,16 +203,16 @@ def test_setting_downloaded_updates_in_book_object_and_database(peewee_database) def test_chapters_return_correct_count_of_chapters(peewee_database): - from cozy.model.book import Book from cozy.db.book import Book as BookDB + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(1)) assert len(book.chapters) == 1 def test_tracks_are_ordered_by_disk_number_name(peewee_database): - from cozy.model.book import Book from cozy.db.book import Book as BookDB + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(9)) @@ -223,8 +223,8 @@ def test_tracks_are_ordered_by_disk_number_name(peewee_database): def test_current_track_is_actually_current_track(peewee_database): - from cozy.model.book import Book from cozy.db.book import Book as BookDB + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(9)) @@ -232,26 +232,26 @@ def test_current_track_is_actually_current_track(peewee_database): def test_try_to_init_empty_book_should_throw_exception(peewee_database): - from cozy.model.book import Book - from cozy.model.book import BookIsEmpty from cozy.db.book import Book as BookDB + from cozy.model.book import Book, BookIsEmpty with pytest.raises(BookIsEmpty): Book(peewee_database, BookDB.get(10)) def test_try_to_init_non_existant_book_throws_exception(peewee_database): - from cozy.model.book import Book - from cozy.db.book import Book as BookDB from peewee import DoesNotExist + from cozy.db.book import Book as BookDB + from cozy.model.book import Book + with pytest.raises(DoesNotExist): Book(peewee_database, BookDB.get(-42)) def test_delete_deletes_book_from_db(peewee_database, mocker): - from cozy.model.book import Book from cozy.db.book import Book as BookDB + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(1)) spy = mocker.spy(book, "emit_event") @@ -263,9 +263,9 @@ def test_delete_deletes_book_from_db(peewee_database, mocker): def test_deleted_book_removed_from_last_played_book_if_necessary(peewee_database): + from cozy.db.book import Book as BookDB from cozy.model.book import Book from cozy.model.settings import Settings - from cozy.db.book import Book as BookDB settings = Settings() inject.clear_and_configure( @@ -279,16 +279,16 @@ def test_deleted_book_removed_from_last_played_book_if_necessary(peewee_database def test_skipping_removing_a_non_existing_chapter(peewee_database): - from cozy.model.book import Book from cozy.db.book import Book as BookDB + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(1)) book._on_chapter_event("chapter-deleted", None) def test_progress_return_progress_for_started_book(peewee_database): - from cozy.model.book import Book from cozy.db.book import Book as BookDB + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(1)) chapter = book.chapters[0] @@ -299,8 +299,8 @@ def test_progress_return_progress_for_started_book(peewee_database): def test_progress_should_be_zero_for_unstarted_book(peewee_database): - from cozy.model.book import Book from cozy.db.book import Book as BookDB + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(1)) book.position = 0 @@ -309,8 +309,8 @@ def test_progress_should_be_zero_for_unstarted_book(peewee_database): def test_progress_should_be_100_for_finished_book(peewee_database): - from cozy.model.book import Book from cozy.db.book import Book as BookDB + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(1)) book.position = -1 @@ -319,11 +319,11 @@ def test_progress_should_be_100_for_finished_book(peewee_database): def test_removing_book_removes_all_traces_in_db(peewee_database): - from cozy.model.book import Book from cozy.db.book import Book as BookDB - from cozy.db.track import Track from cozy.db.file import File + from cozy.db.track import Track from cozy.db.track_to_file import TrackToFile + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(1)) track_ids = [chapter.id for chapter in book.chapters] @@ -345,11 +345,11 @@ def test_removing_book_removes_all_traces_in_db(peewee_database): def test_removing_book_with_missing_file_removes_all_traces_in_db(peewee_database): - from cozy.model.book import Book from cozy.db.book import Book as BookDB - from cozy.db.track import Track from cozy.db.file import File + from cozy.db.track import Track from cozy.db.track_to_file import TrackToFile + from cozy.model.book import Book book = Book(peewee_database, BookDB.get(1)) track_ids = [chapter.id for chapter in book.chapters] diff --git a/test/cozy/model/test_database_importer.py b/test/cozy/model/test_database_importer.py index 266e38fd..491aaaee 100644 --- a/test/cozy/model/test_database_importer.py +++ b/test/cozy/model/test_database_importer.py @@ -12,8 +12,8 @@ def setup_inject(peewee_database): def test_prepare_files_db_objects_skips_existing_files(): - from cozy.model.database_importer import DatabaseImporter from cozy.media.media_file import MediaFile + from cozy.model.database_importer import DatabaseImporter media_file = MediaFile(book_name="New Book Name", author="New Author", @@ -30,8 +30,8 @@ def test_prepare_files_db_objects_skips_existing_files(): def test_prepare_files_db_objects_skips_duplicate_file(): - from cozy.model.database_importer import DatabaseImporter from cozy.media.media_file import MediaFile + from cozy.model.database_importer import DatabaseImporter media_file = MediaFile(book_name="New Book Name", author="New Author", @@ -49,9 +49,9 @@ def test_prepare_files_db_objects_skips_duplicate_file(): def test_update_files_db_objects_updates_modified_field(): - from cozy.model.database_importer import DatabaseImporter - from cozy.media.media_file import MediaFile from cozy.db.file import File + from cozy.media.media_file import MediaFile + from cozy.model.database_importer import DatabaseImporter media_file = MediaFile(book_name="New Book Name", author="New Author", @@ -70,8 +70,8 @@ def test_update_files_db_objects_updates_modified_field(): def test_prepare_files_db_objects_returns_object_for_new_file(): - from cozy.model.database_importer import DatabaseImporter from cozy.media.media_file import MediaFile + from cozy.model.database_importer import DatabaseImporter media_file = MediaFile(book_name="New Book Name", author="New Author", @@ -96,10 +96,10 @@ def test_prepare_db_objects_skips_none(): def test_create_track_db_object_creates_object(): - from cozy.model.database_importer import DatabaseImporter - from cozy.media.media_file import MediaFile from cozy.db.book import Book from cozy.media.chapter import Chapter + from cozy.media.media_file import MediaFile + from cozy.model.database_importer import DatabaseImporter database_importer = DatabaseImporter() @@ -126,10 +126,10 @@ def test_create_track_db_object_creates_object(): def test_update_book_db_object_updates_object(): - from cozy.model.database_importer import DatabaseImporter - from cozy.media.media_file import MediaFile from cozy.db.book import Book from cozy.media.chapter import Chapter + from cozy.media.media_file import MediaFile + from cozy.model.database_importer import DatabaseImporter database_importer = DatabaseImporter() @@ -154,10 +154,10 @@ def test_update_book_db_object_updates_object(): def test_update_book_db_object_updates_object_regardless_of_book_spelling(): - from cozy.model.database_importer import DatabaseImporter - from cozy.media.media_file import MediaFile from cozy.db.book import Book from cozy.media.chapter import Chapter + from cozy.media.media_file import MediaFile + from cozy.model.database_importer import DatabaseImporter database_importer = DatabaseImporter() @@ -182,10 +182,10 @@ def test_update_book_db_object_updates_object_regardless_of_book_spelling(): def test_create_book_db_object_creates_object(): - from cozy.model.database_importer import DatabaseImporter - from cozy.media.media_file import MediaFile from cozy.db.book import Book from cozy.media.chapter import Chapter + from cozy.media.media_file import MediaFile + from cozy.model.database_importer import DatabaseImporter database_importer = DatabaseImporter() @@ -212,11 +212,11 @@ def test_create_book_db_object_creates_object(): def test_prepare_db_objects_recreates_existing_track(mocker): - from cozy.model.database_importer import DatabaseImporter - from cozy.media.media_file import MediaFile - from cozy.media.chapter import Chapter - from cozy.db.track_to_file import TrackToFile from cozy.db.file import File + from cozy.db.track_to_file import TrackToFile + from cozy.media.chapter import Chapter + from cozy.media.media_file import MediaFile + from cozy.model.database_importer import DatabaseImporter database_importer = DatabaseImporter() @@ -248,9 +248,9 @@ def test_prepare_db_objects_recreates_existing_track(mocker): def test_prepare_db_objects_skips_if_file_object_not_present(mocker): - from cozy.model.database_importer import DatabaseImporter - from cozy.media.media_file import MediaFile from cozy.media.chapter import Chapter + from cozy.media.media_file import MediaFile + from cozy.model.database_importer import DatabaseImporter database_importer = DatabaseImporter() @@ -270,10 +270,10 @@ def test_prepare_db_objects_skips_if_file_object_not_present(mocker): def test_prepare_db_objects_creates_new_track(mocker): - from cozy.model.database_importer import DatabaseImporter - from cozy.media.media_file import MediaFile - from cozy.media.chapter import Chapter from cozy.db.file import File + from cozy.media.chapter import Chapter + from cozy.media.media_file import MediaFile + from cozy.model.database_importer import DatabaseImporter database_importer = DatabaseImporter() spy = mocker.spy(database_importer, "_get_track_list_for_db") @@ -296,10 +296,10 @@ def test_prepare_db_objects_creates_new_track(mocker): def test_prepare_db_objects_updates_existing_book(mocker): - from cozy.model.database_importer import DatabaseImporter - from cozy.media.media_file import MediaFile - from cozy.media.chapter import Chapter from cozy.db.file import File + from cozy.media.chapter import Chapter + from cozy.media.media_file import MediaFile + from cozy.model.database_importer import DatabaseImporter database_importer = DatabaseImporter() spy = mocker.spy(database_importer, "_update_book_db_object") @@ -322,10 +322,10 @@ def test_prepare_db_objects_updates_existing_book(mocker): def test_prepare_db_objects_updates_existing_book_regardless_of_spelling(mocker): - from cozy.model.database_importer import DatabaseImporter - from cozy.media.media_file import MediaFile - from cozy.media.chapter import Chapter from cozy.db.file import File + from cozy.media.chapter import Chapter + from cozy.media.media_file import MediaFile + from cozy.model.database_importer import DatabaseImporter database_importer = DatabaseImporter() spy = mocker.spy(database_importer, "_update_book_db_object") @@ -358,10 +358,10 @@ def test_prepare_db_objects_updates_existing_book_regardless_of_spelling(mocker) def test_prepare_db_objects_creates_new_book(mocker): - from cozy.model.database_importer import DatabaseImporter - from cozy.media.media_file import MediaFile - from cozy.media.chapter import Chapter from cozy.db.file import File + from cozy.media.chapter import Chapter + from cozy.media.media_file import MediaFile + from cozy.model.database_importer import DatabaseImporter database_importer = DatabaseImporter() spy = mocker.spy(database_importer, "_create_book_db_object") @@ -384,11 +384,11 @@ def test_prepare_db_objects_creates_new_book(mocker): def test_delete_all_tracks_from_db_does_as_it_says(): - from cozy.media.media_file import MediaFile - from cozy.media.chapter import Chapter from cozy.db.file import File from cozy.db.track import Track from cozy.db.track_to_file import TrackToFile + from cozy.media.chapter import Chapter + from cozy.media.media_file import MediaFile from cozy.model.database_importer import DatabaseImporter database_importer = DatabaseImporter() @@ -467,10 +467,10 @@ def test_is_chapter_count_in_db_different_returns_false_for_equal_chapter_count( def test_insert_track_inserts_all_rows_expected(): - from cozy.model.database_importer import DatabaseImporter, TrackInsertRequest from cozy.db.book import Book from cozy.db.file import File from cozy.db.track_to_file import TrackToFile + from cozy.model.database_importer import DatabaseImporter, TrackInsertRequest database_importer = DatabaseImporter() @@ -501,8 +501,8 @@ def test_insert_track_inserts_all_rows_expected(): def test_update_book_position_skips_empty_book(): - from cozy.model.database_importer import DatabaseImporter from cozy.db.book import Book + from cozy.model.database_importer import DatabaseImporter database_importer = DatabaseImporter() @@ -511,9 +511,9 @@ def test_update_book_position_skips_empty_book(): def test_update_book_position_sets_position_for_multi_chapter_file_correctly(): - from cozy.model.database_importer import DatabaseImporter from cozy.db.book import Book from cozy.db.track import Track + from cozy.model.database_importer import DatabaseImporter database_importer = DatabaseImporter() @@ -526,9 +526,9 @@ def test_update_book_position_sets_position_for_multi_chapter_file_correctly(): def test_update_book_position_sets_position_for_single_chapter_file_correctly(): - from cozy.model.database_importer import DatabaseImporter from cozy.db.book import Book from cozy.db.track import Track + from cozy.model.database_importer import DatabaseImporter database_importer = DatabaseImporter() @@ -542,8 +542,8 @@ def test_update_book_position_sets_position_for_single_chapter_file_correctly(): def test_update_book_position_resets_position_if_it_is_longer_than_the_duration(): - from cozy.model.database_importer import DatabaseImporter from cozy.db.book import Book + from cozy.model.database_importer import DatabaseImporter database_importer = DatabaseImporter() diff --git a/test/cozy/model/test_library.py b/test/cozy/model/test_library.py index d090bf0b..42896ebe 100644 --- a/test/cozy/model/test_library.py +++ b/test/cozy/model/test_library.py @@ -4,8 +4,8 @@ import pytest from peewee import SqliteDatabase -from cozy.application_settings import ApplicationSettings from cozy.model.settings import Settings +from cozy.settings import ApplicationSettings @pytest.fixture(autouse=True) @@ -27,8 +27,8 @@ def test_library_contains_books(): def test_authors_contains_every_author_from_db(): - from cozy.model.library import Library, split_strings_to_set from cozy.db.book import Book + from cozy.model.library import Library, split_strings_to_set library = Library() books = Book.select(Book.author).distinct().order_by(Book.author) @@ -41,8 +41,8 @@ def test_authors_contains_every_author_from_db(): def test_readers_contains_every_reader_from_db(): - from cozy.model.library import Library, split_strings_to_set from cozy.db.book import Book + from cozy.model.library import Library, split_strings_to_set library = Library() books = Book.select(Book.reader).distinct().order_by(Book.reader) diff --git a/test/cozy/model/test_settings.py b/test/cozy/model/test_settings.py index 7f12f065..6d557076 100644 --- a/test/cozy/model/test_settings.py +++ b/test/cozy/model/test_settings.py @@ -11,8 +11,8 @@ def setup_inject(peewee_database): def test_storage_locations_contains_every_storage_location_from_db(peewee_database): - from cozy.model.settings import Settings from cozy.db.storage import Storage + from cozy.model.settings import Settings settings = Settings() storage_locations = Storage.select() @@ -28,8 +28,8 @@ def test_storage_locations_contains_every_storage_location_from_db(peewee_databa def test_external_storage_locations_contain_only_external_storages(peewee_database): - from cozy.model.settings import Settings from cozy.db.storage import Storage + from cozy.model.settings import Settings settings = Settings() storage_locations = Storage.select().where(Storage.external) @@ -39,8 +39,8 @@ def test_external_storage_locations_contain_only_external_storages(peewee_databa def test_last_played_book_returns_correct_value(peewee_database): - from cozy.model.settings import Settings from cozy.db.book import Book + from cozy.model.settings import Settings settings = Settings() @@ -48,8 +48,8 @@ def test_last_played_book_returns_correct_value(peewee_database): def test_setting_last_played_book_to_none_updates_in_settings_object_and_database(peewee_database): - from cozy.model.settings import Settings from cozy.db.settings import Settings as SettingsModel + from cozy.model.settings import Settings settings = Settings() settings.last_played_book = None @@ -59,8 +59,8 @@ def test_setting_last_played_book_to_none_updates_in_settings_object_and_databas def test_fetching_non_existent_last_played_book_returns_none(peewee_database): - from cozy.model.settings import Settings from cozy.db.settings import Settings as SettingsModel + from cozy.model.settings import Settings db_object = SettingsModel.get() db_object.last_played_book = 437878782 @@ -72,8 +72,8 @@ def test_fetching_non_existent_last_played_book_returns_none(peewee_database): def test_fetching_non_existent_last_played_book_sets_it_to_none(peewee_database): - from cozy.model.settings import Settings from cozy.db.settings import Settings as SettingsModel + from cozy.model.settings import Settings db_object = SettingsModel.get() db_object.last_played_book = 437878782 @@ -86,8 +86,8 @@ def test_fetching_non_existent_last_played_book_sets_it_to_none(peewee_database) def test_ensure_default_storage_is_present_adds_default_if_not_present(peewee_database): - from cozy.model.settings import Settings from cozy.db.storage import Storage + from cozy.model.settings import Settings Storage.update(default=False).where(Storage.id == 2).execute() @@ -99,8 +99,8 @@ def test_ensure_default_storage_is_present_adds_default_if_not_present(peewee_da def test_ensure_default_storage_is_present_does_nothing_if_default_is_present(peewee_database): - from cozy.model.settings import Settings from cozy.db.storage import Storage + from cozy.model.settings import Settings settings = Settings() settings._load_all_storage_locations() diff --git a/test/cozy/model/test_storage.py b/test/cozy/model/test_storage.py index 46d0e87b..70a62cb4 100644 --- a/test/cozy/model/test_storage.py +++ b/test/cozy/model/test_storage.py @@ -21,7 +21,7 @@ def test_setting_path_updates_in_track_object_and_database(peewee_database_stora def test_setting_invalid_path_raises_exception(peewee_database_storage): - from cozy.model.storage import Storage, InvalidPath + from cozy.model.storage import InvalidPath, Storage invalid_path = "not an absolute path" storage = Storage(peewee_database_storage, 1) diff --git a/test/cozy/model/test_track.py b/test/cozy/model/test_track.py index fe9ac105..92e6ac4e 100644 --- a/test/cozy/model/test_track.py +++ b/test/cozy/model/test_track.py @@ -8,16 +8,16 @@ def test_db_created(peewee_database): def test_name_returns_correct_value(peewee_database): - from cozy.model.track import Track from cozy.db.track import Track as TrackDB + from cozy.model.track import Track track = Track(peewee_database, TrackDB.get(1)) assert track.name == "Test Track" def test_name_returns_generated_name_when_no_name_is_present(peewee_database): - from cozy.model.track import Track from cozy.db.track import Track as TrackDB + from cozy.model.track import Track track = Track(peewee_database, TrackDB.get(234)) @@ -35,8 +35,8 @@ def test_setting_name_updates_in_track_object_and_database(peewee_database): def test_number_returns_correct_value(peewee_database): - from cozy.model.track import Track from cozy.db.track import Track as TrackDB + from cozy.model.track import Track track = Track(peewee_database, TrackDB.get(1)) assert track.number == 1 @@ -53,8 +53,8 @@ def test_setting_number_updates_in_track_object_and_database(peewee_database): def test_disk_returns_correct_value(peewee_database): - from cozy.model.track import Track from cozy.db.track import Track as TrackDB + from cozy.model.track import Track track = Track(peewee_database, TrackDB.get(1)) assert track.disk == 1 @@ -71,8 +71,8 @@ def test_setting_disk_updates_in_track_object_and_database(peewee_database): def test_position_returns_default_value(peewee_database): - from cozy.model.track import Track from cozy.db.track import Track as TrackDB + from cozy.model.track import Track track = Track(peewee_database, TrackDB.get(1)) assert track.position == 0 @@ -89,17 +89,17 @@ def test_setting_position_updates_in_track_object_and_database(peewee_database): def test_file_returns_default_value(peewee_database): - from cozy.model.track import Track from cozy.db.track import Track as TrackDB + from cozy.model.track import Track track = Track(peewee_database, TrackDB.get(1)) assert track.file == "test.mp3" def test_setting_file_updates_in_track_object_and_database(peewee_database): + from cozy.db.track import Track as TrackDB from cozy.db.track_to_file import TrackToFile from cozy.model.track import Track - from cozy.db.track import Track as TrackDB track = Track(peewee_database, TrackDB.get(1)) track.file = "altered.mp3" @@ -109,10 +109,10 @@ def test_setting_file_updates_in_track_object_and_database(peewee_database): def test_setting_file_gets_file_object_if_it_is_already_present_in_database(peewee_database): - from cozy.db.track_to_file import TrackToFile from cozy.db.file import File - from cozy.model.track import Track from cozy.db.track import Track as TrackDB + from cozy.db.track_to_file import TrackToFile + from cozy.model.track import Track track = Track(peewee_database, TrackDB.get(1)) track.file = "file with multiple chapters.m4b" @@ -125,10 +125,10 @@ def test_setting_file_gets_file_object_if_it_is_already_present_in_database(peew def test_setting_file_gets_file_object_if_it_is_already_present_in_database_but_preserves_old_file_if_still_used( peewee_database): - from cozy.db.track_to_file import TrackToFile from cozy.db.file import File - from cozy.model.track import Track from cozy.db.track import Track as TrackDB + from cozy.db.track_to_file import TrackToFile + from cozy.model.track import Track track = Track(peewee_database, TrackDB.get(230)) track.file = "Changed path" @@ -140,8 +140,8 @@ def test_setting_file_gets_file_object_if_it_is_already_present_in_database_but_ def test_length_returns_default_value(peewee_database): - from cozy.model.track import Track from cozy.db.track import Track as TrackDB + from cozy.model.track import Track track = Track(peewee_database, TrackDB.get(1)) assert track.length == 42.1 * 1e9 @@ -158,9 +158,9 @@ def test_setting_length_updates_in_track_object_and_database(peewee_database): def test_setting_modified_updates_in_track_object_and_database(peewee_database): - from cozy.model.track import Track - from cozy.db.track_to_file import TrackToFile from cozy.db.track import Track as TrackDB + from cozy.db.track_to_file import TrackToFile + from cozy.model.track import Track track = Track(peewee_database, TrackDB.get(1)) track.modified = 42 @@ -186,8 +186,8 @@ def test_delete_deletes_track_from_db(peewee_database, mocker): def test_delete_does_not_delete_book(peewee_database): - from cozy.db.track import Track as TrackDB from cozy.db.book import Book + from cozy.db.track import Track as TrackDB from cozy.model.track import Track track = Track(peewee_database, TrackDB.get(1)) @@ -198,8 +198,8 @@ def test_delete_does_not_delete_book(peewee_database): def test_track_to_file_not_present_throws_exception_and_deletes_track_instance(peewee_database): - from cozy.db.track_to_file import TrackToFile from cozy.db.track import Track as TrackDB + from cozy.db.track_to_file import TrackToFile from cozy.model.track import Track, TrackInconsistentData TrackToFile.select().join(TrackDB).where(TrackToFile.track.id == 1).get().delete_instance() @@ -211,8 +211,8 @@ def test_track_to_file_not_present_throws_exception_and_deletes_track_instance(p def test_delete_removes_file_object_if_not_used_elsewhere(peewee_database): from cozy.db.file import File - from cozy.model.track import Track from cozy.db.track import Track as TrackDB + from cozy.model.track import Track track = Track(peewee_database, TrackDB.get(1)) file_id = track.file_id @@ -223,8 +223,8 @@ def test_delete_removes_file_object_if_not_used_elsewhere(peewee_database): def test_delete_keeps_file_object_if_used_elsewhere(peewee_database): from cozy.db.file import File - from cozy.model.track import Track from cozy.db.track import Track as TrackDB + from cozy.model.track import Track track = Track(peewee_database, TrackDB.get(230)) file_id = track.file_id