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