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