[Sugar-devel] [PATCH sugar 1/4] Actually use property filesize for showing file size in Journal Details View

Sascha Silbe silbe at activitycentral.com
Sun Feb 19 09:44:40 EST 2012


Commit bc1edfa2193ca554d385c68e43c2cef0b9795a54 introduced code that tries to
use the property 'filesize' to avoid a potentially expensive checkout of a
data store entry just to display its size. But since dict.get() is just a
regular function with no short-circuit evaluation of arguments, we end up
checking it out regardless.

Fix this by moving the optimisation to jarabe.journal.model.get_file_size(),
using explicit short-circuiting.

Signed-off-by: Sascha Silbe <silbe at activitycentral.com>
---
 src/jarabe/journal/expandedentry.py |    4 ++--
 src/jarabe/journal/model.py         |    7 ++++++-
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/jarabe/journal/expandedentry.py b/src/jarabe/journal/expandedentry.py
index 4e99dc2..6de6b4a 100644
--- a/src/jarabe/journal/expandedentry.py
+++ b/src/jarabe/journal/expandedentry.py
@@ -260,11 +260,11 @@ def _create_technical(self):
         vbox = hippo.CanvasBox()
         vbox.props.spacing = style.DEFAULT_SPACING

+        size = model.get_file_size(self._metadata['uid'], self._metadata)
         lines = [
             _('Kind: %s') % (self._metadata.get('mime_type') or _('Unknown'),),
             _('Date: %s') % (self._format_date(),),
-            _('Size: %s') % (format_size(int(self._metadata.get('filesize',
-                                model.get_file_size(self._metadata['uid']))))),
+            _('Size: %s') % (format_size(size), ),
             ]

         for line in lines:
diff --git a/src/jarabe/journal/model.py b/src/jarabe/journal/model.py
index 5285a7c..5d9dd6c 100644
--- a/src/jarabe/journal/model.py
+++ b/src/jarabe/journal/model.py
@@ -568,13 +568,18 @@ def get_file(object_id):
             return None


-def get_file_size(object_id):
+def get_file_size(object_id, metadata=None):
     """Return the file size for an object
     """
     logging.debug('get_file_size %r', object_id)
     if os.path.exists(object_id):
         return os.stat(object_id).st_size

+    if metadata is None:
+        metadata = get(object_id)
+    if 'filesize' in metadata:
+        return int(metadata['filesize'])
+
     file_path = _get_datastore().get_filename(object_id)
     if file_path:
         size = os.stat(file_path).st_size
--
1.7.9



More information about the Sugar-devel mailing list