Daniel Brötzmann pushed to branch master at gajim / gajim
Commits: a5f1222b by wurstsalat at 2022-01-20T22:39:34+01:00 Const: Sort window modules, typing
- - - - - ad699686 by wurstsalat at 2022-01-20T22:39:34+01:00 FileChoosers: Typing
- - - - -
2 changed files:
- gajim/gtk/const.py - gajim/gtk/filechoosers.py
Changes:
===================================== gajim/gtk/const.py ===================================== @@ -20,15 +20,19 @@ from typing import Callable from typing import NamedTuple from typing import Optional +from typing import Union
-from collections import namedtuple from enum import Enum from enum import IntEnum from enum import unique
from gajim.common.setting_values import AllSettingsT
-Filter = namedtuple('Filter', 'name pattern default') + +class Filter(NamedTuple): + name: str + pattern: Union[str, list[str]] + default: bool
class Setting(NamedTuple): @@ -128,40 +132,40 @@ def __str__(self):
WINDOW_MODULES = { 'AccountsWindow': 'gajim.gui.accounts', - 'HistorySyncAssistant': 'gajim.gui.history_sync', - 'ServerInfo': 'gajim.gui.server_info', - 'MamPreferences': 'gajim.gui.mam_preferences', - 'Preferences': 'gajim.gui.preferences', - 'CreateGroupchatWindow': 'gajim.gui.groupchat_creation', - 'StartChatDialog': 'gajim.gui.start_chat', + 'AccountWizard': 'gajim.gui.account_wizard', 'AddContact': 'gajim.gui.add_contact', - 'SingleMessageWindow': 'gajim.gui.single_message', + 'AdhocMUC': 'gajim.gui.adhoc_muc', + 'AdvancedConfig': 'gajim.gui.advanced_config', + 'BlockingList': 'gajim.gui.blocking', 'Bookmarks': 'gajim.gui.bookmarks', - 'AccountWizard': 'gajim.gui.account_wizard', + 'CertificateDialog': 'gajim.gui.certificate_dialog', + 'ChangePassword': 'gajim.gui.change_password', + 'ContactInfo': 'gajim.gui.contact_info', + 'CreateGroupchatWindow': 'gajim.gui.groupchat_creation', + 'Features': 'gajim.gui.features', + 'GroupchatConfig': 'gajim.gui.groupchat_config', + 'GroupChatInvitation': 'gajim.gui.groupchat_invitation', + 'GroupchatJoin': 'gajim.gui.groupchat_join', + 'HistoryExport': 'gajim.gui.history_export', + 'HistorySyncAssistant': 'gajim.gui.history_sync', + 'MamPreferences': 'gajim.gui.mam_preferences', 'ManageProxies': 'gajim.gui.proxies', 'ManageSounds': 'gajim.gui.manage_sounds', - 'ServiceDiscoveryWindow': 'gajim.gui.discovery', - 'BlockingList': 'gajim.gui.blocking', - 'XMLConsoleWindow': 'gajim.gui.xml_console', - 'GroupchatJoin': 'gajim.gui.groupchat_join', + 'PasswordDialog': 'gajim.gui.password_dialog', 'PEPConfig': 'gajim.gui.pep_config', - 'GroupchatConfig': 'gajim.gui.groupchat_config', + 'PluginsWindow': 'gajim.plugins.gui', + 'Preferences': 'gajim.gui.preferences', 'ProfileWindow': 'gajim.gui.profile', - 'SSLErrorDialog': 'gajim.gui.ssl_error_dialog', - 'Themes': 'gajim.gui.themes', - 'AdvancedConfig': 'gajim.gui.advanced_config', - 'CertificateDialog': 'gajim.gui.certificate_dialog', 'RemoveAccount': 'gajim.gui.remove_account', - 'ChangePassword': 'gajim.gui.change_password', - 'PluginsWindow': 'gajim.plugins.gui', - 'Features': 'gajim.gui.features', - 'GroupChatInvitation': 'gajim.gui.groupchat_invitation', - 'ContactInfo': 'gajim.gui.contact_info', - 'WorkspaceDialog': 'gajim.gui.workspace_dialog', 'RosterItemExchange': 'gajim.gui.roster_item_exchange', + 'ServerInfo': 'gajim.gui.server_info', + 'ServiceDiscoveryWindow': 'gajim.gui.discovery', 'ServiceRegistration': 'gajim.gui.service_registration', + 'SingleMessageWindow': 'gajim.gui.single_message', + 'SSLErrorDialog': 'gajim.gui.ssl_error_dialog', + 'StartChatDialog': 'gajim.gui.start_chat', 'SynchronizeAccounts': 'gajim.gui.synchronize_accounts', - 'AdhocMUC': 'gajim.gui.adhoc_muc', - 'PasswordDialog': 'gajim.gui.password_dialog', - 'HistoryExport': 'gajim.gui.history_export', + 'Themes': 'gajim.gui.themes', + 'WorkspaceDialog': 'gajim.gui.workspace_dialog', + 'XMLConsoleWindow': 'gajim.gui.xml_console', }
===================================== gajim/gtk/filechoosers.py ===================================== @@ -15,7 +15,13 @@ # You should have received a copy of the GNU General Public License # along with Gajim. If not, see http://www.gnu.org/licenses/.
-from typing import List +from __future__ import annotations + +from typing import cast +from typing import Any +from typing import Callable +from typing import Optional +from typing import Union
import os import sys @@ -42,7 +48,12 @@ def _require_native() -> bool: # Notes: Adding mime types to Gtk.FileFilter forces non-native dialogs
class BaseFileChooser: - def _on_response(self, dialog, response, accept_cb, cancel_cb): + def _on_response(self, + dialog: Union[Gtk.FileChooser, Gtk.FileChooserNative], + response: Gtk.ResponseType, + accept_cb: Callable[..., Any], + cancel_cb: Optional[Callable[..., Any]] + ) -> None: if response == Gtk.ResponseType.ACCEPT: if self.get_select_multiple(): accept_cb(dialog.get_filenames()) @@ -54,7 +65,7 @@ def _on_response(self, dialog, response, accept_cb, cancel_cb): if cancel_cb is not None: cancel_cb()
- def _add_filters(self, filters): + def _add_filters(self, filters: list[Filter]) -> None: for filterinfo in filters: filter_ = Gtk.FileFilter() filter_.set_name(filterinfo.name) @@ -67,9 +78,9 @@ def _add_filters(self, filters): if filterinfo.default: self.set_filter(filter_)
- def _update_preview(self, filechooser): + def _update_preview(self, filechooser: Gtk.FileChooser) -> None: path_to_file = filechooser.get_preview_filename() - preview = filechooser.get_preview_widget() + preview = cast(Gtk.Image, filechooser.get_preview_widget()) if path_to_file is None or os.path.isdir(path_to_file): # nothing to preview preview.clear() @@ -80,7 +91,7 @@ def _update_preview(self, filechooser): except GLib.Error: preview.clear() return - filechooser.get_preview_widget().set_from_pixbuf(pixbuf) + preview.set_from_pixbuf(pixbuf)
class BaseFileOpenDialog: @@ -107,12 +118,18 @@ class BaseAvatarChooserDialog: class NativeFileChooserDialog(Gtk.FileChooserNative, BaseFileChooser):
_title = '' - _filters: List[Filter] = [] + _filters: list[Filter] = [] _action = Gtk.FileChooserAction.OPEN
- def __init__(self, accept_cb, cancel_cb=None, transient_for=None, - path=None, file_name=None, select_multiple=False, - modal=False): + def __init__(self, + accept_cb: Callable[..., Any], + cancel_cb: Optional[Callable[..., Any]] = None, + transient_for: Optional[Gtk.Window] = None, + path: Optional[str] = None, + file_name: Optional[str] = None, + select_multiple: bool = False, + modal: bool = False + ) -> None:
if transient_for is None: transient_for = app.app.get_active_window() @@ -159,13 +176,20 @@ class NativeAvatarChooserDialog(BaseAvatarChooserDialog, NativeFileChooserDialog class GtkFileChooserDialog(Gtk.FileChooserDialog, BaseFileChooser):
_title = '' - _filters: List[Filter] = [] + _filters: list[Filter] = [] _action = Gtk.FileChooserAction.OPEN _preivew_size = (200, 200)
- def __init__(self, accept_cb, cancel_cb=None, transient_for=None, - path=None, file_name=None, select_multiple=False, - preview=True, modal=False): + def __init__(self, + accept_cb: Callable[..., Any], + cancel_cb: Optional[Callable[..., Any]] = None, + transient_for: Optional[Gtk.Window] = None, + path: Optional[str] = None, + file_name: Optional[str] = None, + select_multiple: bool = False, + preview: bool = True, + modal: bool = False + ) -> None:
if transient_for is None: transient_for = app.app.get_active_window() @@ -196,7 +220,7 @@ def __init__(self, accept_cb, cancel_cb=None, transient_for=None, self.connect('response', self._on_response, accept_cb, cancel_cb) self.show()
- def _on_response(self, *args): + def _on_response(self, *args: Any) -> None: super()._on_response(*args) self.destroy()
@@ -209,13 +233,18 @@ class GtkAvatarChooserDialog(BaseAvatarChooserDialog, GtkFileChooserDialog): pass
-def FileChooserDialog(*args, **kwargs): +def FileChooserDialog(*args: Any, + **kwargs: Any + ) -> Union[NativeFileOpenDialog, GtkFileOpenDialog]: if _require_native(): return NativeFileOpenDialog(*args, **kwargs) return GtkFileOpenDialog(*args, **kwargs)
-def AvatarChooserDialog(*args, **kwargs): +def AvatarChooserDialog(*args: Any, + **kwargs: Any + ) -> Union[NativeAvatarChooserDialog, + GtkAvatarChooserDialog]: if _require_native(): return NativeAvatarChooserDialog(*args, **kwargs) return GtkAvatarChooserDialog(*args, **kwargs)
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/compare/5d5ad12c31f991c227e42645fad7681b...