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