[gnome-tweak-tool] Dont crash on missing directories



commit 819dddcf05cf5aca65b8425541893485cbdfb96c
Author: John Stowers <john stowers gmail com>
Date:   Wed Apr 27 20:10:31 2011 +1200

    Dont crash on missing directories

 NEWS                             |    2 ++
 gtweak/tweaks/tweak_interface.py |   28 ++++++++++------------------
 gtweak/tweaks/tweak_windows.py   |    9 ++++-----
 gtweak/utils.py                  |   12 ++++++++++++
 4 files changed, 28 insertions(+), 23 deletions(-)
---
diff --git a/NEWS b/NEWS
index 4ba0eff..250663a 100644
--- a/NEWS
+++ b/NEWS
@@ -1,6 +1,8 @@
 3.0.3
    * Autostart nautilus at login if it is configured to show
      desktop icons (bug 648087)
+   * Improved robustness for missing theme/icon
+     directories (bug 648735)
 
 3.0.2
    * Really tweak window theme - both the fallback and the
diff --git a/gtweak/tweaks/tweak_interface.py b/gtweak/tweaks/tweak_interface.py
index de21220..33ee362 100644
--- a/gtweak/tweaks/tweak_interface.py
+++ b/gtweak/tweaks/tweak_interface.py
@@ -20,6 +20,7 @@ import os.path
 from gi.repository import Gtk
 
 import gtweak
+from gtweak.utils import walk_directories
 from gtweak.tweakmodel import TweakGroup
 from gtweak.widgets import GSettingsSwitchTweak, GSettingsComboTweak, build_horizontal_sizegroup
 
@@ -33,14 +34,11 @@ class GtkThemeSwitcher(GSettingsComboTweak):
 
     def _get_valid_themes(self):
         """ Only shows themes that have variations for gtk+-3 and gtk+-2 """
-        valid = []
         dirs = ( os.path.join(gtweak.DATA_DIR, "themes"),
                  os.path.join(os.path.expanduser("~"), ".themes"))
-        for thdir in dirs:
-            for t in os.listdir(thdir):
-                if os.path.exists(os.path.join(thdir, t, "gtk-2.0")) and \
-                    os.path.exists(os.path.join(thdir, t, "gtk-3.0")):
-                     valid.append(t)
+        valid = walk_directories(dirs, lambda d:
+                    os.path.exists(os.path.join(d, "gtk-2.0")) and \
+                        os.path.exists(os.path.join(d, "gtk-3.0")))
         return valid
 
 class IconThemeSwitcher(GSettingsComboTweak):
@@ -52,14 +50,11 @@ class IconThemeSwitcher(GSettingsComboTweak):
             **options)
 
     def _get_valid_icon_themes(self):
-        valid = []
         dirs = ( os.path.join(gtweak.DATA_DIR, "icons"),
                  os.path.join(os.path.expanduser("~"), ".icons"))
-        for thdir in dirs:
-            for t in os.listdir(thdir):
-                if os.path.isdir(os.path.join(thdir, t)) and \
-                        not os.path.exists(os.path.join(thdir, t, "cursors")):
-                    valid.append(t)
+        valid = walk_directories(dirs, lambda d:
+                    os.path.isdir(d) and \
+                        not os.path.exists(os.path.join(d, "cursors")))
         return valid
 
 class CursorThemeSwitcher(GSettingsComboTweak):
@@ -71,14 +66,11 @@ class CursorThemeSwitcher(GSettingsComboTweak):
             **options)
 
     def _get_valid_cursor_themes(self):
-        valid = []
         dirs = ( os.path.join(gtweak.DATA_DIR, "icons"),
                  os.path.join(os.path.expanduser("~"), ".icons"))
-        for thdir in dirs:
-            for t in os.listdir(thdir):
-                if os.path.isdir(os.path.join(thdir, t)) and \
-                    os.path.exists(os.path.join(thdir, t, "cursors")):
-                     valid.append(t)
+        valid = walk_directories(dirs, lambda d:
+                    os.path.isdir(d) and \
+                        os.path.exists(os.path.join(d, "cursors")))
         return valid
 
 sg = build_horizontal_sizegroup()
diff --git a/gtweak/tweaks/tweak_windows.py b/gtweak/tweaks/tweak_windows.py
index e71b22d..70e1fb7 100644
--- a/gtweak/tweaks/tweak_windows.py
+++ b/gtweak/tweaks/tweak_windows.py
@@ -18,6 +18,7 @@
 import os.path
 
 import gtweak
+from gtweak.utils import walk_directories
 from gtweak.tweakmodel import TweakGroup
 from gtweak.widgets import GConfComboTweak, build_horizontal_sizegroup
 from gtweak.gconf import GConfSetting
@@ -45,14 +46,12 @@ class WindowThemeSwitcher(GConfComboTweak):
 
         #also need to change the fallback (metacity) window theme
         self.gconf_metacity = GConfSetting("/apps/metacity/general/theme", str)
+
     def _get_valid_themes(self):
-        valid = []
         dirs = ( os.path.join(gtweak.DATA_DIR, "themes"),
                  os.path.join(os.path.expanduser("~"), ".themes"))
-        for thdir in dirs:
-            for t in os.listdir(thdir):
-                if os.path.exists(os.path.join(thdir, t, "metacity-1")):
-                     valid.append(t)
+        valid = walk_directories(dirs, lambda d:
+                    os.path.exists(os.path.join(d, "metacity-1")))
         return valid
 
     def _on_combo_changed(self, combo):
diff --git a/gtweak/utils.py b/gtweak/utils.py
index 846aa11..d3d86ff 100644
--- a/gtweak/utils.py
+++ b/gtweak/utils.py
@@ -20,6 +20,18 @@ import logging
 
 from gi.repository import GLib
 
+def walk_directories(dirs, filter_func):
+    valid = []
+    try:
+        for thdir in dirs:
+            if os.path.isdir(thdir):
+                for t in os.listdir(thdir):
+                    if filter_func(os.path.join(thdir, t)):
+                         valid.append(t)
+    except:
+        logging.critical("Error parsing directories", exc_info=True)
+    return valid
+
 class AutostartManager:
     def __init__(self, DATA_DIR, desktop_filename, exec_cmd="", extra_exec_args=""):
         self._desktop_file = os.path.join(DATA_DIR, "applications", desktop_filename)



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