utils.py
changeset 7 235ae238f694
equal deleted inserted replaced
6:d9d1f8e5f384 7:235ae238f694
       
     1 ###############################################################
       
     2 # Functions taken from pathutils.py Version 0.2.5 (2005/12/06), http://www.voidspace.org.uk/python/recipebook.shtml#utils
       
     3 # Copyright Michael Foord 2004
       
     4 # Released subject to the BSD License
       
     5 # Please see http://www.voidspace.org.uk/python/license.shtml
       
     6 
       
     7 ###############################################################
       
     8 # formatbytes takes a filesize (as returned by os.getsize() )
       
     9 # and formats it for display in one of two ways !!
       
    10 
       
    11 # For information about bugfixes, updates and support, please join the Pythonutils mailing list.
       
    12 # http://groups.google.com/group/pythonutils/
       
    13 # Comments, suggestions and bug reports welcome.
       
    14 # Scripts maintained at http://www.voidspace.org.uk/python/index.shtml
       
    15 # E-mail fuzzyman@voidspace.org.uk
       
    16 
       
    17 def formatbytes(sizeint, configdict=None, **configs):
       
    18     """
       
    19     Given a file size as an integer, return a nicely formatted string that
       
    20     represents the size. Has various options to control it's output.
       
    21     
       
    22     You can pass in a dictionary of arguments or keyword arguments. Keyword
       
    23     arguments override the dictionary and there are sensible defaults for options
       
    24     you don't set.
       
    25     
       
    26     Options and defaults are as follows :
       
    27     
       
    28     *    ``forcekb = False`` -         If set this forces the output to be in terms
       
    29     of kilobytes and bytes only.
       
    30     
       
    31     *    ``largestonly = True`` -    If set, instead of outputting 
       
    32         ``1 Mbytes, 307 Kbytes, 478 bytes`` it outputs using only the largest 
       
    33         denominator - e.g. ``1.3 Mbytes`` or ``17.2 Kbytes``
       
    34     
       
    35     *    ``kiloname = 'Kbytes'`` -    The string to use for kilobytes
       
    36     
       
    37     *    ``meganame = 'Mbytes'`` - The string to use for Megabytes
       
    38     
       
    39     *    ``bytename = 'bytes'`` -     The string to use for bytes
       
    40     
       
    41     *    ``nospace = True`` -        If set it outputs ``1Mbytes, 307Kbytes``, 
       
    42         notice there is no space.
       
    43     
       
    44     Example outputs : ::
       
    45     
       
    46         19Mbytes, 75Kbytes, 255bytes
       
    47         2Kbytes, 0bytes
       
    48         23.8Mbytes
       
    49     
       
    50     .. note::
       
    51     
       
    52         It currently uses the plural form even for singular.
       
    53     """
       
    54     defaultconfigs = {  'forcekb' : False,
       
    55                         'largestonly' : True,
       
    56                         'kiloname' : 'Kbytes',
       
    57                         'meganame' : 'Mbytes',
       
    58                         'bytename' : 'bytes',
       
    59                         'nospace' : True}
       
    60     if configdict is None:
       
    61         configdict = {}
       
    62     for entry in configs:
       
    63         # keyword parameters override the dictionary passed in
       
    64         configdict[entry] = configs[entry]
       
    65     #
       
    66     for keyword in defaultconfigs:
       
    67         if not configdict.has_key(keyword):
       
    68             configdict[keyword] = defaultconfigs[keyword]
       
    69     #
       
    70     if configdict['nospace']:
       
    71         space = ''
       
    72     else:
       
    73         space = ' '
       
    74     #
       
    75     mb, kb, rb = bytedivider(sizeint)
       
    76     if configdict['largestonly']:
       
    77         if mb and not configdict['forcekb']:
       
    78             return stringround(mb, kb)+ space + configdict['meganame']
       
    79         elif kb or configdict['forcekb']:
       
    80             if mb and configdict['forcekb']:
       
    81                 kb += 1024*mb
       
    82             return stringround(kb, rb) + space+ configdict['kiloname']
       
    83         else:
       
    84             return str(rb) + space + configdict['bytename']
       
    85     else:
       
    86         outstr = ''
       
    87         if mb and not configdict['forcekb']:
       
    88             outstr = str(mb) + space + configdict['meganame'] +', '
       
    89         if kb or configdict['forcekb'] or mb:
       
    90             if configdict['forcekb']:
       
    91                 kb += 1024*mb 
       
    92             outstr += str(kb) + space + configdict['kiloname'] +', '
       
    93         return outstr + str(rb) + space + configdict['bytename']
       
    94 
       
    95 def stringround(main, rest):
       
    96     """
       
    97     Given a file size in either (mb, kb) or (kb, bytes) - round it
       
    98     appropriately.
       
    99     """
       
   100     # divide an int by a float... get a float
       
   101     value = main + rest/1024.0
       
   102     return str(round(value, 1))
       
   103 
       
   104 def bytedivider(nbytes):
       
   105     """
       
   106     Given an integer (probably a long integer returned by os.getsize() )
       
   107     it returns a tuple of (megabytes, kilobytes, bytes).
       
   108     
       
   109     This can be more easily converted into a formatted string to display the
       
   110     size of the file.
       
   111     """ 
       
   112     mb, remainder = divmod(nbytes, 1048576)
       
   113     kb, rb = divmod(remainder, 1024)
       
   114     return (mb, kb, rb)
       
   115 
       
   116