translate.misc.progressbar

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          # pylint: disable-msg=W0613
     34          self.stderr.write('.')
     35          self.stderr.flush()
     36
     37 -    def close(self):
     38          self.stderr.write('\n')
     39          self.stderr.flush()
     40
     41 -    def __del__(self):
     42          self.close()
     43
     44 -class NoProgressBar:
     45      """an invisible indicator that does nothing..."""
     46 -    def __init__(self):
     47          self.amount = 0
     48
     49 -    def show(self, verbosemessage):
     50          """show nothing for progress :-)"""
     51          pass
     52
     53 -    def close(self):
     54          pass
     55
     56 -class ProgressBar:
     57      """a plain progress bar that doesn't know very much about output..."""
     58 -    def __init__(self, minValue = 0, maxValue = 100, totalWidth=50):
     59          self.progBar = "[]"   # This holds the progress bar string
     60          self.min = minValue
     61          self.max = maxValue
     62          self.span = maxValue - minValue
     63          self.width = totalWidth
     64          self.amount = 0       # When amount == max, we are 100% done
     65
     66 -    def __str__(self):
     67          """produces the string representing the progress bar"""
     68          if self.amount < self.min:
     69              self.amount = self.min
     70          if self.amount > self.max:
     71              self.amount = self.max
     72
     73          # Figure out the new percent done, round to an integer
     74          diffFromMin = float(self.amount - self.min)
     75          percentDone = (diffFromMin / float(self.span)) * 100.0
     76          percentDone = round(percentDone)
     77          percentDone = int(percentDone)
     78
     79          # Figure out how many hash bars the percentage should be
     80          allFull = self.width - 7
     81          numHashes = (percentDone / 100.0) * allFull
     82          numHashes = int(round(numHashes))
     83
     84          # build a progress bar with hashes and spaces
     85          self.progBar = "[%s%s] %3d%%" % ('#'*numHashes, ' '*(allFull-numHashes), percentDone)
     86          return str(self.progBar)
     87
     88 -    def show(self, verbosemessage):
     89          """displays the progress bar"""
     90          # pylint: disable-msg=W0613
     91          print self
     92
     93 -class MessageProgressBar(ProgressBar):
     94      """a ProgressBar that just writes out the messages without any progress display"""
     95 -    def __init__(self, *args, **kwargs):
     96          import sys
     97          self.sys = sys
     98          ProgressBar.__init__(self, *args, **kwargs)
     99
    100 -    def show(self, verbosemessage):
    101          self.sys.stderr.write(verbosemessage + '\n')
    102          self.sys.stderr.flush()
    103
    104 -class HashProgressBar(ProgressBar):
    105      """a ProgressBar which knows how to go back to the beginning of the line..."""
    106 -    def __init__(self, *args, **kwargs):
    107          import sys
    108          self.sys = sys
    109          ProgressBar.__init__(self, *args, **kwargs)
    110
    111 -    def show(self, verbosemessage):
    112          self.sys.stderr.write(str(self) + '\r')
    113          self.sys.stderr.flush()
    114
    115 -    def close(self):
    116          self.sys.stderr.write('\n')
    117          self.sys.stderr.flush()
    118
    119 -    def __del__(self):
    120          self.close()
    121
    122 -class VerboseProgressBar(HashProgressBar):
    123 -    def __init__(self, *args, **kwargs):
    124          self.lastwidth = 0
    125          HashProgressBar.__init__(self, *args, **kwargs)
    126
    127 -    def show(self, verbosemessage):
    128          output = str(self)
    129          self.sys.stderr.write('\r' + ' '*self.lastwidth)
    130          self.sys.stderr.write('\r' + verbosemessage + '\n')
    131          self.lastwidth = len(output)
    132          self.sys.stderr.write('\r' + output)
    133          self.sys.stderr.flush()
    134
    135 -def test(progressbar):
    136      import time
    137      for n in range(progressbar.min, progressbar.max+1, 5):
    138          progressbar.amount = n
    139          progressbar.show("Some message")
    140          time.sleep(0.2)
    141
    142  if __name__ == '__main__':
    143      p = HashProgressBar(0, 100, 50)
    144      test(p)
    145