~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:29:35 UTC
  • Revision ID: mbp@sourcefrog.net-20050610062935-cd2fc37ca7ae1e09
- split out proposed progress module

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,138 @@
 
4
@@ -0,0 +1,91 @@
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
24
23
+
25
24
+class Progress(object):
26
25
+    def __init__(self, units, current, total=None):
27
26
+        self.units = units
28
27
+        self.current = current
29
28
+        self.total = 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)
 
29
+        self.percent = None
 
30
+        if self.total is not None:
 
31
+            self.percent = 100.0 * current / total
36
32
+
37
33
+    def __str__(self):
38
34
+        if self.total is not None:
41
37
+        else:
42
38
+            return "%i %s" (self.current, self.units) 
43
39
+
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
 
+
 
40
+
 
41
+def progress_bar(progress):
86
42
+    fmt = " %i of %i %s (%.1f%%)"
87
43
+    f = fmt % (progress.total, progress.total, progress.units, 100.0)
88
44
+    max = len(f)
89
45
+    cols = 77 - max
90
 
+    if start_time is not None:
91
 
+        cols -= len(eta_str)
92
46
+    markers = int (float(cols) * progress.current / progress.total)
93
47
+    txt = fmt % (progress.current, progress.total, progress.units,
94
48
+                 progress.percent)
95
 
+    sys.stderr.write("\r[%s%s]%s%s" % ('='*markers, ' '*(cols-markers), txt, 
96
 
+                                       eta_str))
 
49
+    sys.stderr.write("\r[%s%s]%s" % ('='*markers, ' '*(cols-markers), txt))
97
50
+
98
51
+def clear_progress_bar():
99
52
+    sys.stderr.write('\r%s\r' % (' '*79))