terom@12: ############################################################### terom@12: # Functions taken from pathutils.py Version 0.2.5 (2005/12/06), http://www.voidspace.org.uk/python/recipebook.shtml#utils terom@12: # Copyright Michael Foord 2004 terom@12: # Released subject to the BSD License terom@12: # Please see http://www.voidspace.org.uk/python/license.shtml terom@12: terom@12: ############################################################### terom@12: # formatbytes takes a filesize (as returned by os.getsize() ) terom@12: # and formats it for display in one of two ways !! terom@12: terom@12: # For information about bugfixes, updates and support, please join the Pythonutils mailing list. terom@12: # http://groups.google.com/group/pythonutils/ terom@12: # Comments, suggestions and bug reports welcome. terom@12: # Scripts maintained at http://www.voidspace.org.uk/python/index.shtml terom@12: # E-mail fuzzyman@voidspace.org.uk terom@12: terom@12: def formatbytes(sizeint, configdict=None, **configs): terom@12: """ terom@12: Given a file size as an integer, return a nicely formatted string that terom@12: represents the size. Has various options to control it's output. terom@12: terom@12: You can pass in a dictionary of arguments or keyword arguments. Keyword terom@12: arguments override the dictionary and there are sensible defaults for options terom@12: you don't set. terom@12: terom@12: Options and defaults are as follows : terom@12: terom@12: * ``forcekb = False`` - If set this forces the output to be in terms terom@12: of kilobytes and bytes only. terom@12: terom@12: * ``largestonly = True`` - If set, instead of outputting terom@12: ``1 Mbytes, 307 Kbytes, 478 bytes`` it outputs using only the largest terom@12: denominator - e.g. ``1.3 Mbytes`` or ``17.2 Kbytes`` terom@12: terom@12: * ``kiloname = 'Kbytes'`` - The string to use for kilobytes terom@12: terom@12: * ``meganame = 'Mbytes'`` - The string to use for Megabytes terom@12: terom@12: * ``bytename = 'bytes'`` - The string to use for bytes terom@12: terom@12: * ``nospace = True`` - If set it outputs ``1Mbytes, 307Kbytes``, terom@12: notice there is no space. terom@12: terom@12: Example outputs : :: terom@12: terom@12: 19Mbytes, 75Kbytes, 255bytes terom@12: 2Kbytes, 0bytes terom@12: 23.8Mbytes terom@12: terom@12: .. note:: terom@12: terom@12: It currently uses the plural form even for singular. terom@12: """ terom@12: defaultconfigs = { 'forcekb' : False, terom@12: 'largestonly' : True, terom@12: 'kiloname' : 'Kbytes', terom@12: 'meganame' : 'Mbytes', terom@12: 'bytename' : 'bytes', terom@12: 'nospace' : True} terom@12: if configdict is None: terom@12: configdict = {} terom@12: for entry in configs: terom@12: # keyword parameters override the dictionary passed in terom@12: configdict[entry] = configs[entry] terom@12: # terom@12: for keyword in defaultconfigs: terom@12: if not configdict.has_key(keyword): terom@12: configdict[keyword] = defaultconfigs[keyword] terom@12: # terom@12: if configdict['nospace']: terom@12: space = '' terom@12: else: terom@12: space = ' ' terom@12: # terom@12: mb, kb, rb = bytedivider(sizeint) terom@12: if configdict['largestonly']: terom@12: if mb and not configdict['forcekb']: terom@12: return stringround(mb, kb)+ space + configdict['meganame'] terom@12: elif kb or configdict['forcekb']: terom@12: if mb and configdict['forcekb']: terom@12: kb += 1024*mb terom@12: return stringround(kb, rb) + space+ configdict['kiloname'] terom@12: else: terom@12: return str(rb) + space + configdict['bytename'] terom@12: else: terom@12: outstr = '' terom@12: if mb and not configdict['forcekb']: terom@12: outstr = str(mb) + space + configdict['meganame'] +', ' terom@12: if kb or configdict['forcekb'] or mb: terom@12: if configdict['forcekb']: terom@12: kb += 1024*mb terom@12: outstr += str(kb) + space + configdict['kiloname'] +', ' terom@12: return outstr + str(rb) + space + configdict['bytename'] terom@12: terom@12: def stringround(main, rest): terom@12: """ terom@12: Given a file size in either (mb, kb) or (kb, bytes) - round it terom@12: appropriately. terom@12: """ terom@12: # divide an int by a float... get a float terom@12: value = main + rest/1024.0 terom@12: return str(round(value, 1)) terom@12: terom@12: def bytedivider(nbytes): terom@12: """ terom@12: Given an integer (probably a long integer returned by os.getsize() ) terom@12: it returns a tuple of (megabytes, kilobytes, bytes). terom@12: terom@12: This can be more easily converted into a formatted string to display the terom@12: size of the file. terom@12: """ terom@12: mb, remainder = divmod(nbytes, 1048576) terom@12: kb, rb = divmod(remainder, 1024) terom@12: return (mb, kb, rb) terom@12: terom@12: