blob: 860d9d01125237f06d72c72c5dce94b325209e68 [file] [log] [blame]
nasser@codeaurora.org1f7a3d12010-02-04 15:11:50 +00001#
2# Copyright (C) 2009 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import sys
17from time import time
18
19class Progress(object):
20 def __init__(self, title, total=0):
21 self._title = title
22 self._total = total
23 self._done = 0
24 self._lastp = -1
25 self._start = time()
26 self._show = False
27
28 def update(self, inc=1):
29 self._done += inc
30
31 if not self._show:
32 if 0.5 <= time() - self._start:
33 self._show = True
34 else:
35 return
36
37 if self._total <= 0:
nasser@codeaurora.orgdc06a862010-03-15 15:07:17 +000038 sys.stdout.write('\r%s: %d, ' % (
nasser@codeaurora.org1f7a3d12010-02-04 15:11:50 +000039 self._title,
40 self._done))
nasser@codeaurora.orgdc06a862010-03-15 15:07:17 +000041 sys.stdout.flush()
nasser@codeaurora.org1f7a3d12010-02-04 15:11:50 +000042 else:
43 p = (100 * self._done) / self._total
44
45 if self._lastp != p:
46 self._lastp = p
nasser@codeaurora.orgdc06a862010-03-15 15:07:17 +000047 sys.stdout.write('\r%s: %3d%% (%d/%d) ' % (
nasser@codeaurora.org1f7a3d12010-02-04 15:11:50 +000048 self._title,
49 p,
50 self._done,
51 self._total))
nasser@codeaurora.orgdc06a862010-03-15 15:07:17 +000052 sys.stdout.flush()
nasser@codeaurora.org1f7a3d12010-02-04 15:11:50 +000053
54 def end(self):
55 if not self._show:
56 return
57
58 if self._total <= 0:
nasser@codeaurora.orgdc06a862010-03-15 15:07:17 +000059 sys.stdout.write('\r%s: %d, done. \n' % (
nasser@codeaurora.org1f7a3d12010-02-04 15:11:50 +000060 self._title,
61 self._done))
nasser@codeaurora.orgdc06a862010-03-15 15:07:17 +000062 sys.stdout.flush()
nasser@codeaurora.org1f7a3d12010-02-04 15:11:50 +000063 else:
64 p = (100 * self._done) / self._total
nasser@codeaurora.orgdc06a862010-03-15 15:07:17 +000065 sys.stdout.write('\r%s: %3d%% (%d/%d), done. \n' % (
nasser@codeaurora.org1f7a3d12010-02-04 15:11:50 +000066 self._title,
67 p,
68 self._done,
69 self._total))
nasser@codeaurora.orgdc06a862010-03-15 15:07:17 +000070 sys.stdout.flush()