From 17e64639503c217241ce77412f8ccf087f7901f7 Mon Sep 17 00:00:00 2001 From: "Peter F. Patel-Schneider" Date: Sat, 10 Feb 2024 22:08:29 -0500 Subject: [PATCH] ui: improve imports in ui ui: move imports in about.py to top of file ui: move imports to top of notify.py ui: move imports to top of window.py ui: reorder imports at beginning of __init__.py ui: move imports to top of tray.py ui: move common code out of __init__.py to common.py ui: move imports to top of __init___.py --- lib/solaar/gtk.py | 3 +- lib/solaar/ui/__init__.py | 75 +++++------------------------------ lib/solaar/ui/about.py | 6 +-- lib/solaar/ui/action.py | 2 +- lib/solaar/ui/common.py | 74 ++++++++++++++++++++++++++++++++++ lib/solaar/ui/config_panel.py | 3 +- lib/solaar/ui/notify.py | 13 +++--- lib/solaar/ui/tray.py | 7 ++-- lib/solaar/ui/window.py | 8 ++-- 9 files changed, 106 insertions(+), 85 deletions(-) create mode 100644 lib/solaar/ui/common.py diff --git a/lib/solaar/gtk.py b/lib/solaar/gtk.py index 514a544e..b4e3ed85 100755 --- a/lib/solaar/gtk.py +++ b/lib/solaar/gtk.py @@ -163,8 +163,9 @@ def main(): try: import solaar.listener as listener import solaar.ui as ui + import solaar.ui.common as common - listener.setup_scanner(ui.status_changed, ui.error_dialog) + listener.setup_scanner(ui.status_changed, common.error_dialog) import solaar.upower as _upower if args.restart_on_wake_up: diff --git a/lib/solaar/ui/__init__.py b/lib/solaar/ui/__init__.py index 4ee05e05..449f78f6 100644 --- a/lib/solaar/ui/__init__.py +++ b/lib/solaar/ui/__init__.py @@ -18,15 +18,19 @@ import logging +import gi import yaml as _yaml -import gi # isort:skip +from gi.repository import Gio, GLib, Gtk +from logitech_receiver.status import ALERT +from solaar.i18n import _ +from solaar.tasks import TaskRunner as _TaskRunner +from solaar.ui.config_panel import change_setting +from solaar.ui.window import find_device -gi.require_version('Gtk', '3.0') # NOQA: E402 -from gi.repository import GLib, Gtk, Gio # NOQA: E402 # isort:skip -from logitech_receiver.status import ALERT # NOQA: E402 # isort:skip -from solaar.i18n import _ # NOQA: E402 # isort:skip +from . import diversion_rules, notify, tray, window +gi.require_version('Gtk', '3.0') logger = logging.getLogger(__name__) # @@ -42,68 +46,10 @@ GLib.threads_init() # -def _error_dialog(reason, object): - logger.error('error: %s %s', reason, object) - - if reason == 'permissions': - title = _('Permissions error') - text = ( - _('Found a Logitech receiver or device (%s), but did not have permission to open it.') % object + '\n\n' + - _("If you've just installed Solaar, try disconnecting the receiver or device and then reconnecting it.") - ) - elif reason == 'nodevice': - title = _('Cannot connect to device error') - text = ( - _('Found a Logitech receiver or device at %s, but encountered an error connecting to it.') % object + '\n\n' + - _('Try disconnecting the device and then reconnecting it or turning it off and then on.') - ) - elif reason == 'unpair': - title = _('Unpairing failed') - text = ( - _('Failed to unpair %{device} from %{receiver}.').format(device=object.name, receiver=object.receiver.name) + - '\n\n' + _('The receiver returned an error, with no further details.') - ) - else: - raise Exception("ui.error_dialog: don't know how to handle (%s, %s)", reason, object) - - assert title - assert text - - m = Gtk.MessageDialog(None, Gtk.DialogFlags.MODAL, Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, text) - m.set_title(title) - m.run() - m.destroy() - - -def error_dialog(reason, object): - assert reason is not None - GLib.idle_add(_error_dialog, reason, object) - - -# -# -# - -_task_runner = None - - -def ui_async(function, *args, **kwargs): - if _task_runner: - _task_runner(function, *args, **kwargs) - - -# -# -# - -from . import diversion_rules, notify, tray, window # isort:skip # noqa: E402 - - def _startup(app, startup_hook, use_tray, show_window): if logger.isEnabledFor(logging.DEBUG): logger.debug('startup registered=%s, remote=%s', app.get_is_registered(), app.get_is_remote()) - from solaar.tasks import TaskRunner as _TaskRunner global _task_runner _task_runner = _TaskRunner('AsyncUI') _task_runner.start() @@ -133,8 +79,6 @@ def _command_line(app, command_line): elif args[0] == 'config': # config call from remote instance if logger.isEnabledFor(logging.INFO): logger.info('remote command line %s', args) - from solaar.ui.config_panel import change_setting # prevent circular import - from solaar.ui.window import find_device # prevent circular import dev = find_device(args[1]) if dev: setting = next((s for s in dev.settings if s.name == args[2]), None) @@ -160,7 +104,6 @@ def _shutdown(app, shutdown_hook): def run_loop(startup_hook, shutdown_hook, use_tray, show_window): assert use_tray or show_window, 'need either tray or visible window' - # from gi.repository.Gio import ApplicationFlags as _ApplicationFlags APP_ID = 'io.github.pwr_solaar.solaar' application = Gtk.Application.new(APP_ID, Gio.ApplicationFlags.HANDLES_COMMAND_LINE) diff --git a/lib/solaar/ui/about.py b/lib/solaar/ui/about.py index bd91adf0..1c081bf1 100644 --- a/lib/solaar/ui/about.py +++ b/lib/solaar/ui/about.py @@ -17,6 +17,8 @@ ## with this program; if not, write to the Free Software Foundation, Inc., ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import logging + from gi.repository import Gtk from solaar import NAME, __version__ from solaar.i18n import _ @@ -34,7 +36,7 @@ def _create(): about.set_program_name(NAME) about.set_version(__version__) about.set_comments(_('Manages Logitech receivers,\nkeyboards, mice, and tablets.')) - about.set_logo_icon_name(NAME.lower()) + about.set_icon_name(NAME.lower()) about.set_copyright('© 2012-2023 Daniel Pavel and contributors to the Solaar project') about.set_license_type(Gtk.License.GPL_2_0) @@ -58,11 +60,9 @@ def _create(): ) except TypeError: # gtk3 < ~3.6.4 has incorrect gi bindings - import logging logging.exception('failed to fully create the about dialog') except Exception: # the Gtk3 version may be too old, and the function does not exist - import logging logging.exception('failed to fully create the about dialog') about.set_translator_credits( diff --git a/lib/solaar/ui/action.py b/lib/solaar/ui/action.py index 1a716434..18aad695 100644 --- a/lib/solaar/ui/action.py +++ b/lib/solaar/ui/action.py @@ -19,8 +19,8 @@ from gi.repository import Gdk, Gtk from solaar.i18n import _ -from ..ui import error_dialog from . import pair_window +from .common import error_dialog # import logging # logger = logging.getLogger(__name__) diff --git a/lib/solaar/ui/common.py b/lib/solaar/ui/common.py new file mode 100644 index 00000000..4b7f219e --- /dev/null +++ b/lib/solaar/ui/common.py @@ -0,0 +1,74 @@ +# -*- python-mode -*- + +## Copyright (C) 2012-2013 Daniel Pavel +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License along +## with this program; if not, write to the Free Software Foundation, Inc., +## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +import logging + +from gi.repository import GLib, Gtk +from solaar.i18n import _ + +logger = logging.getLogger(__name__) + + +def _error_dialog(reason, object): + logger.error('error: %s %s', reason, object) + + if reason == 'permissions': + title = _('Permissions error') + text = ( + _('Found a Logitech receiver or device (%s), but did not have permission to open it.') % object + '\n\n' + + _("If you've just installed Solaar, try disconnecting the receiver or device and then reconnecting it.") + ) + elif reason == 'nodevice': + title = _('Cannot connect to device error') + text = ( + _('Found a Logitech receiver or device at %s, but encountered an error connecting to it.') % object + '\n\n' + + _('Try disconnecting the device and then reconnecting it or turning it off and then on.') + ) + elif reason == 'unpair': + title = _('Unpairing failed') + text = ( + _('Failed to unpair %{device} from %{receiver}.').format(device=object.name, receiver=object.receiver.name) + + '\n\n' + _('The receiver returned an error, with no further details.') + ) + else: + raise Exception("ui.error_dialog: don't know how to handle (%s, %s)", reason, object) + + assert title + assert text + + m = Gtk.MessageDialog(None, Gtk.DialogFlags.MODAL, Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, text) + m.set_title(title) + m.run() + m.destroy() + + +def error_dialog(reason, object): + assert reason is not None + GLib.idle_add(_error_dialog, reason, object) + + +# +# +# + +task_runner = None + + +def ui_async(function, *args, **kwargs): + if task_runner: + task_runner(function, *args, **kwargs) diff --git a/lib/solaar/ui/config_panel.py b/lib/solaar/ui/config_panel.py index 6b47b4ea..49fa4e81 100644 --- a/lib/solaar/ui/config_panel.py +++ b/lib/solaar/ui/config_panel.py @@ -26,7 +26,8 @@ from logitech_receiver.hidpp20 import LEDEffectSetting as _LEDEffectSetting from logitech_receiver.settings import KIND as _SETTING_KIND from logitech_receiver.settings import SENSITIVITY_IGNORE as _SENSITIVITY_IGNORE from solaar.i18n import _, ngettext -from solaar.ui import ui_async as _ui_async + +from .common import ui_async as _ui_async logger = logging.getLogger(__name__) diff --git a/lib/solaar/ui/notify.py b/lib/solaar/ui/notify.py index 6db0e294..c858a45c 100644 --- a/lib/solaar/ui/notify.py +++ b/lib/solaar/ui/notify.py @@ -18,8 +18,15 @@ # Optional desktop notifications. +import logging + +from solaar import NAME from solaar.i18n import _ +from . import icons as _icons + +logger = logging.getLogger(__name__) + # # # @@ -37,12 +44,6 @@ except (ValueError, ImportError): available = False if available: - import logging - logger = logging.getLogger(__name__) - - from solaar import NAME - - from . import icons as _icons # cache references to shown notifications here, so if another status comes # while its notification is still visible we don't create another one diff --git a/lib/solaar/ui/tray.py b/lib/solaar/ui/tray.py index 5d9bf1d5..c6159434 100644 --- a/lib/solaar/ui/tray.py +++ b/lib/solaar/ui/tray.py @@ -31,6 +31,7 @@ from solaar.i18n import _ from . import icons as _icons from .about import show_window as _show_about_window +from .action import make as _make from .window import popup as _window_popup from .window import toggle as _window_toggle @@ -58,10 +59,8 @@ def _create_menu(quit_handler): menu.append(no_receiver) menu.append(Gtk.SeparatorMenuItem.new()) - from .action import make - menu.append(make('help-about', _('About %s') % NAME, _show_about_window, stock_id='help-about').create_menu_item()) - menu.append(make('application-exit', _('Quit %s') % NAME, quit_handler, stock_id='application-exit').create_menu_item()) - del make + menu.append(_make('help-about', _('About %s') % NAME, _show_about_window, stock_id='help-about').create_menu_item()) + menu.append(_make('application-exit', _('Quit %s') % NAME, quit_handler, stock_id='application-exit').create_menu_item()) menu.show_all() diff --git a/lib/solaar/ui/window.py b/lib/solaar/ui/window.py index 39c02d08..99800cc0 100644 --- a/lib/solaar/ui/window.py +++ b/lib/solaar/ui/window.py @@ -18,6 +18,8 @@ import logging +import gi + from gi.repository import Gdk, GLib, Gtk from gi.repository.GObject import TYPE_PYOBJECT from logitech_receiver import hidpp10 as _hidpp10 @@ -26,15 +28,16 @@ from logitech_receiver.common import NamedInts as _NamedInts from logitech_receiver.status import KEYS as _K from solaar import NAME from solaar.i18n import _, ngettext -# from solaar import __version__ as VERSION -from solaar.ui import ui_async as _ui_async from . import action as _action from . import config_panel as _config_panel from . import icons as _icons from .about import show_window as _show_about_window +from .common import ui_async as _ui_async from .diversion_rules import show_window as _show_diversion_window +# from solaar import __version__ as VERSION + logger = logging.getLogger(__name__) # @@ -47,7 +50,6 @@ _TREE_ICON_SIZE = Gtk.IconSize.BUTTON _INFO_ICON_SIZE = Gtk.IconSize.LARGE_TOOLBAR _DEVICE_ICON_SIZE = Gtk.IconSize.DND try: - import gi gi.check_version('3.7.4') _CAN_SET_ROW_NONE = None except (ValueError, AttributeError):