~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to patches/progress.diff

  • Committer: Martin Pool
  • Date: 2005-06-10 06:34:26 UTC
  • Revision ID: mbp@sourcefrog.net-20050610063426-cfcf5c0f96c271ec
- split out updated progress indicator

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
*** added file 'bzrlib/progress.py'
2
2
--- /dev/null 
3
3
+++ bzrlib/progress.py 
4
 
@@ -0,0 +1,91 @@
 
4
@@ -0,0 +1,138 @@
5
5
+# Copyright (C) 2005 Aaron Bentley
6
6
+# <aaron.bentley@utoronto.ca>
7
7
+#
20
20
+#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
21
+
22
22
+import sys
 
23
+import datetime
23
24
+
24
25
+class Progress(object):
25
26
+    def __init__(self, units, current, total=None):
26
27
+        self.units = units
27
28
+        self.current = current
28
29
+        self.total = total
29
 
+        self.percent = None
30
 
+        if self.total is not None:
31
 
+            self.percent = 100.0 * current / total
 
30
+
 
31
+    def _get_percent(self):
 
32
+        if self.total is not None and self.current is not None:
 
33
+            return 100.0 * self.current / self.total
 
34
+
 
35
+    percent = property(_get_percent)
32
36
+
33
37
+    def __str__(self):
34
38
+        if self.total is not None:
37
41
+        else:
38
42
+            return "%i %s" (self.current, self.units) 
39
43
+
40
 
+
41
 
+def progress_bar(progress):
 
44
+class ProgressBar(object):
 
45
+    def __init__(self):
 
46
+        self.start = None
 
47
+        object.__init__(self)
 
48
+
 
49
+    def __call__(self, progress):
 
50
+        if self.start is None:
 
51
+            self.start = datetime.datetime.now()
 
52
+        progress_bar(progress, start_time=self.start)
 
53
+        
 
54
+def divide_timedelta(delt, divisor):
 
55
+    """Divides a timedelta object"""
 
56
+    return datetime.timedelta(float(delt.days)/divisor, 
 
57
+                              float(delt.seconds)/divisor, 
 
58
+                              float(delt.microseconds)/divisor)
 
59
+
 
60
+def str_tdelta(delt):
 
61
+    if delt is None:
 
62
+        return "-:--:--"
 
63
+    return str(datetime.timedelta(delt.days, delt.seconds))
 
64
+
 
65
+def get_eta(start_time, progress, enough_samples=20):
 
66
+    if start_time is None or progress.current == 0:
 
67
+        return None
 
68
+    elif progress.current < enough_samples:
 
69
+        return None
 
70
+    elapsed = datetime.datetime.now() - start_time
 
71
+    total_duration = divide_timedelta((elapsed) * long(progress.total), 
 
72
+                                      progress.current)
 
73
+    if elapsed < total_duration:
 
74
+        eta = total_duration - elapsed
 
75
+    else:
 
76
+        eta = total_duration - total_duration
 
77
+    return eta
 
78
+
 
79
+def progress_bar(progress, start_time=None):
 
80
+    eta = get_eta(start_time, progress)
 
81
+    if start_time is not None:
 
82
+        eta_str = " "+str_tdelta(eta)
 
83
+    else:
 
84
+        eta_str = ""
 
85
+
42
86
+    fmt = " %i of %i %s (%.1f%%)"
43
87
+    f = fmt % (progress.total, progress.total, progress.units, 100.0)
44
88
+    max = len(f)
45
89
+    cols = 77 - max
 
90
+    if start_time is not None:
 
91
+        cols -= len(eta_str)
46
92
+    markers = int (float(cols) * progress.current / progress.total)
47
93
+    txt = fmt % (progress.current, progress.total, progress.units,
48
94
+                 progress.percent)
49
 
+    sys.stderr.write("\r[%s%s]%s" % ('='*markers, ' '*(cols-markers), txt))
 
95
+    sys.stderr.write("\r[%s%s]%s%s" % ('='*markers, ' '*(cols-markers), txt, 
 
96
+                                       eta_str))
50
97
+
51
98
+def clear_progress_bar():
52
99
+    sys.stderr.write('\r%s\r' % (' '*79))