[pitivi] Use the soft dependencies objects from the check module



commit 771f6d45ce1d1cc5bfad81f536e204a51c91598b
Author: Alexandru Băluț <alexandru balut gmail com>
Date:   Thu Mar 13 22:08:04 2014 +0100

    Use the soft dependencies objects from the check module

 pitivi/check.py        |   24 +++++++++++++++-------
 pitivi/medialibrary.py |   22 ++++++++++----------
 pitivi/render.py       |   28 +++++++++++++++-----------
 pitivi/utils/system.py |   13 ++++-------
 tests/Makefile.am      |    1 +
 tests/test_check.py    |   49 ++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 98 insertions(+), 39 deletions(-)
---
diff --git a/pitivi/check.py b/pitivi/check.py
index 3e45326..9384057 100644
--- a/pitivi/check.py
+++ b/pitivi/check.py
@@ -88,6 +88,9 @@ class Dependency(object):
         """
         raise NotImplementedError
 
+    def __nonzero__(self):
+        return self.satisfied
+
     def __repr__(self):
         if self.satisfied:
             return ""
@@ -177,14 +180,19 @@ HARD_DEPENDENCIES = (CairoDependency("1.10.0"),
                      GIDependency("Gio", None),
                      GstPluginDependency("gnonlin", "1.1.90"))
 
-SOFT_DEPENDENCIES = (ClassicDependency("pycanberra", None,
-                                       _("enables sound notifications when rendering is complete")),
-                     GIDependency("GnomeDesktop", None,
-                                  _("file thumbnails provided by GNOME's thumbnailers")),
-                     GIDependency("Notify", None,
-                                  _("enables visual notifications when rendering is complete")),
-                     GstPluginDependency("libav", None,
-                                         _("additional multimedia codecs through the Libav library")))
+PYCANBERRA_SOFT_DEPENDENCY = ClassicDependency("pycanberra", None,
+                                               _("enables sound notifications when rendering is complete"))
+GNOMEDESKTOP_SOFT_DEPENDENCY = GIDependency("GnomeDesktop", None,
+                                            _("file thumbnails provided by GNOME's thumbnailers"))
+NOTIFY_SOFT_DEPENDENCY = GIDependency("Notify", None,
+                                      _("enables visual notifications when rendering is complete"))
+LIBAV_SOFT_DEPENDENCY = GstPluginDependency("libav", None,
+                                            _("additional multimedia codecs through the Libav library"))
+
+SOFT_DEPENDENCIES = (PYCANBERRA_SOFT_DEPENDENCY,
+                     GNOMEDESKTOP_SOFT_DEPENDENCY,
+                     NOTIFY_SOFT_DEPENDENCY,
+                     LIBAV_SOFT_DEPENDENCY)
 
 
 def _check_audiosinks():
diff --git a/pitivi/medialibrary.py b/pitivi/medialibrary.py
index dbcab83..8a67e02 100644
--- a/pitivi/medialibrary.py
+++ b/pitivi/medialibrary.py
@@ -26,11 +26,6 @@ from gi.repository import Gst
 from gi.repository import GES
 from gi.repository import Gio
 from gi.repository import GLib
-try:
-    from gi.repository import GnomeDesktop
-    has_gnome_desktop = True
-except ImportError:
-    has_gnome_desktop = False
 from gi.repository import GObject
 from gi.repository import Gtk
 from gi.repository import Gdk
@@ -47,6 +42,7 @@ from urlparse import urlparse
 from hashlib import md5
 from gi.repository.GstPbutils import DiscovererVideoInfo
 
+from pitivi.check import GNOMEDESKTOP_SOFT_DEPENDENCY
 from pitivi.configure import get_ui_dir, get_pixmap_dir
 from pitivi.settings import GlobalSettings
 from pitivi.mediafilespreviewer import PreviewWidget
@@ -300,12 +296,16 @@ class MediaLibraryWidget(Gtk.VBox, Loggable):
         self.pack_start(self.treeview_scrollwin, True, True, 0)
         self.pack_start(self._progressbar, False, True, 0)
 
-        if has_gnome_desktop:
-            # We need to instanciate the thumbnail factory on the main thread...
-            size_normal = GnomeDesktop.DesktopThumbnailSize.NORMAL
-            self.thumbnailer = GnomeDesktop.DesktopThumbnailFactory.new(size_normal)
-        else:
-            self.thumbnailer = None
+        self.thumbnailer = MediaLibraryWidget._getThumbnailer()
+
+    @staticmethod
+    def _getThumbnailer():
+        if not GNOMEDESKTOP_SOFT_DEPENDENCY:
+            return None
+        from gi.repository import GnomeDesktop
+        # We need to instanciate the thumbnail factory on the main thread...
+        size_normal = GnomeDesktop.DesktopThumbnailSize.NORMAL
+        return GnomeDesktop.DesktopThumbnailFactory.new(size_normal)
 
     @staticmethod
     def compare_basename(model, iter1, iter2, unused_user_data):
diff --git a/pitivi/render.py b/pitivi/render.py
index 078077f..fa74772 100644
--- a/pitivi/render.py
+++ b/pitivi/render.py
@@ -25,27 +25,25 @@ Rendering-related utilities and classes
 """
 
 import os
