From 81b6e7a212769d4bd1316096f6e61cc741fc34d1 Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Sun, 24 Feb 2019 11:43:01 -0500 Subject: [PATCH] handle ValueError Exception via Gnome import; refs #75 --- apprise/plugins/NotifyGnome.py | 5 ++++- test/test_glib_plugin.py | 31 +++++++++++++++++++++++++++++++ test/test_gnome_plugin.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/apprise/plugins/NotifyGnome.py b/apprise/plugins/NotifyGnome.py index c1f1cd5b..e274dd15 100644 --- a/apprise/plugins/NotifyGnome.py +++ b/apprise/plugins/NotifyGnome.py @@ -47,10 +47,13 @@ try: # We're good to go! NOTIFY_GNOME_SUPPORT_ENABLED = True -except ImportError: +except (ImportError, ValueError): # No problem; we just simply can't support this plugin; we could # be in microsoft windows, or we just don't have the python-gobject # library available to us (or maybe one we don't support)? + + # Alternativey A ValueError will get thrown upon calling + # gi.require_version() if the requested Notify namespace isn't available pass diff --git a/test/test_glib_plugin.py b/test/test_glib_plugin.py index fbe1416a..03c22925 100644 --- a/test/test_glib_plugin.py +++ b/test/test_glib_plugin.py @@ -260,3 +260,34 @@ def test_dbus_plugin(mock_mainloop, mock_byte, mock_bytearray, # Our notification succeeds even though the gi library was not loaded assert(obj.notify(title='title', body='body', notify_type=apprise.NotifyType.INFO) is True) + + # Force a global import error + _session_bus = sys.modules['dbus'] + sys.modules['dbus'] = compile('raise ImportError()', 'dbus', 'exec') + + # Reload our modules + reload(sys.modules['apprise.plugins.NotifyDBus']) + reload(sys.modules['apprise.plugins']) + reload(sys.modules['apprise.Apprise']) + reload(sys.modules['apprise']) + + # Create our instance + obj = apprise.Apprise.instantiate('glib://', suppress_exceptions=False) + assert(isinstance(obj, apprise.plugins.NotifyDBus) is True) + obj.duration = 0 + + # Test url() call + assert(compat_is_basestring(obj.url()) is True) + + # Our notification fail because the dbus library wasn't present + assert(obj.notify(title='title', body='body', + notify_type=apprise.NotifyType.INFO) is False) + + # Since playing with the sys.modules is not such a good idea, + # let's just put it back now :) + sys.modules['dbus'] = _session_bus + # Reload our modules + reload(sys.modules['apprise.plugins.NotifyDBus']) + reload(sys.modules['apprise.plugins']) + reload(sys.modules['apprise.Apprise']) + reload(sys.modules['apprise']) diff --git a/test/test_gnome_plugin.py b/test/test_gnome_plugin.py index a584f00a..24f7b053 100644 --- a/test/test_gnome_plugin.py +++ b/test/test_gnome_plugin.py @@ -153,3 +153,31 @@ def test_gnome_plugin(): # Test the setting of a the urgency apprise.plugins.NotifyGnome(urgency=0) + + # Verify this all works in the event a ValueError is also thronw + # out of the call to gi.require_version() + + # Emulate require_version function: + gi.require_version.side_effect = ValueError() + + # The following libraries need to be reloaded to prevent + # TypeError: super(type, obj): obj must be an instance or subtype of type + # This is better explained in this StackOverflow post: + # https://stackoverflow.com/questions/31363311/\ + # any-way-to-manually-fix-operation-of-\ + # super-after-ipython-reload-avoiding-ty + # + reload(sys.modules['apprise.plugins.NotifyGnome']) + reload(sys.modules['apprise.plugins']) + reload(sys.modules['apprise.Apprise']) + reload(sys.modules['apprise']) + + # Create our instance + obj = apprise.Apprise.instantiate('gnome://', suppress_exceptions=False) + assert(isinstance(obj, apprise.plugins.NotifyGnome) is True) + obj.duration = 0 + + # Our notifications can not work without our gi library having been + # loaded. + assert(obj.notify(title='title', body='body', + notify_type=apprise.NotifyType.INFO) is False)