add format.py module with filesize/filetime, replaces helpers.py and formatbytes
authorTero Marttila <terom@fixme.fi>
Fri, 05 Jun 2009 19:29:04 +0300
changeset 56 80658a2eebf6
parent 55 77abe8dca695
child 57 8d06e0283b88
add format.py module with filesize/filetime, replaces helpers.py and formatbytes
degal/format.py
degal/formatbytes.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/degal/format.py	Fri Jun 05 19:29:04 2009 +0300
@@ -0,0 +1,32 @@
+"""
+    Miscellaneous formatting functions
+"""
+
+import datetime
+
+def filesize (bytes) :
+    """
+        Returns a human-readable string describing the given filesize
+
+        The returned string is either "x.xx MiB", "x.xx KiB", or "zzz Bytes", whereby x.xx is at least 0.1
+    """
+
+    kb, bytes   = divmod(bytes, 1024)
+    mb, kb      = divmod(kb, 1024)
+
+    if mb or kb > 1024 / 10.0 :
+        return "%f.2 MiB" % (mb + kb / 1024 + bytes / 1024**2)
+
+    elif kb or bytes > 1024 / 10.0 :
+        return "%f.2 KiB" % (kb + bytes / 1024)
+
+    else :
+        return "%d Bytes" % bytes
+
+def filetime (ts) :
+    """
+        Returns a human-readable string describing the given file timestamp
+    """
+
+    return datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S")
+
--- a/degal/formatbytes.py	Fri Jun 05 19:28:11 2009 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,116 +0,0 @@
-###############################################################
-# Functions taken from pathutils.py Version 0.2.5 (2005/12/06), http://www.voidspace.org.uk/python/recipebook.shtml#utils
-# Copyright Michael Foord 2004
-# Released subject to the BSD License
-# Please see http://www.voidspace.org.uk/python/license.shtml
-
-###############################################################
-# formatbytes takes a filesize (as returned by os.getsize() )
-# and formats it for display in one of two ways !!
-
-# For information about bugfixes, updates and support, please join the Pythonutils mailing list.
-# http://groups.google.com/group/pythonutils/
-# Comments, suggestions and bug reports welcome.
-# Scripts maintained at http://www.voidspace.org.uk/python/index.shtml
-# E-mail fuzzyman@voidspace.org.uk
-
-def formatbytes(sizeint, configdict=None, **configs):
-    """
-    Given a file size as an integer, return a nicely formatted string that
-    represents the size. Has various options to control it's output.
-    
-    You can pass in a dictionary of arguments or keyword arguments. Keyword
-    arguments override the dictionary and there are sensible defaults for options
-    you don't set.
-    
-    Options and defaults are as follows :
-    
-    *    ``forcekb = False`` -         If set this forces the output to be in terms
-    of kilobytes and bytes only.
-    
-    *    ``largestonly = True`` -    If set, instead of outputting 
-        ``1 Mbytes, 307 Kbytes, 478 bytes`` it outputs using only the largest 
-        denominator - e.g. ``1.3 Mbytes`` or ``17.2 Kbytes``
-    
-    *    ``kiloname = 'Kbytes'`` -    The string to use for kilobytes
-    
-    *    ``meganame = 'Mbytes'`` - The string to use for Megabytes
-    
-    *    ``bytename = 'bytes'`` -     The string to use for bytes
-    
-    *    ``nospace = True`` -        If set it outputs ``1Mbytes, 307Kbytes``, 
-        notice there is no space.
-    
-    Example outputs : ::
-    
-        19Mbytes, 75Kbytes, 255bytes
-        2Kbytes, 0bytes
-        23.8Mbytes
-    
-    .. note::
-    
-        It currently uses the plural form even for singular.
-    """
-    defaultconfigs = {  'forcekb' : False,
-                        'largestonly' : True,
-                        'kiloname' : 'Kbytes',
-                        'meganame' : 'Mbytes',
-                        'bytename' : 'bytes',
-                        'nospace' : True}
-    if configdict is None:
-        configdict = {}
-    for entry in configs:
-        # keyword parameters override the dictionary passed in
-        configdict[entry] = configs[entry]
-    #
-    for keyword in defaultconfigs:
-        if not configdict.has_key(keyword):
-            configdict[keyword] = defaultconfigs[keyword]
-    #
-    if configdict['nospace']:
-        space = ''
-    else:
-        space = ' '
-    #
-    mb, kb, rb = bytedivider(sizeint)
-    if configdict['largestonly']:
-        if mb and not configdict['forcekb']:
-            return stringround(mb, kb)+ space + configdict['meganame']
-        elif kb or configdict['forcekb']:
-            if mb and configdict['forcekb']:
-                kb += 1024*mb
-            return stringround(kb, rb) + space+ configdict['kiloname']
-        else:
-            return str(rb) + space + configdict['bytename']
-    else:
-        outstr = ''
-        if mb and not configdict['forcekb']:
-            outstr = str(mb) + space + configdict['meganame'] +', '
-        if kb or configdict['forcekb'] or mb:
-            if configdict['forcekb']:
-                kb += 1024*mb 
-            outstr += str(kb) + space + configdict['kiloname'] +', '
-        return outstr + str(rb) + space + configdict['bytename']
-
-def stringround(main, rest):
-    """
-    Given a file size in either (mb, kb) or (kb, bytes) - round it
-    appropriately.
-    """
-    # divide an int by a float... get a float
-    value = main + rest/1024.0
-    return str(round(value, 1))
-
-def bytedivider(nbytes):
-    """
-    Given an integer (probably a long integer returned by os.getsize() )
-    it returns a tuple of (megabytes, kilobytes, bytes).
-    
-    This can be more easily converted into a formatted string to display the
-    size of the file.
-    """ 
-    mb, remainder = divmod(nbytes, 1048576)
-    kb, rb = divmod(remainder, 1024)
-    return (mb, kb, rb)
-
-