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: 6969 $" 
 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 -def formatTime(seconds, fractional=0):
72 """ 73 Nicely format time in a human-readable format. 74 Will chunks weeks, days, hours and minutes. 75 76 @param seconds: the time in seconds to format. 77 @type seconds: int or float 78 @param fractional: how many digits to show for the fractional part. 79 @type fractional: int 80 81 @rtype: string 82 @returns: a nicely formatted time string. 83 """ 84 chunks = [] 85 86 week = 60 * 60 * 24 * 7 87 weeks = seconds / week 88 seconds %= week 89 90 day = 60 * 60 * 24 91 days = seconds / day 92 seconds %= day 93 94 hour = 60 * 60 95 hours = seconds / hour 96 seconds %= hour 97 98 minute = 60 99 minutes = seconds / minute 100 seconds %= minute 101 102 if weeks >= 1: 103 chunks.append(gettext.dngettext( 104 configure.PACKAGE, 105 N_('%d week'), N_('%d weeks'), weeks) % weeks) 106 if days >= 1: 107 chunks.append(gettext.dngettext( 108 configure.PACKAGE, 109 N_('%d day'), N_('%d days'), days) % days) 110 111 chunk = _('%02d:%02d') % (hours, minutes) 112 if fractional > 0: 113 chunk += ':%0*.*f' % (fractional + 3, fractional, seconds) 114 115 chunks.append(chunk) 116 return " ".join(chunks)
117
118 -def formatTimeStamp(timeOrTuple):
119 """ 120 Format a timestamp in a human-readable format. 121 122 @param timeOrTuple: the timestamp to format 123 @type timeOrTuple: something that time.strftime will accept 124 125 @rtype: string 126 @returns: a nicely formatted timestamp string. 127 """ 128 return time.strftime("%Y-%m-%d %H:%M %Z", timeOrTuple)
129 130
131 -def strftime(format, t):
132 """A version of time.strftime that can handle unicode formats. 133 @param format: format to convert, see man strftime(3) 134 @param t: time tuple as returned by time.localtime() 135 """ 136 out = [] 137 percent = False 138 for c in format: 139 if percent: 140 out.append(time.strftime('%' + c, t)) 141 percent = False 142 elif c == '%': 143 percent = True 144 else: 145 out.append(c) 146 if percent: 147 out.append('%') 148 return ''.join(out)
149