VirtualMailManager/common.py
branchv0.6.x
changeset 393 fb2ba1456bc5
parent 383 ac5ac03b58da
child 394 d3a3c6155879
equal deleted inserted replaced
392:ffce67e3c6eb 393:fb2ba1456bc5
    63                        NO_SUCH_BINARY)
    63                        NO_SUCH_BINARY)
    64     if not os.access(binary, os.X_OK):
    64     if not os.access(binary, os.X_OK):
    65         raise VMMError(_(u"File is not executable: '%s'") %
    65         raise VMMError(_(u"File is not executable: '%s'") %
    66                        get_unicode(binary), NOT_EXECUTABLE)
    66                        get_unicode(binary), NOT_EXECUTABLE)
    67     return binary
    67     return binary
       
    68 
       
    69 
       
    70 def human_size(size):
       
    71     """Converts the `size` in bytes in human readable format."""
       
    72     if not isinstance(size, (long, int)) or size < 1:
       
    73         raise TypeError("'size' must be a long or int and greater than 0.")
       
    74     unit_limit = (('T', 1 << 40), ('G', 1 << 30), ('M', 1 << 20),
       
    75                   ('k', 1 << 10), ('b', 1))
       
    76     for unit, limit in unit_limit:
       
    77         if size >= limit:
       
    78             if unit != 'b':
       
    79                 return '%.2f%s' % (size / float(limit), unit)
       
    80             else:
       
    81                 return '%u%s' % (size / limit, unit)
    68 
    82 
    69 
    83 
    70 def size_in_bytes(size):
    84 def size_in_bytes(size):
    71     """Converts the string `size` to a long (size in bytes).
    85     """Converts the string `size` to a long (size in bytes).
    72 
    86