Ticket #1215: 0001-add-filesize-to-returned-metadata.patch

File 0001-add-filesize-to-returned-metadata.patch, 2.9 KB (added by sascha_silbe, 13 years ago)

sugar-datastore: add 'filesize' property to metadata returned by API calls

  • src/carquinyol/datastore.py

    From 6745aabf9aef557a157c2f7e3c8ba294c05d081d Mon Sep 17 00:00:00 2001
    From: Sascha Silbe <sascha@silbe.org>
    Date: Mon, 24 Aug 2009 16:48:43 +0200
    Subject: [PATCH] add filesize to returned metadata
    
    ---
     src/carquinyol/datastore.py |   20 ++++++++++----------
     src/carquinyol/filestore.py |    7 +++++++
     2 files changed, 17 insertions(+), 10 deletions(-)
    
    diff --git a/src/carquinyol/datastore.py b/src/carquinyol/datastore.py
    index dc2e167..21de309 100644
    a b import dbus.service 
    2626import gobject
    2727
    2828from sugar import mime
     29from sugar.logger import trace
    2930
    3031from carquinyol import layoutmanager
    3132from carquinyol import migration
    class DataStore(dbus.service.Object): 
    225226
    226227                return self._find_all(query, properties)
    227228
    228             metadata = self._metadata_store.retrieve(uid, properties)
    229             entries.append(metadata)
     229            entries.append(self._get_metadata(uid, properties))
    230230
    231231        logger.debug('find(): %r', time.time() - t)
    232232
    class DataStore(dbus.service.Object): 
    240240        limit = query.get('limit', MAX_QUERY_LIMIT)
    241241        uids = uids[offset:offset + limit]
    242242
    243         entries = []
    244         for uid in uids:
    245             metadata = self._metadata_store.retrieve(uid, properties)
    246             entries.append(metadata)
    247 
     243        entries = [self._get_metadata(uid, properties) for uid in uids]
    248244        return entries, count
    249245
     246    def _get_metadata(self, uid, properties):
     247        metadata = self._metadata_store.retrieve(uid, properties)
     248        metadata['filesize'] = self._file_store.get_file_size(uid)
     249        return metadata
     250
    250251    @dbus.service.method(DS_DBUS_INTERFACE,
    251252             in_signature='s',
    252253             out_signature='s',
    class DataStore(dbus.service.Object): 
    266267    @dbus.service.method(DS_DBUS_INTERFACE,
    267268                         in_signature='s',
    268269                         out_signature='a{sv}')
     270    @trace(logger=logger)
    269271    def get_properties(self, uid):
    270         logging.debug('datastore.get_properties %r', uid)
    271         metadata = self._metadata_store.retrieve(uid)
    272         return metadata
     272        return self._get_metadata(uid, [])
    273273
    274274    @dbus.service.method(DS_DBUS_INTERFACE,
    275275                         in_signature='sa{sv}',
  • src/carquinyol/filestore.py

    diff --git a/src/carquinyol/filestore.py b/src/carquinyol/filestore.py
    index 71d6344..baa0e28 100644
    a b class FileStore(object): 
    146146    def get_file_path(self, uid):
    147147        return layoutmanager.get_instance().get_data_path(uid)
    148148
     149    def get_file_size(self, uid):
     150        file_path = layoutmanager.get_instance().get_data_path(uid)
     151        if not os.path.exists(file_path):
     152            return 0
     153
     154        return os.stat(file_path).st_size
     155
    149156    def delete(self, uid):
    150157        """Remove the file associated to a given entry.
    151158