Package translate :: Package misc :: Module progressbar
[hide private]
[frames] | no frames]

Source Code for Module translate.misc.progressbar

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  #  
  4  # Copyright 2004, 2005 Zuza Software Foundation 
  5  #  
  6  # This file is part of translate. 
  7  # 
  8  # translate is free software; you can redistribute it and/or modify 
  9  # it under the terms of the GNU General Public License as published by 
 10  # the Free Software Foundation; either version 2 of the License, or 
 11  # (at your option) any later version. 
 12  #  
 13  # translate is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU General Public License for more details. 
 17  # 
 18  # You should have received a copy of the GNU General Public License 
 19  # along with translate; if not, write to the Free Software 
 20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 21   
 22  """progress bar utilities for reporting feedback on progress of application...""" 
 23   
24 -class DotsProgressBar:
25 """an ultra-simple progress indicator that just writes a dot for each action"""
26 - def __init__(self):
27 import sys 28 self.stderr = sys.stderr 29 self.amount = 0
30
31 - def show(self, verbosemessage):
32 """show a dot for progress :-)""" 33 self.stderr.write('.') 34 self.stderr.flush()
35
36 - def close(self):
37 self.stderr.write('\n') 38 self.stderr.flush()
39
40 - def __del__(self):
41 self.close()
42
43 -class NoProgressBar:
44 """an invisible indicator that does nothing..."""
45 - def __init__(self):
46 self.amount = 0
47
48 - def show(self, verbosemessage):
49 """show nothing for progress :-)""" 50 pass
51
52 - def close(self):
53 pass
54
55 -class ProgressBar:
56 """a plain progress bar that doesn't know very much about output..."""
57 - def __init__(self, minValue = 0, maxValue = 100, totalWidth=50):
58 self.progBar = "[]" # This holds the progress bar string 59 self.min = minValue 60 self.max = maxValue 61 self.span = maxValue - minValue 62 self.width = totalWidth 63 self.amount = 0 # When amount == max, we are 100% done
64
65 - def __str__(self):
66 """produces the string representing the progress bar""" 67 if self.amount < self.min: self.amount = self.min 68 if self.amount > self.max: self.amount = self.max 69 70 # Figure out the new percent done, round to an integer 71 diffFromMin = float(self.amount - self.min) 72 percentDone = (diffFromMin / float(self.span)) * 100.0 73 percentDone = round(percentDone) 74 percentDone = int(percentDone) 75 76 # Figure out how many hash bars the percentage should be 77 allFull = self.width - 7 78 numHashes = (percentDone / 100.0) * allFull 79 numHashes = int(round(numHashes)) 80 81 # build a progress bar with hashes and spaces 82 self.progBar = "[%s%s] %3d%%" % ('#'*numHashes, ' '*(allFull-numHashes), percentDone) 83 return str(self.progBar)
84
85 - def show(self, verbosemessage):
86 """displays the progress bar""" 87 print self
88
89 -class MessageProgressBar(ProgressBar):
90 """a ProgressBar that just writes out the messages without any progress display"""
91 - def __init__(self, *args, **kwargs):
92 import sys 93 self.sys = sys 94 ProgressBar.__init__(self, *args, **kwargs)
95
96 - def show(self, verbosemessage):
97 self.sys.stderr.write(verbosemessage + '\n') 98 self.sys.stderr.flush()
99
100 -class HashProgressBar(ProgressBar):
101 """a ProgressBar which knows how to go back to the beginning of the line..."""
102 - def __init__(self, *args, **kwargs):
103 import sys 104 self.sys = sys 105 ProgressBar.__init__(self, *args, **kwargs)
106
107 - def show(self, verbosemessage):
108 self.sys.stderr.write(str(self) + '\r') 109 self.sys.stderr.flush()
110
111 - def close(self):
112 self.sys.stderr.write('\n') 113 self.sys.stderr.flush()
114
115 - def __del__(self):
116 self.close()
117
118 -class VerboseProgressBar(HashProgressBar):
119 - def __init__(self, *args, **kwargs):
120 self.lastwidth = 0 121 HashProgressBar.__init__(self, *args, **kwargs)
122
123 - def show(self, verbosemessage):
124 output = str(self) 125 self.sys.stderr.write('\r' + ' '*self.lastwidth) 126 self.sys.stderr.write('\r' + verbosemessage + '\n') 127 self.lastwidth = len(output) 128 self.sys.stderr.write('\r' + output) 129 self.sys.stderr.flush()
130
131 -def test(progressbar):
132 import time 133 for n in range(progressbar.min, progressbar.max+1, 5): 134 progressbar.amount = n 135 progressbar.show("Some message") 136 time.sleep(0.2)
137 138 if __name__ == '__main__': 139 p = HashProgressBar(0,100,50) 140 test(p) 141