Ticket #1408: sugar-1408.patch

File sugar-1408.patch, 6.0 KB (added by alsroot, 15 years ago)
  • src/jarabe/journal/listmodel.py

    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  
    1515# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1616
    1717import logging
     18from gettext import gettext as _
    1819
    1920import cjson
    2021import gobject
    class ListModel(gtk.GenericTreeModel, gtk.TreeDragSource): 
    135136            xo_color = misc.get_icon_color(metadata)
    136137        self._cached_row.append(xo_color)
    137138
    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')
    139142        self._cached_row.append('<b>%s</b>' % title)
    140143
    141         timestamp = int(metadata.get('timestamp', 0))
     144        try:
     145            timestamp = int(metadata.get('timestamp', 0))
     146        except ValueError:
     147            timestamp = 0
    142148        self._cached_row.append(util.timestamp_to_elapsed_string(timestamp))
    143149
    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)
    145155
    146156        if metadata.get('buddies', ''):
    147157            buddies = cjson.decode(metadata['buddies']).values()
  • src/jarabe/journal/model.py

    diff --git a/src/jarabe/journal/model.py b/src/jarabe/journal/model.py
    index 5deff6a..c83b5b7 100644
    a b import gio 
    3030from sugar import dispatch
    3131from sugar import mime
    3232from sugar import util
     33from sugar.graphics import style
    3334
    3435DS_DBUS_SERVICE = 'org.laptop.sugar.DataStore'
    3536DS_DBUS_INTERFACE = 'org.laptop.sugar.DataStore'
    class InplaceResultSet(BaseResultSet): 
    349350            except Exception:
    350351                logging.exception('Error reading file %r', full_path)
    351352
     353def _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
    352364def _get_file_metadata(path, stat):
    353365    client = gconf.client_get_default()
    354366    return {'uid': path,
    def _get_mount_point(path): 
    411423def get(object_id):
    412424    """Returns the metadata for an object
    413425    """
    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()
    418436    else:
    419437        metadata = _get_datastore().get_properties(object_id, byte_arrays=True)
    420438        metadata['mountpoint'] = '/'
    def get(object_id): 
    423441def get_file(object_id):
    424442    """Returns the file for an object
    425443    """
    426     if os.path.exists(object_id):
     444    if not object_id:
     445        return None
     446    elif object_id.startswith('/'):
    427447        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
    429452    else:
    430453        logging.debug('get_file asked for entry with id %r', object_id)
    431454        file_path = _get_datastore().get_filename(object_id)
    def get_file_size(object_id): 
    438461    """Return the file size for an object
    439462    """
    440463    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
    443469
    444470    file_path = _get_datastore().get_filename(object_id)
    445471    if file_path:
    def get_unique_values(key): 
    458484def delete(object_id):
    459485    """Removes an object from persistent storage
    460486    """
    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)
    464495    else:
    465496        _get_datastore().delete(object_id)
    466497
  • src/jarabe/journal/palettes.py

    diff --git a/src/jarabe/journal/palettes.py b/src/jarabe/journal/palettes.py
    index e0dfbf4..f5dab7d 100644
    a b class ObjectPalette(Palette): 
    5353        activity_icon.props.file = misc.get_icon_name(metadata)
    5454        activity_icon.props.xo_color = misc.get_icon_color(metadata)
    5555
    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:
    5958            title = _('Untitled')
    6059
    6160        Palette.__init__(self, primary_text=title,