bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
649
by Martin Pool
- some cleanups for the progressbar method |
1 |
# Copyright (C) 2005 Aaron Bentley <aaron.bentley@utoronto.ca>
|
2 |
# Copyright (C) 2005 Canonical <canonical.com>
|
|
|
648
by Martin Pool
- import aaron's progress-indicator code |
3 |
#
|
4 |
# This program is free software; you can redistribute it and/or modify
|
|
5 |
# it under the terms of the GNU General Public License as published by
|
|
6 |
# the Free Software Foundation; either version 2 of the License, or
|
|
7 |
# (at your option) any later version.
|
|
8 |
#
|
|
9 |
# This program is distributed in the hope that it will be useful,
|
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
# GNU General Public License for more details.
|
|
13 |
#
|
|
14 |
# You should have received a copy of the GNU General Public License
|
|
15 |
# along with this program; if not, write to the Free Software
|
|
16 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
17 |
||
|
649
by Martin Pool
- some cleanups for the progressbar method |
18 |
|
19 |
"""
|
|
20 |
Simple text-mode progress indicator.
|
|
21 |
||
22 |
Everyone loves ascii art!
|
|
23 |
||
24 |
To display an indicator, create a ProgressBar object. Call it,
|
|
25 |
passing Progress objects indicating the current state. When done,
|
|
26 |
call clear().
|
|
27 |
||
28 |
Progress is suppressed when output is not sent to a terminal, so as
|
|
29 |
not to clutter log files.
|
|
30 |
"""
|
|
31 |
||
32 |
# TODO: remove functions in favour of keeping everything in one class
|
|
33 |
||
34 |
||
|
648
by Martin Pool
- import aaron's progress-indicator code |
35 |
import sys |
36 |
import datetime |
|
37 |
||
|
649
by Martin Pool
- some cleanups for the progressbar method |
38 |
|
39 |
def _width(): |
|
40 |
"""Return estimated terminal width. |
|
41 |
||
42 |
TODO: Do something smart on Windows?
|
|
43 |
||
44 |
TODO: Is there anything that gets a better update when the window
|
|
45 |
is resized while the program is running?
|
|
46 |
"""
|
|
47 |
import os |
|
48 |
try: |
|
49 |
return int(os.environ['COLUMNS']) |
|
50 |
except (IndexError, KeyError, ValueError): |
|
51 |
return 80 |
|
52 |
||
53 |
||
54 |
def _supports_progress(f): |
|
55 |
return hasattr(f, 'isatty') and f.isatty() |
|
56 |
||
57 |
||
58 |
||
|
648
by Martin Pool
- import aaron's progress-indicator code |
59 |
class Progress(object): |
60 |
def __init__(self, units, current, total=None): |
|
61 |
self.units = units |
|
62 |
self.current = current |
|
63 |
self.total = total |
|
64 |
||
65 |
def _get_percent(self): |
|
66 |
if self.total is not None and self.current is not None: |
|
67 |
return 100.0 * self.current / self.total |
|
68 |
||
69 |
percent = property(_get_percent) |
|
70 |
||
71 |
def __str__(self): |
|
72 |
if self.total is not None: |
|
73 |
return "%i of %i %s %.1f%%" % (self.current, self.total, self.units, |
|
74 |
self.percent) |
|
75 |
else: |
|
|
649
by Martin Pool
- some cleanups for the progressbar method |
76 |
return "%i %s" (self.current, self.units) |
77 |
||
78 |
||
|
648
by Martin Pool
- import aaron's progress-indicator code |
79 |
|
80 |
class ProgressBar(object): |
|
|
649
by Martin Pool
- some cleanups for the progressbar method |
81 |
def __init__(self, to_file=sys.stderr): |
82 |
object.__init__(self) |
|
|
648
by Martin Pool
- import aaron's progress-indicator code |
83 |
self.start = None |
|
649
by Martin Pool
- some cleanups for the progressbar method |
84 |
self.to_file = to_file |
85 |
self.suppressed = not _supports_progress(self.to_file) |
|
86 |
||
|
648
by Martin Pool
- import aaron's progress-indicator code |
87 |
|
88 |
def __call__(self, progress): |
|
89 |
if self.start is None: |
|
90 |
self.start = datetime.datetime.now() |
|
|
649
by Martin Pool
- some cleanups for the progressbar method |
91 |
if not self.suppressed: |
92 |
draw_progress_bar(progress, start_time=self.start, |
|
93 |
to_file=self.to_file) |
|
94 |
||
95 |
def clear(self): |
|
96 |
if not self.suppressed: |
|
97 |
clear_progress_bar(self.to_file) |
|
98 |
||
99 |
||
|
648
by Martin Pool
- import aaron's progress-indicator code |
100 |
|
101 |
def divide_timedelta(delt, divisor): |
|
102 |
"""Divides a timedelta object""" |
|
103 |
return datetime.timedelta(float(delt.days)/divisor, |
|
104 |
float(delt.seconds)/divisor, |
|
105 |
float(delt.microseconds)/divisor) |
|
106 |
||
107 |
def str_tdelta(delt): |
|
108 |
if delt is None: |
|
109 |
return "-:--:--" |
|
110 |
return str(datetime.timedelta(delt.days, delt.seconds)) |
|
111 |
||
|
649
by Martin Pool
- some cleanups for the progressbar method |
112 |
|
|
648
by Martin Pool
- import aaron's progress-indicator code |
113 |
def get_eta(start_time, progress, enough_samples=20): |
114 |
if start_time is None or progress.current == 0: |
|
115 |
return None |
|
116 |
elif progress.current < enough_samples: |
|
117 |
return None |
|
118 |
elapsed = datetime.datetime.now() - start_time |
|
119 |
total_duration = divide_timedelta((elapsed) * long(progress.total), |
|
120 |
progress.current) |
|
121 |
if elapsed < total_duration: |
|
122 |
eta = total_duration - elapsed |
|
123 |
else: |
|
124 |
eta = total_duration - total_duration |
|
125 |
return eta |
|
126 |
||
|
649
by Martin Pool
- some cleanups for the progressbar method |
127 |
|
128 |
def draw_progress_bar(progress, start_time=None, to_file=sys.stderr): |
|
|
648
by Martin Pool
- import aaron's progress-indicator code |
129 |
eta = get_eta(start_time, progress) |
130 |
if start_time is not None: |
|
131 |
eta_str = " "+str_tdelta(eta) |
|
132 |
else: |
|
133 |
eta_str = "" |
|
134 |
||
135 |
fmt = " %i of %i %s (%.1f%%)" |
|
136 |
f = fmt % (progress.total, progress.total, progress.units, 100.0) |
|
|
649
by Martin Pool
- some cleanups for the progressbar method |
137 |
cols = _width() - 3 - len(f) |
|
648
by Martin Pool
- import aaron's progress-indicator code |
138 |
if start_time is not None: |
139 |
cols -= len(eta_str) |
|
140 |
markers = int (float(cols) * progress.current / progress.total) |
|
141 |
txt = fmt % (progress.current, progress.total, progress.units, |
|
142 |
progress.percent) |
|
|
649
by Martin Pool
- some cleanups for the progressbar method |
143 |
to_file.write("\r[%s%s]%s%s" % ('='*markers, ' '*(cols-markers), txt, |
|
648
by Martin Pool
- import aaron's progress-indicator code |
144 |
eta_str)) |
145 |
||
|
649
by Martin Pool
- some cleanups for the progressbar method |
146 |
def clear_progress_bar(to_file=sys.stderr): |
147 |
to_file.write('\r%s\r' % (' '*79)) |
|
148 |
||
|
648
by Martin Pool
- import aaron's progress-indicator code |
149 |
|
150 |
def spinner_str(progress, show_text=False): |
|
151 |
""" |
|
152 |
Produces the string for a textual "spinner" progress indicator
|
|
153 |
:param progress: an object represinting current progress
|
|
154 |
:param show_text: If true, show progress text as well
|
|
155 |
:return: The spinner string
|
|
156 |
||
157 |
>>> spinner_str(Progress("baloons", 0))
|
|
158 |
'|'
|
|
159 |
>>> spinner_str(Progress("baloons", 5))
|
|
160 |
'/'
|
|
161 |
>>> spinner_str(Progress("baloons", 6), show_text=True)
|
|
162 |
'- 6 baloons'
|
|
163 |
"""
|
|
164 |
positions = ('|', '/', '-', '\\') |
|
165 |
text = positions[progress.current % 4] |
|
166 |
if show_text: |
|
167 |
text+=" %i %s" % (progress.current, progress.units) |
|
168 |
return text |
|
169 |
||
|
649
by Martin Pool
- some cleanups for the progressbar method |
170 |
|
|
648
by Martin Pool
- import aaron's progress-indicator code |
171 |
def spinner(progress, show_text=False, output=sys.stderr): |
172 |
""" |
|
173 |
Update a spinner progress indicator on an output
|
|
174 |
:param progress: The progress to display
|
|
175 |
:param show_text: If true, show text as well as spinner
|
|
176 |
:param output: The output to write to
|
|
177 |
||
178 |
>>> spinner(Progress("baloons", 6), show_text=True, output=sys.stdout)
|
|
179 |
\r- 6 baloons
|
|
180 |
"""
|
|
181 |
output.write('\r%s' % spinner_str(progress, show_text)) |
|
182 |
||
|
649
by Martin Pool
- some cleanups for the progressbar method |
183 |
|
|
648
by Martin Pool
- import aaron's progress-indicator code |
184 |
def run_tests(): |
185 |
import doctest |
|
186 |
result = doctest.testmod() |
|
187 |
if result[1] > 0: |
|
188 |
if result[0] == 0: |
|
189 |
print "All tests passed" |
|
190 |
else: |
|
191 |
print "No tests to run" |
|
|
649
by Martin Pool
- some cleanups for the progressbar method |
192 |
|
193 |
||
194 |
def demo(): |
|
195 |
from time import sleep |
|
196 |
pb = ProgressBar() |
|
197 |
for i in range(100): |
|
198 |
pb(Progress('Elephanten', i, 100)) |
|
199 |
sleep(0.3) |
|
200 |
print 'done!' |
|
201 |
||
|
648
by Martin Pool
- import aaron's progress-indicator code |
202 |
if __name__ == "__main__": |
|
649
by Martin Pool
- some cleanups for the progressbar method |
203 |
demo() |