home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / BitTorrent / fmt.py < prev    next >
Encoding:
Text File  |  2006-07-20  |  1.1 KB  |  46 lines

  1. # Written by Michael Janssen
  2. # See LICENSE.txt for license information
  3.  
  4. def fmttime(n, compact = 0):
  5.     if n == -1:
  6.         if compact: 
  7.             return '(no seeds?)'
  8.         else: 
  9.             return 'download not progressing (no seeds?)'
  10.     if n == 0:
  11.         if compact: 
  12.             return "complete"
  13.         else: 
  14.             return 'download complete!'
  15.     n = int(n)
  16.     m, s = divmod(n, 60)
  17.     h, m = divmod(m, 60)
  18.     if h > 1000000:
  19.         return 'n/a'
  20.     if compact: 
  21.         return '%d:%02d:%02d' % (h, m, s)
  22.     else: 
  23.         return 'finishing in %d:%02d:%02d' % (h, m, s)
  24.  
  25. def fmtsize(n, baseunit = 0, padded = 1):
  26.     unit = [' B', ' K', ' M', ' G', ' T', ' P', ' E', ' Z', ' Y']
  27.     i = baseunit
  28.     while i + 1 < len(unit) and n >= 999:
  29.         i += 1
  30.         n = float(n) / (1 << 10)
  31.     size = ''
  32.     if padded:
  33.         if n < 10:
  34.             size = '  '
  35.         elif n < 100:
  36.             size = ' '
  37.     if i != 0:
  38.         size += '%.1f %s' % (n, unit[i])
  39.     else:
  40.         if padded:
  41.             size += '%.0f   %s' % (n, unit[i])
  42.         else:
  43.             size += '%.0f %s' % (n, unit[i])
  44.     return size
  45.  
  46.