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

Source Code for Module flumotion.common.formatting

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