+import time
+
 from gi.repository import GLib
 from gi.repository import Gtk
 from gi.repository import Gst
 from gi.repository import GES
-import time
 
 from gettext import gettext as _
+
 from pitivi import configure
-from pitivi.utils.signal import Signallable
 
+from pitivi.check import PYCANBERRA_SOFT_DEPENDENCY
 from pitivi.utils.loggable import Loggable
-from pitivi.utils.widgets import GstElementSettingsDialog
-from pitivi.utils.ripple_update_group import RippleUpdateGroup
 from pitivi.utils.misc import show_user_manual, path_from_uri
+from pitivi.utils.ripple_update_group import RippleUpdateGroup
+from pitivi.utils.signal import Signallable
 from pitivi.utils.ui import model, frame_rates, audio_rates,\
     audio_channels, get_combo_value, set_combo_value, beautify_ETA
-try:
-    import pycanberra
-    has_canberra = True
-except ImportError:
-    has_canberra = False
+from pitivi.utils.widgets import GstElementSettingsDialog
 
 
 class CachedEncoderList(object):
@@ -865,6 +863,14 @@ class RenderDialog(Loggable):
     def destroy(self):
         self.window.destroy()
 
+    @staticmethod
+    def _maybePlayFinishedSound():
+        if not PYCANBERRA_SOFT_DEPENDENCY:
+            return
+        import pycanberra
+        canberra = pycanberra.Canberra()
+        canberra.play(1, pycanberra.CA_PROP_EVENT_ID, "complete-media", None)
+
     #------------------- Callbacks ------------------------------------------#
 
     #-- UI callbacks
@@ -966,9 +972,7 @@ class RenderDialog(Loggable):
             if not self.progress.window.is_active():
                 notification = _('"%s" has finished rendering.' % self.fileentry.get_text())
                 self.notification = self.app.system.desktopMessage(_("Render complete"), notification, 
"pitivi")
-            if has_canberra:
-                canberra = pycanberra.Canberra()
-                canberra.play(1, pycanberra.CA_PROP_EVENT_ID, "complete-media", None)
+            self._maybePlayFinishedSound()
             self.progress.play_rendered_file_button.show()
             self.progress.close_button.show()
             self.progress.cancel_button.hide()
diff --git a/pitivi/utils/system.py b/pitivi/utils/system.py
index 1ae6c73..6e80eb9 100644
--- a/pitivi/utils/system.py
+++ b/pitivi/utils/system.py
@@ -23,16 +23,11 @@ import multiprocessing
 import os
 import resource
 
+from pitivi.check import NOTIFY_SOFT_DEPENDENCY
 from pitivi.configure import APPNAME
 from pitivi.utils.loggable import Loggable
 from pitivi.utils.signal import Signallable
 
-try:
-    from gi.repository import Notify
-    has_libnotify = True
-except ImportError:
-    has_libnotify = False
-
 
 class System(Signallable, Loggable):
     """A base class for all *Systems
@@ -173,14 +168,16 @@ class FreedesktopOrgSystem(System):
 
     def __init__(self):
         System.__init__(self)
-        if has_libnotify:
+        if NOTIFY_SOFT_DEPENDENCY:
+            from gi.repository import Notify
             Notify.init(APPNAME)
 
     def desktopMessage(self, title, message, icon="pitivi"):
         #call super method for consistent logging
         System.desktopMessage(self, title, message, icon)
 
-        if has_libnotify:
+        if NOTIFY_SOFT_DEPENDENCY:
+            from gi.repository import Notify
             notification = Notify.Notification.new(title, message, icon=icon)
             try:
                 notification.show()
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 87de577..4df70eb 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -4,6 +4,7 @@
 # Keep this list sorted!
 tests =        \
        test_application.py \
+       test_check.py \
        test_common.py \
        test_log.py \
        test_mainwindow.py \
diff --git a/tests/test_check.py b/tests/test_check.py
new file mode 100644
index 0000000..30be252
--- /dev/null
+++ b/tests/test_check.py
@@ -0,0 +1,49 @@
+# -*- coding: utf-8 -*-
+# Pitivi video editor
+#
+#       tests/test_check.py
+#
+# Copyright (c) 2014, Alex Băluț <alexandru balut gmail com>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 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
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the
+# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+# Boston, MA 02110-1301, USA.
+
+from common import TestCase
+
+from pitivi import check
+
+
+class FakeDependency(check.Dependency):
+    import_result = None
+
+    def _try_importing_component(self):
+        return self.import_result
+
+
+class TestDependency(TestCase):
+
+    def testBoolEvaluation(self):
+        dependency = FakeDependency(modulename="module1", version_required_string=None)
+        self.assertFalse(dependency)
+        self.assertFalse(dependency.satisfied)
+
+        dependency.check()
+        self.assertFalse(dependency)
+        self.assertFalse(dependency.satisfied)
+
+        dependency.import_result = "something"
+        dependency.check()
+        self.assertTrue(dependency)
+        self.assertTrue(dependency.satisfied)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]