[Dextrose] [PATCH sugar 07/11] Journal list view: don't choke on invalid or incomplete metadata (SL#1408)

Aleksey Lim alsroot at member.fsf.org
Sat Jan 15 09:10:46 EST 2011


From: Sascha Silbe <sascha-pgp at silbe.org>

Metadata can get corrupted by crashes or malformed by buggy activities.
We should do our best to display the parts that are usable and certainly
never mess up the entire Journal.

Acked-by: Simon Schampijer <simon at schampijer.de>
---
 src/jarabe/journal/listmodel.py |   51 ++++++++++++++++++++++++++++----------
 1 files changed, 37 insertions(+), 14 deletions(-)

diff --git a/src/jarabe/journal/listmodel.py b/src/jarabe/journal/listmodel.py
index c07d9e1..a22c5ea 100644
--- a/src/jarabe/journal/listmodel.py
+++ b/src/jarabe/journal/listmodel.py
@@ -142,11 +142,16 @@ class ListModel(gtk.GenericTreeModel, gtk.TreeDragSource):
             xo_color = misc.get_icon_color(metadata)
         self._cached_row.append(xo_color)
 
-        title = gobject.markup_escape_text(metadata.get('title', None))
-        self._cached_row.append('<b>%s</b>' % title)
-
-        timestamp = int(metadata.get('timestamp', 0))
-        self._cached_row.append(util.timestamp_to_elapsed_string(timestamp))
+        title = gobject.markup_escape_text(metadata.get('title',
+                                           _('Untitled')))
+        self._cached_row.append('<b>%s</b>' % (title, ))
+
+        try:
+            timestamp = float(metadata.get('timestamp', 0))
+        except (TypeError, ValueError):
+            self._cached_row.append(_('Unknown'))
+        else:
+            self._cached_row.append(util.timestamp_to_elapsed_string(timestamp))
 
         ctime = metadata.get('ctime')
         if ctime:
@@ -158,19 +163,37 @@ class ListModel(gtk.GenericTreeModel, gtk.TreeDragSource):
         size = int(metadata.get('filesize', 0))
         self._cached_row.append(util.format_size(size))
 
-        self._cached_row.append(int(metadata.get('progress', 100)))
-
-        if metadata.get('buddies', ''):
-            buddies = simplejson.loads(metadata['buddies']).values()
-        else:
+        try:
+            progress = int(float(metadata.get('progress', 100)))
+        except (TypeError, ValueError):
+            progress = 100
+        self._cached_row.append(progress)
+
+        buddies = []
+        if metadata.get('buddies'):
+            try:
+                buddies = simplejson.loads(metadata['buddies']).values()
+            except simplejson.decoder.JSONDecodeError, exception:
+                logging.warning('Cannot decode buddies for %r: %s',
+                                metadata['uid'], exception)
+
+        if not isinstance(buddies, list):
+            logging.warning('Content of buddies for %r is not a list: %r',
+                            metadata['uid'], buddies)
             buddies = []
 
         for n_ in xrange(0, 3):
             if buddies:
-                nick, color = buddies.pop(0)
-                self._cached_row.append((nick, XoColor(color)))
-            else:
-                self._cached_row.append(None)
+                try:
+                    nick, color = buddies.pop(0)
+                except (AttributeError, ValueError), exception:
+                    logging.warning('Malformed buddies for %r: %s',
+                                    metadata['uid'], exception)
+                else:
+                    self._cached_row.append((nick, XoColor(color)))
+                    continue
+
+            self._cached_row.append(None)
 
         return self._cached_row[column]
 
-- 
1.7.3.4



More information about the Dextrose mailing list