3 from __future__
import print_function
12 class ProgressBar(object):
13 """ProgressBar class holds the options of the progress bar.
15 start State from which start the progress. For example, if start is
16 5 and the end is 10, the progress of this state is 50%
17 end State in which the progress has terminated.
19 fill String to use for "filled" used to represent the progress
20 blank String to use for "filled" used to represent remaining space.
24 light_block
= six
.unichr(0x2591).encode("utf-8")
25 solid_block
= six
.unichr(0x2588).encode("utf-8")
26 solid_right_arrow
= six
.unichr(0x25BA).encode("utf-8")
32 fill
=six
.unichr(0x25C9).encode("utf-8"),
33 blank
=six
.unichr(0x25CC).encode("utf-8"),
34 marker
=six
.unichr(0x25CE).encode("utf-8"),
35 format
='[%(fill)s%(marker)s%(blank)s] %(progress)s%%',
37 super(ProgressBar
, self
).__init
__()
46 self
.incremental
= incremental
47 self
.step
= 100 / float(width
) # fix
50 def __add__(self
, increment
):
51 increment
= self
._get
_progress
(increment
)
52 if 100 > self
.progress
+ increment
:
53 self
.progress
+= increment
63 progressed
= int(self
.progress
/ self
.step
) # fix
64 fill
= progressed
* self
.fill
65 blank
= (self
.width
- progressed
) * self
.blank
66 return self
.format
% {
69 'marker': self
.marker
,
75 def _get_progress(self
, increment
):
76 return float(increment
* 100) / self
.end
79 """Resets the current progress to the start point"""
80 self
.progress
= self
._get
_progress
(self
.start
)
84 class AnimatedProgressBar(ProgressBar
):
85 """Extends ProgressBar to allow you to use it straighforward on a script.
86 Accepts an extra keyword argument named `stdout` (by default use sys.stdout)
87 and may be any file-object to which send the progress status.
94 fill
=six
.unichr(0x25C9).encode("utf-8"),
95 blank
=six
.unichr(0x25CC).encode("utf-8"),
96 marker
=six
.unichr(0x25CE).encode("utf-8"),
97 format
='[%(fill)s%(marker)s%(blank)s] %(progress)s%%',
113 def show_progress(self
):
114 if hasattr(self
.stdout
, 'isatty') and self
.stdout
.isatty():
115 self
.stdout
.write('\r')
117 self
.stdout
.write('\n')
118 self
.stdout
.write(str(self
))
122 class ProgressWithEvents(AnimatedProgressBar
):
123 """Extends AnimatedProgressBar to allow you to track a set of events that
124 cause the progress to move. For instance, in a deletion progress bar, you
125 can track files that were nuked and files that the user doesn't have access to
132 fill
=six
.unichr(0x25C9).encode("utf-8"),
133 blank
=six
.unichr(0x25CC).encode("utf-8"),
134 marker
=six
.unichr(0x25CE).encode("utf-8"),
135 format
='[%(fill)s%(marker)s%(blank)s] %(progress)s%%',
152 def add_event(self
, event
):
153 if event
in self
.events
:
154 self
.events
[event
] += 1
156 self
.events
[event
] = 1
158 def show_progress(self
):
159 isatty
= hasattr(self
.stdout
, 'isatty') and self
.stdout
.isatty()
161 self
.stdout
.write('\r')
163 self
.stdout
.write('\n')
164 self
.stdout
.write(str(self
))
165 if len(self
.events
) == 0:
167 self
.stdout
.write('\n')
168 for key
in list(self
.events
.keys()):
169 self
.stdout
.write(str(key
) + ' = ' + str(self
.events
[key
]) + ' ')
171 self
.stdout
.write('\033[1A')
175 if __name__
== '__main__':
176 p
= AnimatedProgressBar(end
=200, width
=200)
182 if p
.progress
== 100: