[pitivi] preset: Don't hide exceptions when loading



commit 6686d32afac9e2a1d4f8dac868e6bd6dc106243e
Author: Alexandru Băluț <alexandru balut gmail com>
Date:   Wed May 20 23:56:58 2015 +0200

    preset: Don't hide exceptions when loading
    
    Differential Revision: https://phabricator.freedesktop.org/D359
    Reviewed-by: Thibault Saunier <tsaunier gnome org>

 pitivi/preset.py     |   29 +++++++++++++++--------------
 tests/test_preset.py |   10 +++++++++-
 2 files changed, 24 insertions(+), 15 deletions(-)
---
diff --git a/pitivi/preset.py b/pitivi/preset.py
index 718472d..29d54bc 100644
--- a/pitivi/preset.py
+++ b/pitivi/preset.py
@@ -31,6 +31,7 @@ from pitivi.settings import xdg_data_home
 from pitivi.utils.misc import isWritable
 from pitivi.configure import get_renderpresets_dir, get_audiopresets_dir, get_videopresets_dir
 from pitivi.utils import system
+from pitivi.utils.loggable import Loggable
 
 
 # Presets created with this name by the app should have "volatile": True
@@ -44,7 +45,7 @@ class DuplicatePresetNameException(Exception):
     pass
 
 
-class PresetManager(object):
+class PresetManager(Loggable):
 
     """Abstract class for storing a list of presets.
 
@@ -66,6 +67,8 @@ class PresetManager(object):
     """
 
     def __init__(self, default_path, user_path):
+        Loggable.__init__(self)
+
         self.default_path = default_path
         self.user_path = user_path
 
@@ -78,21 +81,19 @@ class PresetManager(object):
         self.system = system.getSystem()
 
     def loadAll(self):
-        filepaths = []
-        try:
-            for uri in os.listdir(self.default_path):
-                filepaths.append(os.path.join(self.default_path, uri))
-        except Exception:
-            pass
-        try:
-            for uri in os.listdir(self.user_path):
-                filepaths.append(os.path.join(self.user_path, uri))
-        except Exception:
-            pass
+        self._loadFromDir(self.default_path)
+        self._loadFromDir(self.user_path)
 
-        for filepath in filepaths:
+    def _loadFromDir(self, presets_dir):
+        try:
+            files = os.listdir(presets_dir)
+        except FileNotFoundError:
+            self.debug("Presets directory missing: %s", presets_dir)
+            return
+        for uri in files:
+            filepath = os.path.join(presets_dir, uri)
             if filepath.endswith("json"):
-                self._loadSection(os.path.join(self.default_path, filepath))
+                self._loadSection(filepath)
 
     def saveAll(self):
         """Write changes to disk for all presets"""
diff --git a/tests/test_preset.py b/tests/test_preset.py
index e41a6d6..727b69b 100644
--- a/tests/test_preset.py
+++ b/tests/test_preset.py
@@ -43,7 +43,10 @@ class FakePresetManager(PresetManager):
 
 
 def clearPresetManagerPaths(preset_manager):
-    shutil.rmtree(preset_manager.user_path)
+    try:
+        shutil.rmtree(preset_manager.user_path)
+    except FileNotFoundError:
+        pass
 
 
 def countJsonFilesIn(dir_path):
@@ -117,6 +120,11 @@ class TestPresetBasics(TestCase):
         self.assertRaises(DuplicatePresetNameException,
                           self.manager.renamePreset, '0', 'Preset two')
 
+    def testLoadHandlesMissingDirectory(self):
+        self.manager.default_path = '/pitivi/non/existing/directory/1'
+        self.manager.user_path = '/pitivi/non/existing/directory/2'
+        self.manager.loadAll()
+
 
 class TestAudioPresetsIO(TestCase):
 


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