# HG changeset patch # User Thierry Florac # Date 1351157934 -7200 # Node ID 722ebc739cf1ed01581749a793d2ee1833a954bb # Parent db7a00baf3892e5a4b5df863e64a46db87733775 Added getHumanSize utility function diff -r db7a00baf389 -r 722ebc739cf1 src/ztfy/utils/size.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ztfy/utils/size.py Thu Oct 25 11:38:54 2012 +0200 @@ -0,0 +1,45 @@ +### -*- coding: utf-8 -*- #################################################### +############################################################################## +# +# Copyright (c) 2012 Thierry Florac +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## + + +# import standard packages + +# import Zope3 interfaces + +# import local interfaces + +# import Zope3 packages +from zope.i18n import translate + +# import local packages +from ztfy.utils.request import queryRequest + +from ztfy.utils import _ + + +def getHumanSize(value, request=None): + """Convert given bytes value in human readable format""" + if request is None: + request = queryRequest() + if value < 1024: + return translate(_("%d bytes"), context=request) % value + value = value / 1024.0 + if value < 1024: + return translate(_("%.1f Kb"), context=request) % value + value = value / 1024.0 + if value < 1024: + return translate(_("%.2f Mb"), context=request) % value + value = value / 1024.0 + return translate(_("%.3f Gb"), context=request) % value