From 708a62c06f5212773aee46d4263b9e3a96d8cf32 Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Mon, 19 May 2008 18:03:04 +0200 Subject: [PATCH] Move timestamp_to_elapsed_string to sugar.util and add ActivityBundle.installation_time --- src/sugar/activity/registry.py | 8 ++-- src/sugar/bundle/activitybundle.py | 5 +++ src/sugar/util.py | 69 ++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 3 deletions(-) diff --git a/src/sugar/activity/registry.py b/src/sugar/activity/registry.py index 5f5aefc9..d5d05297 100644 --- a/src/sugar/activity/registry.py +++ b/src/sugar/activity/registry.py @@ -31,11 +31,12 @@ def _activity_info_from_dict(info_dict): return ActivityInfo(info_dict['name'], info_dict['icon'], info_dict['bundle_id'], info_dict['version'], info_dict['path'], info_dict['show_launcher'], - info_dict['command'], info_dict['favorite']) + info_dict['command'], info_dict['favorite'], + info_dict['installation_time']) class ActivityInfo(object): - def __init__(self, name, icon, bundle_id, version, - path, show_launcher, command, favorite): + def __init__(self, name, icon, bundle_id, version, path, show_launcher, + command, favorite, installation_time): self.name = name self.icon = icon self.bundle_id = bundle_id @@ -44,6 +45,7 @@ class ActivityInfo(object): self.command = command self.show_launcher = show_launcher self.favorite = favorite + self.installation_time = installation_time class ActivityRegistry(gobject.GObject): __gsignals__ = { diff --git a/src/sugar/bundle/activitybundle.py b/src/sugar/bundle/activitybundle.py index 5f1fb7bb..db305553 100644 --- a/src/sugar/bundle/activitybundle.py +++ b/src/sugar/bundle/activitybundle.py @@ -163,6 +163,11 @@ class ActivityBundle(Bundle): """Get the activity user visible name.""" return self._name + def get_installation_time(self): + """Get a timestamp representing the time at which this activity was + installed.""" + return os.stat(self._path).st_mtime + def get_bundle_id(self): """Get the activity bundle id""" return self._bundle_id diff --git a/src/sugar/util.py b/src/sugar/util.py index 12f68249..f885523d 100644 --- a/src/sugar/util.py +++ b/src/sugar/util.py @@ -21,6 +21,8 @@ import sha import random import binascii import string +from gettext import gettext as _ +import gettext def printable_hash(in_hash): """Convert binary hash data into printable characters.""" @@ -168,3 +170,70 @@ class LRU: yield j def keys(self): return self.d.keys() + +units = [['%d year', '%d years', 356 * 24 * 60 * 60], + ['%d month', '%d months', 30 * 24 * 60 * 60], + ['%d week', '%d weeks', 7 * 24 * 60 * 60], + ['%d day', '%d days', 24 * 60 * 60], + ['%d hour', '%d hours', 60 * 60], + ['%d minute', '%d minutes', 60]] + +AND = _(' and ') +COMMA = _(', ') + +# TRANS: Indicating something that just happened, eg. "just now", "moments ago" +NOW = _('Seconds ago') + +# TRANS: Indicating time passed, eg. "[10 day, 5 hours] ago", +# "[2 minutes] in the past", or "[3 years, 1 month] earlier" +ELAPSED = _('%s ago') + +# Explanation of the following hack: +# The xgettext utility extracts plural forms by reading the strings included as +# parameters of ngettext(). As our plurals are not passed to ngettext() +# straight away because there needs to be a calculation before we know which +# strings need to be used, then we need to call ngettext() in a fake way so +# xgettext will pick them up as plurals. + +def ngettext(singular, plural, n): pass + +# TRANS: Relative dates (eg. 1 month and 5 days). +ngettext('%d year', '%d years', 1) +ngettext('%d month', '%d months', 1) +ngettext('%d week', '%d weeks', 1) +ngettext('%d day', '%d days', 1) +ngettext('%d hour', '%d hours', 1) +ngettext('%d minute', '%d minutes', 1) + +del ngettext + +# End of plurals hack + +def timestamp_to_elapsed_string(timestamp, max_levels=2): + levels = 0 + time_period = '' + elapsed_seconds = int(time.time() - timestamp) + + for name_singular, name_plural, factor in units: + elapsed_units = elapsed_seconds / factor + if elapsed_units > 0: + + if levels > 0: + time_period += COMMA + + time_period += gettext.ngettext(name_singular, name_plural, + elapsed_units) % elapsed_units + + elapsed_seconds -= elapsed_units * factor + + if time_period != '': + levels += 1 + + if levels == max_levels: + break + + if levels == 0: + return NOW + + return ELAPSED % time_period +