VMM/common: Reworked human_size() once more. Return bytes w/o v0.6.x
authorPascal Volk <neverseen@users.sourceforge.net>
Sat, 12 Feb 2011 17:19:43 +0000
branchv0.6.x
changeset 399 fb22773f7a85
parent 398 3e5ed678d535
child 400 0512d940918f
VMM/common: Reworked human_size() once more. Return bytes w/o prefix. Use translatable $FOO_bibyte prefixes, e.g., MiB.
VirtualMailManager/common.py
--- a/VirtualMailManager/common.py	Sat Feb 12 01:46:00 2011 +0000
+++ b/VirtualMailManager/common.py	Sat Feb 12 17:19:43 2011 +0000
@@ -76,16 +76,16 @@
             raise TypeError("'size' must be a positive long or int.")
     if size < 0:
         raise ValueError("'size' must be a positive long or int.")
-    if not size:
-        return '0b'
-    unit_limit = (('T', 1 << 40), ('G', 1 << 30), ('M', 1 << 20),
-                  ('k', 1 << 10), ('b', 1))
-    for unit, limit in unit_limit:
-        if size >= limit:
-            if unit != 'b':
-                return '%.2f%s' % (size / float(limit), unit)
-            else:
-                return '%ub' % size
+    if size < 1024:
+        return str(size)
+    prefix_multiply = ((_(u'TiB'), 1 << 40), (_(u'GiB'), 1 << 30),
+                       (_(u'MiB'), 1 << 20), (_(u'KiB'), 1 << 10))
+    for prefix, multiply in prefix_multiply:
+        if size >= multiply:
+            # TP: e.g.: '%(size).2f %(prefix)s' -> '118.30 MiB'
+            return _(u'%(size).2f %(prefix)s') % {
+                    'size': float(size) / multiply,
+                    'prefix': prefix}
 
 
 def size_in_bytes(size):