[pitivi: 87/94] Refactor methods for checking if a preset can be saved or removed



commit fa228e943a72a1a5b30ef1c9157bd582053f662c
Author: Jean-FranÃois Fortin Tam <nekohayo gmail com>
Date:   Fri Sep 9 16:23:32 2011 -0400

    Refactor methods for checking if a preset can be saved or removed
    
    Add a utility function for checking file write permissions

 pitivi/ui/preset.py |   53 ++++++++++++++++++++++++++++++--------------------
 pitivi/utils.py     |    9 ++++++++
 2 files changed, 41 insertions(+), 21 deletions(-)
---
diff --git a/pitivi/ui/preset.py b/pitivi/ui/preset.py
index 5c7b046..6ba0a2c 100644
--- a/pitivi/ui/preset.py
+++ b/pitivi/ui/preset.py
@@ -26,6 +26,7 @@ import gst
 import gtk
 
 from pitivi.settings import xdg_data_home
+from pitivi.utils import isWritable
 from pitivi.configure import get_data_dir, get_renderpresets_dir, \
         get_audiopresets_dir, get_videopresets_dir
 import json
@@ -242,32 +243,42 @@ class PresetManager(object):
         self.ordered.prepend((name, values))
 
     def isSaveButtonSensitive(self):
-        """Check if Save buttons should be sensitive"""
+        """Check if the Save button should be sensitive"""
+        if not self.cur_preset or self.cur_preset == "No preset":
+            return False
         try:
-            (dir, name) = os.path.split(self.presets[self.cur_preset]["filepath"])
-        except:
-            dir = None
-        if self.cur_preset == "No preset" or not self.cur_preset or \
-                dir == self.default_path:
-            # There is no preset selected, nothing to do.
+            full_path = self.presets[self.cur_preset]["filepath"]
+            (dir, name) = os.path.split(full_path)
+        except KeyError:
+            # This is a newly created preset that has not yet been saved
+            return True
+        if dir == self.default_path or not isWritable(full_path):
+            # default_path is the system-wide directory where the default
+            # presets are installed; they are not expected to be editable.
             return False
-
-        values = self.presets[self.cur_preset]
-        return any((values[field] != getter()
-                    for field, (setter, getter) in self.widget_map.iteritems()))
+        else:
+            values = self.presets[self.cur_preset]
+            return any((values[field] != getter()
+                for field, (setter, getter)
+                in self.widget_map.iteritems()))
 
     def isRemoveButtonSensitive(self):
         """Check if Remove buttons should be sensitive"""
-        try:
-            (dir, name) = os.path.split(self.presets[self.cur_preset]["filepath"])
-        except:
-            dir = None
-        if self.cur_preset == "No preset" or not self.cur_preset or \
-                dir == self.default_path:
-            # There is no preset selected, nothing to do.
-            return False
-        else:
-            return True
+        if not self.cur_preset or self.cur_preset == "No preset":
+            try:
+                full_path = self.presets[self.cur_preset]["filepath"]
+                (dir, name) = os.path.split(full_path)
+            except KeyError:
+                # This is a newly created preset that has not yet been saved
+                # We cannot remove it since it does not exist
+                return False
+            if dir == self.default_path or not isWritable(full_path):
+                # default_path is the system-wide directory where the default
+                # presets are installed; they are not expected to be editable.
+                return False
+            else:
+                return True
+        return False
 
 
 class VideoPresetManager(PresetManager):
diff --git a/pitivi/utils.py b/pitivi/utils.py
index 9a2254c..7767404 100644
--- a/pitivi/utils.py
+++ b/pitivi/utils.py
@@ -279,6 +279,15 @@ def filter_(caps):
 
 
 ## URI functions
+def isWritable(path):
+    """Check if the file/path is writable"""
+    try:
+        # Needs to be "rw", not "w", otherwise you'll corrupt files
+        f = open(path, "rw")
+    except:
+        return False
+    f.close()
+    return True
 
 
 def uri_is_valid(uri):



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