view rhodecode/lib/vcs/utils/filesize.py @ 3774:60335b702a00 beta

invalidation: don't create CacheInvalidation records on startup Creating the records early gave an advantage before lightweight was introduced. With lightweight it is no longer necessary. The records will be created on demand anyway and there is no reason to create and maintain them before they are used.
author Mads Kiilerich <madski@unity3d.com>
date Wed, 03 Apr 2013 15:56:12 +0200
parents 324ac367a4da
children
line wrap: on
line source

def filesizeformat(bytes, sep=' '):
    """
    Formats the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB,
    102 B, 2.3 GB etc).

    Grabbed from Django (http://www.djangoproject.com), slightly modified.

    :param bytes: size in bytes (as integer)
    :param sep: string separator between number and abbreviation
    """
    try:
        bytes = float(bytes)
    except (TypeError, ValueError, UnicodeDecodeError):
        return '0%sB' % sep

    if bytes < 1024:
        size = bytes
        template = '%.0f%sB'
    elif bytes < 1024 * 1024:
        size = bytes / 1024
        template = '%.0f%sKB'
    elif bytes < 1024 * 1024 * 1024:
        size = bytes / 1024 / 1024
        template = '%.1f%sMB'
    else:
        size = bytes / 1024 / 1024 / 1024
        template = '%.2f%sGB'
    return template % (size, sep)