From 242bd2401f33d7c9e3b646a2157d482d13afef5b Mon Sep 17 00:00:00 2001 From: rdbende Date: Thu, 21 Dec 2023 16:03:22 +0100 Subject: [PATCH] Add missing type annotations and format the code --- cozy/ui/preferences_view.py | 89 +++++++++++++++++--------- cozy/ui/widgets/storages.py | 69 ++++++++++++-------- cozy/view_model/storages_view_model.py | 24 ++++--- 3 files changed, 112 insertions(+), 70 deletions(-) diff --git a/cozy/ui/preferences_view.py b/cozy/ui/preferences_view.py index bf587d71..ce871893 100644 --- a/cozy/ui/preferences_view.py +++ b/cozy/ui/preferences_view.py @@ -1,14 +1,14 @@ -from gi.repository import Gtk -from cozy.view_model.settings_view_model import SettingsViewModel -from gi.repository import Adw, Gio -from typing import Callable +from typing import Any + +from gi.repository import Adw, Gio, Gtk from cozy.ext import inject from cozy.ui.widgets.error_reporting import ErrorReporting from cozy.ui.widgets.storages import StorageLocations +from cozy.view_model.settings_view_model import SettingsViewModel -@Gtk.Template.from_resource('/com/github/geigi/cozy/preferences.ui') +@Gtk.Template.from_resource("/com/github/geigi/cozy/preferences.ui") class PreferencesView(Adw.PreferencesWindow): __gtype_name__ = "PreferencesWindow" @@ -30,7 +30,7 @@ class PreferencesView(Adw.PreferencesWindow): forward_duration_adjustment: Gtk.Adjustment = Gtk.Template.Child() fadeout_duration_adjustment: Gtk.Adjustment = Gtk.Template.Child() - def __init__(self, **kwargs): + def __init__(self, **kwargs: Any) -> None: super().__init__(transient_for=self.main_window.window, **kwargs) error_reporting = ErrorReporting() @@ -46,33 +46,62 @@ def __init__(self, **kwargs): self.connect("close-request", self._hide_window) - def _bind_settings(self): - self._glib_settings.bind("dark-mode", self.dark_mode_switch, "active", - Gio.SettingsBindFlags.DEFAULT) - - self._glib_settings.bind("swap-author-reader", self.swap_author_reader_switch, "active", - Gio.SettingsBindFlags.DEFAULT) - - self._glib_settings.bind("replay", self.replay_switch, "active", Gio.SettingsBindFlags.DEFAULT) - self._glib_settings.bind("rewind-duration", self.rewind_duration_adjustment, "value", - Gio.SettingsBindFlags.DEFAULT) - self._glib_settings.bind("forward-duration", self.forward_duration_adjustment, "value", - Gio.SettingsBindFlags.DEFAULT) - - self._glib_settings.bind("sleep-timer-fadeout", self.sleep_timer_fadeout_switch, "enable-expansion", - Gio.SettingsBindFlags.DEFAULT) - - self._glib_settings.bind("sleep-timer-fadeout-duration", self.fadeout_duration_adjustment, - "value", Gio.SettingsBindFlags.DEFAULT) - - self._glib_settings.bind("prefer-external-cover", self.artwork_prefer_external_switch, "active", - Gio.SettingsBindFlags.DEFAULT) - - def _on_lock_ui_changed(self): + def _bind_settings(self) -> None: + self._glib_settings.bind( + "dark-mode", self.dark_mode_switch, "active", Gio.SettingsBindFlags.DEFAULT + ) + + self._glib_settings.bind( + "swap-author-reader", + self.swap_author_reader_switch, + "active", + Gio.SettingsBindFlags.DEFAULT, + ) + + self._glib_settings.bind( + "replay", self.replay_switch, "active", Gio.SettingsBindFlags.DEFAULT + ) + self._glib_settings.bind( + "rewind-duration", + self.rewind_duration_adjustment, + "value", + Gio.SettingsBindFlags.DEFAULT, + ) + self._glib_settings.bind( + "forward-duration", + self.forward_duration_adjustment, + "value", + Gio.SettingsBindFlags.DEFAULT, + ) + + self._glib_settings.bind( + "sleep-timer-fadeout", + self.sleep_timer_fadeout_switch, + "enable-expansion", + Gio.SettingsBindFlags.DEFAULT, + ) + + self._glib_settings.bind( + "sleep-timer-fadeout-duration", + self.fadeout_duration_adjustment, + "value", + Gio.SettingsBindFlags.DEFAULT, + ) + + self._glib_settings.bind( + "prefer-external-cover", + self.artwork_prefer_external_switch, + "active", + Gio.SettingsBindFlags.DEFAULT, + ) + + self.storage_locations_view.set_sensitive(False) + + def _on_lock_ui_changed(self) -> None: sensitive = not self._view_model.lock_ui self.storage_locations_view.set_sensitive(sensitive) - + def _hide_window(self, *_): self.hide() return True diff --git a/cozy/ui/widgets/storages.py b/cozy/ui/widgets/storages.py index 0f6e59c0..55d5280c 100644 --- a/cozy/ui/widgets/storages.py +++ b/cozy/ui/widgets/storages.py @@ -1,13 +1,10 @@ import logging -from threading import Thread from typing import Callable -from cozy.control.filesystem_monitor import FilesystemMonitor -from cozy.model.storage import Storage +from gi.repository import Adw, Gio, GLib, GObject, Gtk + from cozy.ext import inject -from cozy.model.library import Library -from cozy.model.settings import Settings -from gi.repository import Gtk, GObject, Gio, GLib, Adw +from cozy.model.storage import Storage from cozy.view_model.storages_view_model import StoragesViewModel log = logging.getLogger("settings") @@ -29,7 +26,9 @@ def finish_callback(dialog, result): if file is not None: callback(file.get_path()) - location_chooser.select_folder(inject.instance("MainWindow").window, None, finish_callback) + location_chooser.select_folder( + inject.instance("MainWindow").window, None, finish_callback + ) @Gtk.Template.from_resource("/com/github/geigi/cozy/storage_row.ui") @@ -56,16 +55,16 @@ def __init__(self, model: Storage, menu_model: Gio.Menu) -> None: def model(self) -> Storage: return self._model - def ask_for_new_location(self, *_): + def ask_for_new_location(self, *_) -> None: ask_storage_location(self._on_folder_changed) - def _on_folder_changed(self, new_path): + def _on_folder_changed(self, new_path: str) -> None: self.emit("location-changed", new_path) - def _on_menu_opened(self, *_): + def _on_menu_opened(self, *_) -> None: self.emit("menu-opened") - def _set_drive_icon(self): + def _set_drive_icon(self) -> None: if self._model.external: self.icon.set_from_icon_name("network-server-symbolic") self.icon.set_tooltip_text(_("External drive")) @@ -73,13 +72,20 @@ def _set_drive_icon(self): self.icon.set_from_icon_name("folder-open-symbolic") self.icon.set_tooltip_text(_("Internal drive")) - def _set_default_icon(self): + def _set_default_icon(self) -> None: self.default_icon.set_visible(self._model.default) -GObject.signal_new('location-changed', StorageRow, GObject.SIGNAL_RUN_LAST, GObject.TYPE_PYOBJECT, - (GObject.TYPE_PYOBJECT,)) -GObject.signal_new('menu-opened', StorageRow, GObject.SIGNAL_RUN_LAST, GObject.TYPE_PYOBJECT, ()) +GObject.signal_new( + "location-changed", + StorageRow, + GObject.SIGNAL_RUN_LAST, + GObject.TYPE_PYOBJECT, + (GObject.TYPE_PYOBJECT,), +) +GObject.signal_new( + "menu-opened", StorageRow, GObject.SIGNAL_RUN_LAST, GObject.TYPE_PYOBJECT, () +) @Gtk.Template.from_resource("/com/github/geigi/cozy/storage_locations.ui") @@ -104,7 +110,7 @@ def __init__(self) -> None: self._reload_storage_list() - def _create_actions(self): + def _create_actions(self) -> None: self.action_group = Gio.SimpleActionGroup.new() self.insert_action_group("storage", self.action_group) @@ -126,7 +132,7 @@ def _create_actions(self): self.make_default_action.connect("activate", self._set_default_storage_location) self.action_group.add_action(self.make_default_action) - def _reload_storage_list(self): + def _reload_storage_list(self) -> None: self.storage_locations_list.remove_all() for storage in self._view_model.storages: @@ -135,27 +141,36 @@ def _reload_storage_list(self): row.connect("menu-opened", self._on_storage_menu_opened) self.storage_locations_list.append(row) - def _remove_storage_location(self, *_): + def _remove_storage_location(self, *_) -> None: self._view_model.remove(self._view_model.selected_storage) - def _set_default_storage_location(self, *_): + def _set_default_storage_location(self, *_) -> None: self._view_model.set_default(self._view_model.selected_storage) - def _mark_storage_location_external(self, action, value): + def _mark_storage_location_external( + self, action: Gio.SimpleAction, value: GObject.ParamSpec + ) -> None: value = action.get_property(value.name) self._view_model.set_external(self._view_model.selected_storage, value) - def _on_new_storage_clicked(self, *junk): + def _on_new_storage_clicked(self, *junk) -> None: ask_storage_location(self._view_model.add_storage_location) - def _on_storage_location_changed(self, widget, new_location): + def _on_storage_location_changed( + self, widget: StorageRow, new_location: str + ) -> None: self._view_model.change_storage_location(widget.model, new_location) - def _on_storage_menu_opened(self, widget: StorageRow): + def _on_storage_menu_opened(self, widget: StorageRow) -> None: with self.set_external_action.handler_block(self.set_external_signal_handler): - self.set_external_action.props.state = GLib.Variant.new_boolean(widget.model.external) + self.set_external_action.props.state = GLib.Variant.new_boolean( + widget.model.external + ) - self.remove_action.props.enabled = not widget.model.default and len(self._view_model.storages) > 1 - self.make_default_action.props.enabled = widget.model is not self._view_model.default + self.remove_action.props.enabled = ( + not widget.model.default and len(self._view_model.storages) > 1 + ) + self.make_default_action.props.enabled = ( + widget.model is not self._view_model.default + ) self._view_model.selected_storage = widget.model - diff --git a/cozy/view_model/storages_view_model.py b/cozy/view_model/storages_view_model.py index 22aeee3a..c9463fe3 100644 --- a/cozy/view_model/storages_view_model.py +++ b/cozy/view_model/storages_view_model.py @@ -2,22 +2,20 @@ from threading import Thread 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 -from cozy.model.library import Library -from cozy.model.storage import Storage from cozy.ext import inject from cozy.media.importer import Importer +from cozy.model.library import Library from cozy.model.settings import Settings -from cozy.report import reporter -from gi.repository import Gtk, Adw - - +from cozy.model.storage import Storage log = logging.getLogger("storages_view_model") + class StoragesViewModel(Observable, EventSender): _library: Library = inject.attr(Library) _importer: Importer = inject.attr(Importer) @@ -26,24 +24,24 @@ class StoragesViewModel(Observable, EventSender): _db = inject.attr(SqliteDatabase) _fs_monitor = inject.attr(FilesystemMonitor) - def __init__(self): + def __init__(self) -> None: super().__init__() super(Observable, self).__init__() self._selected_storage = None - def _scan_new_storage(self, model: Storage): + def _scan_new_storage(self, model: Storage) -> None: self.emit_event("storage-added", model) log.info("New audiobook location added. Starting import scan.") Thread(target=self._importer.scan, name="ImportThread").start() - def _rebase_storage_location(self, model: Storage, old_path: str): + def _rebase_storage_location(self, model: Storage, old_path: str) -> None: self.emit_event("storage-changed", model) log.info("Audio book location changed, rebasing the location in Cozy.") Thread( target=self._library.rebase_path, args=(old_path, model.path), - name="RebaseStorageLocationThread" + name="RebaseStorageLocationThread", ).start() def add_storage_location(self, path: str) -> None: @@ -55,7 +53,7 @@ def add_storage_location(self, path: str) -> None: self._scan_new_storage(model) - def add_first_storage_location(self, path: str): + def add_first_storage_location(self, path: str) -> None: storage = self.storages[0] storage.path = path storage.default = True @@ -100,7 +98,7 @@ def remove(self, model: Storage) -> None: self._notify("storage_locations") - def set_default(self, model: Storage): + def set_default(self, model: Storage) -> None: if model.default: return @@ -111,7 +109,7 @@ def set_default(self, model: Storage): self._notify("storage_attributes") - def set_external(self, model: Storage, external: bool): + def set_external(self, model: Storage, external: bool) -> None: model.external = external if external: