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
This commit is contained in:
Peter F. Patel-Schneider 2024-02-10 22:08:29 -05:00
parent ad6e3dc80e
commit 17e6463950
9 changed files with 106 additions and 85 deletions

View File

@ -163,8 +163,9 @@ def main():
try: try:
import solaar.listener as listener import solaar.listener as listener
import solaar.ui as ui 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 import solaar.upower as _upower
if args.restart_on_wake_up: if args.restart_on_wake_up:

View File

@ -18,15 +18,19 @@
import logging import logging
import gi
import yaml as _yaml 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 . import diversion_rules, notify, tray, window
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
gi.require_version('Gtk', '3.0')
logger = logging.getLogger(__name__) 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): def _startup(app, startup_hook, use_tray, show_window):
if logger.isEnabledFor(logging.DEBUG): if logger.isEnabledFor(logging.DEBUG):
logger.debug('startup registered=%s, remote=%s', app.get_is_registered(), app.get_is_remote()) 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 global _task_runner
_task_runner = _TaskRunner('AsyncUI') _task_runner = _TaskRunner('AsyncUI')
_task_runner.start() _task_runner.start()
@ -133,8 +79,6 @@ def _command_line(app, command_line):
elif args[0] == 'config': # config call from remote instance elif args[0] == 'config': # config call from remote instance
if logger.isEnabledFor(logging.INFO): if logger.isEnabledFor(logging.INFO):
logger.info('remote command line %s', args) 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]) dev = find_device(args[1])
if dev: if dev:
setting = next((s for s in dev.settings if s.name == args[2]), None) 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): def run_loop(startup_hook, shutdown_hook, use_tray, show_window):
assert use_tray or show_window, 'need either tray or visible 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' APP_ID = 'io.github.pwr_solaar.solaar'
application = Gtk.Application.new(APP_ID, Gio.ApplicationFlags.HANDLES_COMMAND_LINE) application = Gtk.Application.new(APP_ID, Gio.ApplicationFlags.HANDLES_COMMAND_LINE)

View File

@ -17,6 +17,8 @@
## with this program; if not, write to the Free Software Foundation, Inc., ## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import logging
from gi.repository import Gtk from gi.repository import Gtk
from solaar import NAME, __version__ from solaar import NAME, __version__
from solaar.i18n import _ from solaar.i18n import _
@ -34,7 +36,7 @@ def _create():
about.set_program_name(NAME) about.set_program_name(NAME)
about.set_version(__version__) about.set_version(__version__)
about.set_comments(_('Manages Logitech receivers,\nkeyboards, mice, and tablets.')) 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_copyright('© 2012-2023 Daniel Pavel and contributors to the Solaar project')
about.set_license_type(Gtk.License.GPL_2_0) about.set_license_type(Gtk.License.GPL_2_0)
@ -58,11 +60,9 @@ def _create():
) )
except TypeError: except TypeError:
# gtk3 < ~3.6.4 has incorrect gi bindings # gtk3 < ~3.6.4 has incorrect gi bindings
import logging
logging.exception('failed to fully create the about dialog') logging.exception('failed to fully create the about dialog')
except Exception: except Exception:
# the Gtk3 version may be too old, and the function does not exist # the Gtk3 version may be too old, and the function does not exist
import logging
logging.exception('failed to fully create the about dialog') logging.exception('failed to fully create the about dialog')
about.set_translator_credits( about.set_translator_credits(

View File

@ -19,8 +19,8 @@
from gi.repository import Gdk, Gtk from gi.repository import Gdk, Gtk
from solaar.i18n import _ from solaar.i18n import _
from ..ui import error_dialog
from . import pair_window from . import pair_window
from .common import error_dialog
# import logging # import logging
# logger = logging.getLogger(__name__) # logger = logging.getLogger(__name__)

74
lib/solaar/ui/common.py Normal file
View File

@ -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)

View File

@ -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 KIND as _SETTING_KIND
from logitech_receiver.settings import SENSITIVITY_IGNORE as _SENSITIVITY_IGNORE from logitech_receiver.settings import SENSITIVITY_IGNORE as _SENSITIVITY_IGNORE
from solaar.i18n import _, ngettext 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__) logger = logging.getLogger(__name__)

View File

@ -18,8 +18,15 @@
# Optional desktop notifications. # Optional desktop notifications.
import logging
from solaar import NAME
from solaar.i18n import _ from solaar.i18n import _
from . import icons as _icons
logger = logging.getLogger(__name__)
# #
# #
# #
@ -37,12 +44,6 @@ except (ValueError, ImportError):
available = False available = False
if available: 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 # cache references to shown notifications here, so if another status comes
# while its notification is still visible we don't create another one # while its notification is still visible we don't create another one

View File

@ -31,6 +31,7 @@ from solaar.i18n import _
from . import icons as _icons from . import icons as _icons
from .about import show_window as _show_about_window 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 popup as _window_popup
from .window import toggle as _window_toggle from .window import toggle as _window_toggle
@ -58,10 +59,8 @@ def _create_menu(quit_handler):
menu.append(no_receiver) menu.append(no_receiver)
menu.append(Gtk.SeparatorMenuItem.new()) 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('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.append(make('application-exit', _('Quit %s') % NAME, quit_handler, stock_id='application-exit').create_menu_item())
del make
menu.show_all() menu.show_all()

View File

@ -18,6 +18,8 @@
import logging import logging
import gi
from gi.repository import Gdk, GLib, Gtk from gi.repository import Gdk, GLib, Gtk
from gi.repository.GObject import TYPE_PYOBJECT from gi.repository.GObject import TYPE_PYOBJECT
from logitech_receiver import hidpp10 as _hidpp10 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 logitech_receiver.status import KEYS as _K
from solaar import NAME from solaar import NAME
from solaar.i18n import _, ngettext 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 action as _action
from . import config_panel as _config_panel from . import config_panel as _config_panel
from . import icons as _icons from . import icons as _icons
from .about import show_window as _show_about_window 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 .diversion_rules import show_window as _show_diversion_window
# from solaar import __version__ as VERSION
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# #
@ -47,7 +50,6 @@ _TREE_ICON_SIZE = Gtk.IconSize.BUTTON
_INFO_ICON_SIZE = Gtk.IconSize.LARGE_TOOLBAR _INFO_ICON_SIZE = Gtk.IconSize.LARGE_TOOLBAR
_DEVICE_ICON_SIZE = Gtk.IconSize.DND _DEVICE_ICON_SIZE = Gtk.IconSize.DND
try: try:
import gi
gi.check_version('3.7.4') gi.check_version('3.7.4')
_CAN_SET_ROW_NONE = None _CAN_SET_ROW_NONE = None
except (ValueError, AttributeError): except (ValueError, AttributeError):