[Dextrose] [PATCH sugar 08/11] Journal details view: don't choke on invalid timestamp (SL#1590, SL#2208)
Aleksey Lim
alsroot at member.fsf.org
Sat Jan 15 09:10:47 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.
Acked-by: Simon Schampijer <simon at schampijer.de>
---
src/jarabe/journal/expandedentry.py | 13 +++++++++----
src/jarabe/journal/listmodel.py | 1 +
src/jarabe/journal/misc.py | 26 ++++++++++++++++++--------
3 files changed, 28 insertions(+), 12 deletions(-)
diff --git a/src/jarabe/journal/expandedentry.py b/src/jarabe/journal/expandedentry.py
index 725c0f9..9775901 100644
--- a/src/jarabe/journal/expandedentry.py
+++ b/src/jarabe/journal/expandedentry.py
@@ -280,10 +280,15 @@ class ExpandedEntry(hippo.CanvasBox):
def _format_date(self):
if 'timestamp' in self._metadata:
- timestamp = float(self._metadata['timestamp'])
- return time.strftime('%x', time.localtime(timestamp))
- else:
- return _('No date')
+ try:
+ timestamp = float(self._metadata['timestamp'])
+ except (ValueError, TypeError):
+ logging.warning('Invalid timestamp for %r: %r',
+ self._metadata['uid'],
+ self._metadata['timestamp'])
+ else:
+ return time.strftime('%x', time.localtime(timestamp))
+ return _('No date')
def _create_buddy_list(self):
diff --git a/src/jarabe/journal/listmodel.py b/src/jarabe/journal/listmodel.py
index a22c5ea..396213b 100644
--- a/src/jarabe/journal/listmodel.py
+++ b/src/jarabe/journal/listmodel.py
@@ -15,6 +15,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import logging
+from gettext import gettext as _
import simplejson
import gobject
diff --git a/src/jarabe/journal/misc.py b/src/jarabe/journal/misc.py
index 710cb15..4a2211f 100644
--- a/src/jarabe/journal/misc.py
+++ b/src/jarabe/journal/misc.py
@@ -87,14 +87,24 @@ def get_icon_name(metadata):
def get_date(metadata):
""" Convert from a string in iso format to a more human-like format. """
- if metadata.has_key('timestamp'):
- timestamp = float(metadata['timestamp'])
- return util.timestamp_to_elapsed_string(timestamp)
- elif metadata.has_key('mtime'):
- ti = time.strptime(metadata['mtime'], "%Y-%m-%dT%H:%M:%S")
- return util.timestamp_to_elapsed_string(time.mktime(ti))
- else:
- return _('No date')
+ if 'timestamp' in metadata:
+ try:
+ timestamp = float(metadata['timestamp'])
+ except (TypeError, ValueError):
+ logging.warning('Invalid timestamp: %r', metadata['timestamp'])
+ else:
+ return util.timestamp_to_elapsed_string(timestamp)
+
+ if 'mtime' in metadata:
+ try:
+ ti = time.strptime(metadata['mtime'], '%Y-%m-%dT%H:%M:%S')
+ except (TypeError, ValueError):
+ logging.warning('Invalid mtime: %r', metadata['mtime'])
+ else:
+ return util.timestamp_to_elapsed_string(time.mktime(ti))
+
+ return _('No date')
+
def get_bundle(metadata):
try:
--
1.7.3.4
More information about the Dextrose
mailing list