OLD | NEW |
(Empty) | |
| 1 # |
| 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 |
| 16 import sys |
| 17 from time import time |
| 18 |
| 19 class 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: |
| 38 sys.stderr.write('\r%s: %d, ' % ( |
| 39 self._title, |
| 40 self._done)) |
| 41 sys.stderr.flush() |
| 42 else: |
| 43 p = (100 * self._done) / self._total |
| 44 |
| 45 if self._lastp != p: |
| 46 self._lastp = p |
| 47 sys.stderr.write('\r%s: %3d%% (%d/%d) ' % ( |
| 48 self._title, |
| 49 p, |
| 50 self._done, |
| 51 self._total)) |
| 52 sys.stderr.flush() |
| 53 |
| 54 def end(self): |
| 55 if not self._show: |
| 56 return |
| 57 |
| 58 if self._total <= 0: |
| 59 sys.stderr.write('\r%s: %d, done. \n' % ( |
| 60 self._title, |
| 61 self._done)) |
| 62 sys.stderr.flush() |
| 63 else: |
| 64 p = (100 * self._done) / self._total |
| 65 sys.stderr.write('\r%s: %3d%% (%d/%d), done. \n' % ( |
| 66 self._title, |
| 67 p, |
| 68 self._done, |
| 69 self._total)) |
| 70 sys.stderr.flush() |
OLD | NEW |