1# Copyright (C) 2010, 2012 Google Inc. All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
7#     * Redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer.
9#     * Redistributions in binary form must reproduce the above
10# copyright notice, this list of conditions and the following disclaimer
11# in the documentation and/or other materials provided with the
12# distribution.
13#     * Neither the name of Google Inc. nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29import logging
30import os
31import sys
32import time
33
34LOG_HANDLER_NAME = 'MeteredStreamLogHandler'
35
36
37class MeteredStream(object):
38    """
39    This class implements a stream wrapper that has 'meters' as well as
40    regular output. A 'meter' is a single line of text that can be erased
41    and rewritten repeatedly, without producing multiple lines of output. It
42    can be used to produce effects like progress bars.
43    """
44
45    @staticmethod
46    def _erasure(txt):
47        num_chars = len(txt)
48        return '\b' * num_chars + ' ' * num_chars + '\b' * num_chars
49
50    @staticmethod
51    def _ensure_newline(txt):
52        return txt if txt.endswith('\n') else txt + '\n'
53
54    def __init__(self, stream=None, verbose=False, logger=None, time_fn=None, pid=None, number_of_columns=None):
55        self._stream = stream or sys.stderr
56        self._verbose = verbose
57        self._time_fn = time_fn or time.time
58        self._pid = pid or os.getpid()
59        self._isatty = self._stream.isatty()
60        self._erasing = self._isatty and not verbose
61        self._last_partial_line = ''
62        self._last_write_time = 0.0
63        self._throttle_delay_in_secs = 0.066 if self._erasing else 10.0
64        self._number_of_columns = sys.maxint
65        if self._isatty and number_of_columns:
66            self._number_of_columns = number_of_columns
67
68        self._logger = logger
69        self._log_handler = None
70        if self._logger:
71            log_level = logging.DEBUG if verbose else logging.INFO
72            self._log_handler = _LogHandler(self)
73            self._log_handler.setLevel(log_level)
74            self._logger.addHandler(self._log_handler)
75
76    def __del__(self):
77        self.cleanup()
78
79    def cleanup(self):
80        if self._logger:
81            self._logger.removeHandler(self._log_handler)
82            self._log_handler = None
83
84    def write_throttled_update(self, txt):
85        now = self._time_fn()
86        if now - self._last_write_time >= self._throttle_delay_in_secs:
87            self.write_update(txt, now)
88
89    def write_update(self, txt, now=None):
90        self.write(txt, now)
91        if self._erasing:
92            self._last_partial_line = txt[txt.rfind('\n') + 1:]
93
94    def write(self, txt, now=None, pid=None):
95        now = now or self._time_fn()
96        pid = pid or self._pid
97        self._last_write_time = now
98        if self._last_partial_line:
99            self._erase_last_partial_line()
100        if self._verbose:
101            now_tuple = time.localtime(now)
102            msg = '%02d:%02d:%02d.%03d %d %s' % (now_tuple.tm_hour, now_tuple.tm_min, now_tuple.tm_sec, int((now * 1000) % 1000), pid, self._ensure_newline(txt))
103        elif self._isatty:
104            msg = txt
105        else:
106            msg = self._ensure_newline(txt)
107
108        self._stream.write(msg)
109
110    def writeln(self, txt, now=None, pid=None):
111        self.write(self._ensure_newline(txt), now, pid)
112
113    def _erase_last_partial_line(self):
114        num_chars = len(self._last_partial_line)
115        self._stream.write(self._erasure(self._last_partial_line))
116        self._last_partial_line = ''
117
118    def flush(self):
119        if self._last_partial_line:
120            self._stream.write('\n')
121            self._last_partial_line = ''
122            self._stream.flush()
123
124    def number_of_columns(self):
125        return self._number_of_columns
126
127
128class _LogHandler(logging.Handler):
129    def __init__(self, meter):
130        logging.Handler.__init__(self)
131        self._meter = meter
132        self.name = LOG_HANDLER_NAME
133
134    def emit(self, record):
135        self._meter.writeln(record.getMessage(), record.created, record.process)
136