From e1eee2e078ae986bb2e0947ce5a0ddca888a23ba Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Mon, 14 Mar 2016 11:33:05 +0100 Subject: [PATCH] Fix "Gtk was imported without specifying a version first" Fixes the following warnings: sys:1: PyGIWarning: Gtk was imported without specifying a version first. Use gi.require_version('Gtk', '3.0') before import to ensure that the right version gets loaded. sys:1: PyGIWarning: Notify was imported without specifying a version first. Use gi.require_version('Notify', '0.7') before import to ensure that the right version gets loaded. --- lib/solaar/gtk.py | 7 +++++-- lib/solaar/ui/notify.py | 14 +++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/solaar/gtk.py b/lib/solaar/gtk.py index 16c8549f..f7284205 100644 --- a/lib/solaar/gtk.py +++ b/lib/solaar/gtk.py @@ -20,6 +20,8 @@ from __future__ import absolute_import, division, print_function, unicode_literals +import importlib + from solaar import __version__, NAME import solaar.i18n as _i18n @@ -31,7 +33,7 @@ import solaar.cli as _cli def _require(module, os_package): try: - __import__(module) + return importlib.import_module(module) except ImportError: import sys sys.exit("%s: missing required package '%s'" % (NAME, os_package)) @@ -85,7 +87,8 @@ def main(): if not args: return if args.action: return _cli.run(args.action, args.hidraw_path) - _require('gi.repository', 'python-gi') + gi = _require('gi', 'python-gi') + gi.require_version('Gtk', '3.0') _require('gi.repository.Gtk', 'gir1.2-gtk-3.0') try: diff --git a/lib/solaar/ui/notify.py b/lib/solaar/ui/notify.py index 00016770..3bca1049 100644 --- a/lib/solaar/ui/notify.py +++ b/lib/solaar/ui/notify.py @@ -29,9 +29,17 @@ from solaar.i18n import _ # try: + import gi + gi.require_version('Notify', '0.7') # this import is allowed to fail, in which case the entire feature is unavailable from gi.repository import Notify + # assumed to be working since the import succeeded + available = True +except (ValueError, ImportError): + available = False + +if available: from logging import getLogger, INFO as _INFO _log = getLogger(__name__) del getLogger @@ -39,9 +47,6 @@ try: from solaar import NAME from . import icons as _icons - # assumed to be working since the import succeeded - available = True - # cache references to shown notifications here, so if another status comes # while its notification is still visible we don't create another one _notifications = {} @@ -137,8 +142,7 @@ try: except Exception: _log.exception("showing %s", n) -except ImportError: - available = False +else: init = lambda: False uninit = lambda: None # toggle = lambda action: False