Skip to content

Commit

Permalink
ui: improve imports in ui
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pfps committed Feb 18, 2024
1 parent ad6e3dc commit 17e6463
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 85 deletions.
3 changes: 2 additions & 1 deletion lib/solaar/gtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
75 changes: 9 additions & 66 deletions lib/solaar/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

#
Expand All @@ -42,68 +46,10 @@
#


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()
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions lib/solaar/ui/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 _
Expand All @@ -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)
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion lib/solaar/ui/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
74 changes: 74 additions & 0 deletions lib/solaar/ui/common.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 2 additions & 1 deletion lib/solaar/ui/config_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
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__)

Expand Down
13 changes: 7 additions & 6 deletions lib/solaar/ui/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

#
#
#
Expand All @@ -37,12 +44,6 @@
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
Expand Down
7 changes: 3 additions & 4 deletions lib/solaar/ui/tray.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

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

Expand Down Expand Up @@ -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()

Expand Down
8 changes: 5 additions & 3 deletions lib/solaar/ui/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,15 +28,16 @@
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__)

#
Expand All @@ -47,7 +50,6 @@
_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):
Expand Down

0 comments on commit 17e6463

Please sign in to comment.