VirtualMailManager/common.py
branchv0.6.x
changeset 399 fb22773f7a85
parent 396 7f931c1ca059
child 414 ae1a8428298c
equal deleted inserted replaced
398:3e5ed678d535 399:fb22773f7a85
    74             size = long(size)
    74             size = long(size)
    75         except ValueError:
    75         except ValueError:
    76             raise TypeError("'size' must be a positive long or int.")
    76             raise TypeError("'size' must be a positive long or int.")
    77     if size < 0:
    77     if size < 0:
    78         raise ValueError("'size' must be a positive long or int.")
    78         raise ValueError("'size' must be a positive long or int.")
    79     if not size:
    79     if size < 1024:
    80         return '0b'
    80         return str(size)
    81     unit_limit = (('T', 1 << 40), ('G', 1 << 30), ('M', 1 << 20),
    81     prefix_multiply = ((_(u'TiB'), 1 << 40), (_(u'GiB'), 1 << 30),
    82                   ('k', 1 << 10), ('b', 1))
    82                        (_(u'MiB'), 1 << 20), (_(u'KiB'), 1 << 10))
    83     for unit, limit in unit_limit:
    83     for prefix, multiply in prefix_multiply:
    84         if size >= limit:
    84         if size >= multiply:
    85             if unit != 'b':
    85             # TP: e.g.: '%(size).2f %(prefix)s' -> '118.30 MiB'
    86                 return '%.2f%s' % (size / float(limit), unit)
    86             return _(u'%(size).2f %(prefix)s') % {
    87             else:
    87                     'size': float(size) / multiply,
    88                 return '%ub' % size
    88                     'prefix': prefix}
    89 
    89 
    90 
    90 
    91 def size_in_bytes(size):
    91 def size_in_bytes(size):
    92     """Converts the string `size` to a long (size in bytes).
    92     """Converts the string `size` to a long (size in bytes).
    93 
    93