[Sugar-devel] [PATCH sugar v2] Prevent view source display of some binary and "junk" files (SL#2854)

Sascha Silbe silbe at activitycentral.com
Mon Aug 1 14:34:50 EDT 2011


From: Walter Bender <walter.bender at gmail.com>

This patch addresses the issue raised in SL#2854: the exclusion of some file
types from being displayed by viewsource. This version of the patch uses a
global, _EXCLUDE, that includes a list of many more common file types to
exclude, as per some mining of .gitignore files by Sascha. In a future patch,
it may make sense to append the data from the .gitignore and git exclude files.

[added exclude list for (exact-match) file names, tweaked description]
Signed-off-by: Sascha Silbe <silbe at activitycentral.com>
---
v1->v2: split excludes into patterns and exact matches, tweaked patch
        description

 src/jarabe/view/viewsource.py |   27 +++++++++++++++++----------
 1 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/src/jarabe/view/viewsource.py b/src/jarabe/view/viewsource.py
index 1f89b75..648e740 100644
--- a/src/jarabe/view/viewsource.py
+++ b/src/jarabe/view/viewsource.py
@@ -1,5 +1,6 @@
 # Copyright (C) 2008 One Laptop Per Child
 # Copyright (C) 2009 Tomeu Vizoso, Simon Schampijer
+# Copyright (C) 2011 Walter Bender
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -37,6 +38,10 @@ from sugar.datastore import datastore
 from sugar import mime


+_EXCLUDE_EXTENSIONS = ('.pyc', '.pyo', '.so', '.o', '.a', '.la', '.mo', '~',
+                       '.xo', '.tar', '.bz2', '.zip', '.gz')
+_EXCLUDE_NAMES = ['.deps', '.libs']
+
 _SOURCE_FONT = pango.FontDescription('Monospace %d' % style.FONT_SIZE)

 _logger = logging.getLogger('ViewSource')
@@ -388,16 +393,18 @@ class FileViewer(gtk.ScrolledWindow):
     def _add_dir_to_model(self, dir_path, parent=None):
         model = self._tree_view.get_model()
         for f in os.listdir(dir_path):
-            if not f.endswith('.pyc'):
-                full_path = os.path.join(dir_path, f)
-                if os.path.isdir(full_path):
-                    new_iter = model.append(parent, [f, full_path])
-                    self._add_dir_to_model(full_path, new_iter)
-                else:
-                    current_iter = model.append(parent, [f, full_path])
-                    if f == self._initial_filename:
-                        selection = self._tree_view.get_selection()
-                        selection.select_iter(current_iter)
+            if f.endswith(_EXCLUDE_EXTENSIONS) or f in _EXCLUDE_NAMES:
+                continue
+
+            full_path = os.path.join(dir_path, f)
+            if os.path.isdir(full_path):
+                new_iter = model.append(parent, [f, full_path])
+                self._add_dir_to_model(full_path, new_iter)
+            else:
+                current_iter = model.append(parent, [f, full_path])
+                if f == self._initial_filename:
+                    selection = self._tree_view.get_selection()
+                    selection.select_iter(current_iter)

     def __selection_changed_cb(self, selection):
         model, tree_iter = selection.get_selected()
--
1.7.5.4



More information about the Sugar-devel mailing list