From 19b3249e8b62490ace28d3baf92bccdf72d00e5c Mon Sep 17 00:00:00 2001
From: Aleksey Lim <alsroot@member.fsf.org>
Date: Sun, 27 Sep 2009 16:58:46 +0000
Subject: Journal should behave gracefully on entries with unexpected (but syntactically valid) metadata #1408
---
src/jarabe/journal/listmodel.py | 16 +++++++++--
src/jarabe/journal/model.py | 53 ++++++++++++++++++++++++++++++--------
src/jarabe/journal/palettes.py | 5 +--
3 files changed, 57 insertions(+), 17 deletions(-)
diff --git a/src/jarabe/journal/listmodel.py b/src/jarabe/journal/listmodel.py
index 32df853..770e52f 100644
a
|
b
|
|
15 | 15 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
16 | 16 | |
17 | 17 | import logging |
| 18 | from gettext import gettext as _ |
18 | 19 | |
19 | 20 | import cjson |
20 | 21 | import gobject |
… |
… |
class ListModel(gtk.GenericTreeModel, gtk.TreeDragSource): |
135 | 136 | xo_color = misc.get_icon_color(metadata) |
136 | 137 | self._cached_row.append(xo_color) |
137 | 138 | |
138 | | title = gobject.markup_escape_text(metadata.get('title', None)) |
| 139 | title = gobject.markup_escape_text(metadata.get('title', '')) |
| 140 | if not title: |
| 141 | title = _('Untitled') |
139 | 142 | self._cached_row.append('<b>%s</b>' % title) |
140 | 143 | |
141 | | timestamp = int(metadata.get('timestamp', 0)) |
| 144 | try: |
| 145 | timestamp = int(metadata.get('timestamp', 0)) |
| 146 | except ValueError: |
| 147 | timestamp = 0 |
142 | 148 | self._cached_row.append(util.timestamp_to_elapsed_string(timestamp)) |
143 | 149 | |
144 | | self._cached_row.append(int(metadata.get('progress', 100))) |
| 150 | try: |
| 151 | progress = int(metadata.get('progress', 100)) |
| 152 | except ValueError: |
| 153 | progress = 100 |
| 154 | self._cached_row.append(progress) |
145 | 155 | |
146 | 156 | if metadata.get('buddies', ''): |
147 | 157 | buddies = cjson.decode(metadata['buddies']).values() |
diff --git a/src/jarabe/journal/model.py b/src/jarabe/journal/model.py
index 5deff6a..c83b5b7 100644
a
|
b
|
import gio |
30 | 30 | from sugar import dispatch |
31 | 31 | from sugar import mime |
32 | 32 | from sugar import util |
| 33 | from sugar.graphics import style |
33 | 34 | |
34 | 35 | DS_DBUS_SERVICE = 'org.laptop.sugar.DataStore' |
35 | 36 | DS_DBUS_INTERFACE = 'org.laptop.sugar.DataStore' |
… |
… |
class InplaceResultSet(BaseResultSet): |
349 | 350 | except Exception: |
350 | 351 | logging.exception('Error reading file %r', full_path) |
351 | 352 | |
| 353 | def _get_none_metadata(): |
| 354 | return {'uid': '', |
| 355 | 'title': '', |
| 356 | 'timestamp': 0, |
| 357 | 'mime_type': 'application/octet-stream', |
| 358 | 'activity': '', |
| 359 | 'activity_id': '', |
| 360 | 'icon-color': '%s,%s' % (style.COLOR_BUTTON_GREY.get_svg(), |
| 361 | style.COLOR_TRANSPARENT.get_svg()), |
| 362 | 'description': ''} |
| 363 | |
352 | 364 | def _get_file_metadata(path, stat): |
353 | 365 | client = gconf.client_get_default() |
354 | 366 | return {'uid': path, |
… |
… |
def _get_mount_point(path): |
411 | 423 | def get(object_id): |
412 | 424 | """Returns the metadata for an object |
413 | 425 | """ |
414 | | if os.path.exists(object_id): |
415 | | stat = os.stat(object_id) |
416 | | metadata = _get_file_metadata(object_id, stat) |
417 | | metadata['mountpoint'] = _get_mount_point(object_id) |
| 426 | if not object_id: |
| 427 | metadata = _get_none_metadata() |
| 428 | metadata['mountpoint'] = '/dummy/path/to/mark/it/non-ds' |
| 429 | elif object_id.startswith('/'): |
| 430 | if os.path.exists(object_id): |
| 431 | stat = os.stat(object_id) |
| 432 | metadata = _get_file_metadata(object_id, stat) |
| 433 | metadata['mountpoint'] = _get_mount_point(object_id) |
| 434 | else: |
| 435 | metadata = _get_none_metadata() |
418 | 436 | else: |
419 | 437 | metadata = _get_datastore().get_properties(object_id, byte_arrays=True) |
420 | 438 | metadata['mountpoint'] = '/' |
… |
… |
def get(object_id): |
423 | 441 | def get_file(object_id): |
424 | 442 | """Returns the file for an object |
425 | 443 | """ |
426 | | if os.path.exists(object_id): |
| 444 | if not object_id: |
| 445 | return None |
| 446 | elif object_id.startswith('/'): |
427 | 447 | logging.debug('get_file asked for file with path %r', object_id) |
428 | | return object_id |
| 448 | if os.path.exists(object_id): |
| 449 | return object_id |
| 450 | else: |
| 451 | return None |
429 | 452 | else: |
430 | 453 | logging.debug('get_file asked for entry with id %r', object_id) |
431 | 454 | file_path = _get_datastore().get_filename(object_id) |
… |
… |
def get_file_size(object_id): |
438 | 461 | """Return the file size for an object |
439 | 462 | """ |
440 | 463 | logging.debug('get_file_size %r', object_id) |
441 | | if os.path.exists(object_id): |
442 | | return os.stat(object_id).st_size |
| 464 | if object_id.startswith('/'): |
| 465 | if os.path.exists(object_id): |
| 466 | return os.stat(object_id).st_size |
| 467 | else: |
| 468 | return 0 |
443 | 469 | |
444 | 470 | file_path = _get_datastore().get_filename(object_id) |
445 | 471 | if file_path: |
… |
… |
def get_unique_values(key): |
458 | 484 | def delete(object_id): |
459 | 485 | """Removes an object from persistent storage |
460 | 486 | """ |
461 | | if os.path.exists(object_id): |
462 | | os.unlink(object_id) |
463 | | deleted.send(None, object_id=object_id) |
| 487 | if not object_id: |
| 488 | pass |
| 489 | elif object_id.startswith('/'): |
| 490 | if os.path.exists(object_id): |
| 491 | os.unlink(object_id) |
| 492 | deleted.send(None, object_id=object_id) |
| 493 | else: |
| 494 | logging.error('Can not find file %s to delete', object_id) |
464 | 495 | else: |
465 | 496 | _get_datastore().delete(object_id) |
466 | 497 | |
diff --git a/src/jarabe/journal/palettes.py b/src/jarabe/journal/palettes.py
index e0dfbf4..f5dab7d 100644
a
|
b
|
class ObjectPalette(Palette): |
53 | 53 | activity_icon.props.file = misc.get_icon_name(metadata) |
54 | 54 | activity_icon.props.xo_color = misc.get_icon_color(metadata) |
55 | 55 | |
56 | | if metadata.has_key('title'): |
57 | | title = gobject.markup_escape_text(metadata['title']) |
58 | | else: |
| 56 | title = gobject.markup_escape_text(metadata.get('title', '')) |
| 57 | if not title: |
59 | 58 | title = _('Untitled') |
60 | 59 | |
61 | 60 | Palette.__init__(self, primary_text=title, |