Changeset - be0096a02772
[Not reviewed]
default
0 1 0
Marcin Kuzminski - 16 years ago 2010-04-25 00:12:34
marcin@python-works.com
added helper for filesize
1 file changed with 22 insertions and 0 deletions:
0 comments (0 inline, 0 general)
pylons_app/lib/helpers.py
Show inline comments
 
@@ -4,6 +4,7 @@ Consists of functions to typically be us
 
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 @@ class _GetError(object):
 
        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()
0 comments (0 inline, 0 general)