9 class ProgressBar(object):
10 """ProgressBar class holds the options of the progress bar.
12 start State from which start the progress. For example, if start is
13 5 and the end is 10, the progress of this state is 50%
14 end State in which the progress has terminated.
16 fill String to use for "filled" used to represent the progress
17 blank String to use for "filled" used to represent remaining space.
21 light_block
= chr(0x2591).encode("utf-8")
22 solid_block
= chr(0x2588).encode("utf-8")
23 solid_right_arrow
= chr(0x25BA).encode("utf-8")
29 fill
=chr(0x25C9).encode("utf-8"),
30 blank
=chr(0x25CC).encode("utf-8"),
31 marker
=chr(0x25CE).encode("utf-8"),
32 format
='[%(fill)s%(marker)s%(blank)s] %(progress)s%%',
34 super(ProgressBar
, self
).__init
__()
43 self
.incremental
= incremental
44 self
.step
= 100 / float(width
) # fix
47 def __add__(self
, increment
):
48 increment
= self
._get
_progress
(increment
)
49 if 100 > self
.progress
+ increment
:
50 self
.progress
+= increment
60 progressed
= int(self
.progress
/ self
.step
) # fix
61 fill
= progressed
* self
.fill
62 blank
= (self
.width
- progressed
) * self
.blank
63 return self
.format
% {
66 'marker': self
.marker
,
72 def _get_progress(self
, increment
):
73 return float(increment
* 100) / self
.end
76 """Resets the current progress to the start point"""
77 self
.progress
= self
._get
_progress
(self
.start
)
81 class AnimatedProgressBar(ProgressBar
):
82 """Extends ProgressBar to allow you to use it straighforward on a script.
83 Accepts an extra keyword argument named `stdout` (by default use sys.stdout)
84 and may be any file-object to which send the progress status.
91 fill
=chr(0x25C9).encode("utf-8"),
92 blank
=chr(0x25CC).encode("utf-8"),
93 marker
=chr(0x25CE).encode("utf-8"),
94 format
='[%(fill)s%(marker)s%(blank)s] %(progress)s%%',
110 def show_progress(self
):
111 if hasattr(self
.stdout
, 'isatty') and self
.stdout
.isatty():
112 self
.stdout
.write('\r')
114 self
.stdout
.write('\n')
115 self
.stdout
.write(str(self
))
119 class ProgressWithEvents(AnimatedProgressBar
):
120 """Extends AnimatedProgressBar to allow you to track a set of events that
121 cause the progress to move. For instance, in a deletion progress bar, you
122 can track files that were nuked and files that the user doesn't have access to
129 fill
=chr(0x25C9).encode("utf-8"),
130 blank
=chr(0x25CC).encode("utf-8"),
131 marker
=chr(0x25CE).encode("utf-8"),
132 format
='[%(fill)s%(marker)s%(blank)s] %(progress)s%%',
149 def add_event(self
, event
):
150 if event
in self
.events
:
151 self
.events
[event
] += 1
153 self
.events
[event
] = 1
155 def show_progress(self
):
156 isatty
= hasattr(self
.stdout
, 'isatty') and self
.stdout
.isatty()
158 self
.stdout
.write('\r')
160 self
.stdout
.write('\n')
161 self
.stdout
.write(str(self
))
162 if len(self
.events
) == 0:
164 self
.stdout
.write('\n')
165 for key
in list(self
.events
.keys()):
166 self
.stdout
.write(str(key
) + ' = ' + str(self
.events
[key
]) + ' ')
168 self
.stdout
.write('\033[1A')
172 if __name__
== '__main__':
173 p
= AnimatedProgressBar(end
=200, width
=200)
179 if p
.progress
== 100: