src/ztfy/utils/size.py
branchZTK-1.1
changeset 184 722ebc739cf1
child 207 7630f79b1506
equal deleted inserted replaced
183:db7a00baf389 184:722ebc739cf1
       
     1 ### -*- coding: utf-8 -*- ####################################################
       
     2 ##############################################################################
       
     3 #
       
     4 # Copyright (c) 2012 Thierry Florac <tflorac AT ulthar.net>
       
     5 # All Rights Reserved.
       
     6 #
       
     7 # This software is subject to the provisions of the Zope Public License,
       
     8 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     9 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
    10 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
    11 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    12 # FOR A PARTICULAR PURPOSE.
       
    13 #
       
    14 ##############################################################################
       
    15 
       
    16 
       
    17 # import standard packages
       
    18 
       
    19 # import Zope3 interfaces
       
    20 
       
    21 # import local interfaces
       
    22 
       
    23 # import Zope3 packages
       
    24 from zope.i18n import translate
       
    25 
       
    26 # import local packages
       
    27 from ztfy.utils.request import queryRequest
       
    28 
       
    29 from ztfy.utils import _
       
    30 
       
    31 
       
    32 def getHumanSize(value, request=None):
       
    33     """Convert given bytes value in human readable format"""
       
    34     if request is None:
       
    35         request = queryRequest()
       
    36     if value < 1024:
       
    37         return translate(_("%d bytes"), context=request) % value
       
    38     value = value / 1024.0
       
    39     if value < 1024:
       
    40         return translate(_("%.1f Kb"), context=request) % value
       
    41     value = value / 1024.0
       
    42     if value < 1024:
       
    43         return translate(_("%.2f Mb"), context=request) % value
       
    44     value = value / 1024.0
       
    45     return translate(_("%.3f Gb"), context=request) % value