solaar: Add type hints

This commit is contained in:
MattHag 2024-09-15 19:11:37 +02:00 committed by Peter F. Patel-Schneider
parent 0481950324
commit 128ec43d70
2 changed files with 30 additions and 5 deletions

View File

@ -14,9 +14,12 @@
## You should have received a copy of the GNU General Public License along ## 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., ## 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.
from __future__ import annotations
import logging import logging
from typing import Callable
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
try: try:
@ -49,14 +52,22 @@ _LOGIND_PATH = "/org/freedesktop/login1"
_LOGIND_INTERFACE = "org.freedesktop.login1.Manager" _LOGIND_INTERFACE = "org.freedesktop.login1.Manager"
def watch_suspend_resume(on_resume_callback=None, on_suspend_callback=None): def watch_suspend_resume(
on_resume_callback: Callable[[], None] | None = None,
on_suspend_callback: Callable[[], None] | None = None,
):
"""Register callback for suspend/resume events. """Register callback for suspend/resume events.
They are called only if the system DBus is running, and the Login daemon is available.""" They are called only if the system DBus is running, and the Login daemon is available."""
global _resume_callback, _suspend_callback global _resume_callback, _suspend_callback
_suspend_callback = on_suspend_callback _suspend_callback = on_suspend_callback
_resume_callback = on_resume_callback _resume_callback = on_resume_callback
if bus is not None and on_resume_callback is not None or on_suspend_callback is not None: if bus is not None and on_resume_callback is not None or on_suspend_callback is not None:
bus.add_signal_receiver(_suspend_or_resume, "PrepareForSleep", dbus_interface=_LOGIND_INTERFACE, path=_LOGIND_PATH) bus.add_signal_receiver(
_suspend_or_resume,
"PrepareForSleep",
dbus_interface=_LOGIND_INTERFACE,
path=_LOGIND_PATH,
)
if logger.isEnabledFor(logging.INFO): if logger.isEnabledFor(logging.INFO):
logger.info("connected to system dbus, watching for suspend/resume events") logger.info("connected to system dbus, watching for suspend/resume events")

View File

@ -17,6 +17,8 @@
import logging import logging
from typing import Callable
import gi import gi
import yaml import yaml
@ -43,6 +45,9 @@ logger = logging.getLogger(__name__)
assert Gtk.get_major_version() > 2, "Solaar requires Gtk 3 python bindings" assert Gtk.get_major_version() > 2, "Solaar requires Gtk 3 python bindings"
APP_ID = "io.github.pwr_solaar.solaar"
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())
@ -88,12 +93,21 @@ def _shutdown(app, shutdown_hook):
desktop_notifications.uninit() desktop_notifications.uninit()
def run_loop(startup_hook, shutdown_hook, use_tray, show_window): def run_loop(
startup_hook: Callable[[], None],
shutdown_hook: Callable[[], None],
use_tray: bool,
show_window: bool,
):
assert use_tray or show_window, "need either tray or visible window" assert use_tray or show_window, "need either tray or visible window"
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)
application.connect("startup", lambda app, startup_hook: _startup(app, startup_hook, use_tray, show_window), startup_hook) application.connect(
"startup",
lambda app, startup_hook: _startup(app, startup_hook, use_tray, show_window),
startup_hook,
)
application.connect("command-line", _command_line) application.connect("command-line", _command_line)
application.connect("activate", _activate) application.connect("activate", _activate)
application.connect("shutdown", _shutdown, shutdown_hook) application.connect("shutdown", _shutdown, shutdown_hook)