16
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
21
class Progress(object):
21
22
def __init__(self, units, current, total=None):
34
35
return "%i %s" (self.current, self.units)
37
def progress_bar(progress):
37
class ProgressBar(object):
42
def __call__(self, progress):
43
if self.start is None:
44
self.start = datetime.datetime.now()
45
progress_bar(progress, start_time=self.start)
47
def divide_timedelta(delt, divisor):
48
"""Divides a timedelta object"""
49
return datetime.timedelta(float(delt.days)/divisor,
50
float(delt.seconds)/divisor,
51
float(delt.microseconds)/divisor)
56
return str(datetime.timedelta(delt.days, delt.seconds))
58
def get_eta(start_time, progress, enough_samples=20):
59
if start_time is None or progress.current == 0:
61
elif progress.current < enough_samples:
63
elapsed = datetime.datetime.now() - start_time
64
total_duration = divide_timedelta((elapsed) * long(progress.total),
66
if elapsed < total_duration:
67
eta = total_duration - elapsed
69
eta = total_duration - total_duration
72
def progress_bar(progress, start_time=None):
73
eta = get_eta(start_time, progress)
74
if start_time is not None:
75
eta_str = " "+str_tdelta(eta)
38
79
fmt = " %i of %i %s (%.1f%%)"
39
80
f = fmt % (progress.total, progress.total, progress.units, 100.0)
83
if start_time is not None:
42
85
markers = int (float(cols) * progress.current / progress.total)
43
86
txt = fmt % (progress.current, progress.total, progress.units,
45
sys.stderr.write("\r[%s%s]%s" % ('='*markers, ' '*(cols-markers), txt))
88
sys.stderr.write("\r[%s%s]%s%s" % ('='*markers, ' '*(cols-markers), txt,
47
91
def clear_progress_bar():
48
92
sys.stderr.write('\r%s\r' % (' '*79))