1 #!/usr/bin/env python 2 # Copyright (C) 2010 Google Inc. All rights reserved. 3 # 4 # Redistribution and use in source and binary forms, with or without 5 # modification, are permitted provided that the following conditions are 6 # met: 7 # 8 # * Redistributions of source code must retain the above copyright 9 # notice, this list of conditions and the following disclaimer. 10 # * Redistributions in binary form must reproduce the above 11 # copyright notice, this list of conditions and the following disclaimer 12 # in the documentation and/or other materials provided with the 13 # distribution. 14 # * Neither the name of Google Inc. nor the names of its 15 # contributors may be used to endorse or promote products derived from 16 # this software without specific prior written permission. 17 # 18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 30 """ 31 Package that implements a stream wrapper that has 'meters' as well as 32 regular output. A 'meter' is a single line of text that can be erased 33 and rewritten repeatedly, without producing multiple lines of output. It 34 can be used to produce effects like progress bars. 35 """ 36 37 38 class MeteredStream: 39 """This class is a wrapper around a stream that allows you to implement 40 meters. 41 42 It can be used like a stream, but calling update() will print 43 the string followed by only a carriage return (instead of a carriage 44 return and a line feed). This can be used to implement progress bars and 45 other sorts of meters. Note that anything written by update() will be 46 erased by a subsequent update(), write(), or flush().""" 47 48 def __init__(self, verbose, stream): 49 """ 50 Args: 51 verbose: whether update is a no-op 52 stream: output stream to write to 53 """ 54 self._dirty = False 55 self._verbose = verbose 56 self._stream = stream 57 self._last_update = "" 58 59 def write(self, txt): 60 """Write text directly to the stream, overwriting and resetting the 61 meter.""" 62 if self._dirty: 63 self.update("") 64 self._dirty = False 65 self._stream.write(txt) 66 67 def flush(self): 68 """Flush any buffered output.""" 69 self._stream.flush() 70 71 def update(self, str): 72 """Write an update to the stream that will get overwritten by the next 73 update() or by a write(). 74 75 This is used for progress updates that don't need to be preserved in 76 the log. Note that verbose disables this routine; we have this in 77 case we are logging lots of output and the update()s will get lost 78 or won't work properly (typically because verbose streams are 79 redirected to files. 80 81 TODO(dpranke): figure out if there is a way to detect if we're writing 82 to a stream that handles CRs correctly (e.g., terminals). That might 83 be a cleaner way of handling this. 84 """ 85 if self._verbose: 86 return 87 88 # Print the necessary number of backspaces to erase the previous 89 # message. 90 self._stream.write("\b" * len(self._last_update)) 91 self._stream.write(str) 92 num_remaining = len(self._last_update) - len(str) 93 if num_remaining > 0: 94 self._stream.write(" " * num_remaining + "\b" * num_remaining) 95 self._last_update = str 96 self._dirty = True 97