Package flumotion :: Package common :: Module format
[hide private]

Source Code for Module flumotion.common.format

  1  # -*- Mode: Python; test-case-name: flumotion.test.test_common_format -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007,2008 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  """formatting functions for storage, time, etc 
 23  """ 
 24   
 25  import gettext 
 26  import locale 
 27  import sys 
 28  import time 
 29   
 30  from flumotion.common.i18n import N_ 
 31  from flumotion.configure import configure 
 32   
 33  _ = gettext.gettext 
 34  __version__ = "$Rev$" 
 35   
 36   
37 -def formatStorage(units, precision=2):
38 """ 39 Nicely formats a storage size using SI units. 40 See Wikipedia and other sources for rationale. 41 Prefixes are k, M, G, ... 42 Sizes are powers of 10. 43 Actual result should be suffixed with bit or byte, not b or B. 44 45 @param units: the unit size to format 46 @type units: int or float 47 @param precision: the number of floating point digits to use 48 @type precision: int 49 50 @rtype: string 51 @returns: value of units, formatted using SI scale and the given precision 52 """ 53 54 # XXX: We might end up calling float(), which breaks 55 # when using LC_NUMERIC when it is not C -- only in python 56 # 2.3 though, no prob in 2.4. See PEP 331 57 if sys.version_info < (2, 4): 58 locale.setlocale(locale.LC_NUMERIC, "C") 59 60 prefixes = ['E', 'P', 'T', 'G', 'M', 'k', ''] 61 62 value = float(units) 63 prefix = prefixes.pop() 64 while prefixes and value >= 1000: 65 prefix = prefixes.pop() 66 value /= 1000 67 68 format = "%%.%df %%s" % precision 69 return format % (value, prefix)
70 71
72 -def formatTime(seconds, fractional=0):
73 """ 74 Nicely format time in a human-readable format, like 75 5 days 3 weeks HH:MM 76 77 If fractional is zero, no seconds will be shown. 78 If it is greater than 0, we will show seconds and fractions of seconds. 79 As a side consequence, there is no way to show seconds without fractions) 80 81 @param seconds: the time in seconds to format. 82 @type seconds: int or float 83 @param fractional: how many digits to show for the fractional part of 84 seconds. 85 @type fractional: int 86 87 @rtype: string 88 @returns: a nicely formatted time string. 89 """ 90 chunks = [] 91 92 if seconds < 0: 93 chunks.append(('-')) 94 seconds = -seconds 95 96 week = 60 * 60 * 24 * 7 97 weeks = seconds / week 98 seconds %= week 99 100 day = 60 * 60 * 24 101 days = seconds / day 102 seconds %= day 103 104 hour = 60 * 60 105 hours = seconds / hour 106 seconds %= hour 107 108 minute = 60 109 minutes = seconds / minute 110 seconds %= minute 111 112 if weeks >= 1: 113 chunks.append(gettext.dngettext( 114 configure.PACKAGE, 115 N_('%d week'), N_('%d weeks'), weeks) % weeks) 116 if days >= 1: 117 chunks.append(gettext.dngettext( 118 configure.PACKAGE, 119 N_('%d day'), N_('%d days'), days) % days) 120 121 chunk = _('%02d:%02d') % (hours, minutes) 122 if fractional > 0: 123 chunk += ':%0*.*f' % (fractional + 3, fractional, seconds) 124 125 chunks.append(chunk) 126 127 return " ".join(chunks)
128 129
130 -def formatTimeStamp(timeOrTuple):
131 """ 132 Format a timestamp in a human-readable format. 133 134 @param timeOrTuple: the timestamp to format 135 @type timeOrTuple: something that time.strftime will accept 136 137 @rtype: string 138 @returns: a nicely formatted timestamp string. 139 """ 140 return time.strftime("%Y-%m-%d %H:%M %Z", timeOrTuple)
141 142
143 -def strftime(format, t):
144 """A version of time.strftime that can handle unicode formats. 145 @param format: format to convert, see man strftime(3) 146 @param t: time tuple as returned by time.localtime() 147 """ 148 out = [] 149 percent = False 150 for c in format: 151 if percent: 152 out.append(time.strftime('%' + c, t)) 153 percent = False 154 elif c == '%': 155 percent = True 156 else: 157 out.append(c) 158 if percent: 159 out.append('%') 160 return ''.join(out)
161