1# Copyright 2014 Google Inc. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
16class Stats(object):
17
18    def __init__(self, status_format, time_fn, size):
19        self.fmt = status_format
20        self.finished = 0
21        self.started = 0
22        self.total = 0
23        self.started_time = time_fn()
24        self._times = []
25        self._size = size
26        self._time = time_fn
27        self._times.append(self.started_time)
28
29    def add_time(self):
30        if len(self._times) > self._size:
31            self._times.pop(0)
32        self._times.append(self._time())
33
34    def format(self):
35        # Too many statements pylint: disable=R0915
36        out = ''
37        p = 0
38        end = len(self.fmt)
39        while p < end:
40            c = self.fmt[p]
41            if c == '%' and p < end - 1:
42                cn = self.fmt[p + 1]
43                if cn == 'c':
44                    elapsed = self._times[-1] - self._times[0]
45                    if elapsed > 0:
46                        out += '%5.1f' % ((len(self._times) - 1) / elapsed)
47                    else:
48                        out += '-'
49                elif cn == 'e':
50                    now = self._time()
51                    assert now >= self.started_time
52                    out += '%-5.3f' % (now - self.started_time)
53                elif cn == 'f':
54                    out += str(self.finished)
55                elif cn == 'o':
56                    now = self._time()
57                    if now > self.started_time:
58                        out += '%5.1f' % (self.finished * 1.0 /
59                                          (now - self.started_time))
60                    else:
61                        out += '-'
62                elif cn == 'p':
63                    if self.total:
64                        out += '%5.1f' % (self.started * 100.0 / self.total)
65                    else:
66                        out += '-'
67                elif cn == 'r':
68                    out += str(self.started - self.finished)
69                elif cn == 's':
70                    out += str(self.started)
71                elif cn == 't':
72                    out += str(self.total)
73                elif cn == 'u':
74                    out += str(self.total - self.finished)
75                elif cn == '%':
76                    out += '%'
77                else:
78                    out += c + cn
79                p += 2
80            else:
81                out += c
82                p += 1
83        return out
84