# HG changeset patch # User Marcin Kuzminski # Date 1272147154 -7200 # Node ID be0096a02772cf831de953e02ec3cad8e73afc21 # Parent f24b9a2934cfbddd2dd3eb38045be63a54af6bf8 added helper for filesize diff -r f24b9a2934cf -r be0096a02772 pylons_app/lib/helpers.py --- a/pylons_app/lib/helpers.py Sat Apr 24 18:20:59 2010 +0200 +++ b/pylons_app/lib/helpers.py Sun Apr 25 00:12:34 2010 +0200 @@ -4,6 +4,7 @@ available to Controllers. This module is available to both as 'h'. """ from pylons import url +from pylons.i18n.translation import _, ungettext from webhelpers.html import (literal, HTML, escape) from webhelpers.html.tools import (auto_link, button_to, highlight, js_obfuscate , mail_to, strip_links, strip_tags, tag_re) @@ -43,6 +44,27 @@ if form_errors and form_errors.has_key(field_name): return literal(tmpl % form_errors.get(field_name)) +class _FileSizeFormat(): + """ + Formats the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB, + 102 bytes, etc). + """ + def __call__(self, bytes): + try: + bytes = float(bytes) + except TypeError: + return u"0 bytes" + + if bytes < 1024: + return ungettext("%(size)d byte", "%(size)d bytes", bytes) % {'size': bytes} + if bytes < 1024 * 1024: + return _("%.1f KB") % (bytes / 1024) + if bytes < 1024 * 1024 * 1024: + return _("%.1f MB") % (bytes / (1024 * 1024)) + return _("%.1f GB") % (bytes / (1024 * 1024 * 1024)) + + +filesizeformat = _FileSizeFormat() link = _Link() flash = _Flash() get_error = _GetError()