summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/progressbar
diff options
context:
space:
mode:
authorPaul Eggleton <paul.eggleton@linux.intel.com>2016-06-23 22:59:04 +1200
committerRichard Purdie <richard.purdie@linuxfoundation.org>2016-07-08 09:57:26 +0100
commit1cf6e14a6c5ddb4daec1c1e0cce113eea8570545 (patch)
treeb24af53ea6048aa6d441b10cf96ea633a7936d5d /bitbake/lib/progressbar
parent481048cd2a64a08501bb5ea7ec0bc9ef0a9a5cc9 (diff)
downloadpoky-1cf6e14a6c5ddb4daec1c1e0cce113eea8570545.tar.gz
bitbake: knotty: import latest python-progressbar
Since we're going to make some minor extensions to it, it makes sense to bring in the latest version of python-progressbar. Its structure has changed a little but the API hasn't; however we do need to ensure our overridden _needs_update() function's signature in BBProgress() matches properly. (Bitbake rev: c3e51d71b36cbc9e9ed1b35fb93d0978e24bc98a) Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/progressbar')
-rw-r--r--bitbake/lib/progressbar/LICENSE.txt52
-rw-r--r--bitbake/lib/progressbar/__init__.py49
-rw-r--r--bitbake/lib/progressbar/compat.py44
-rw-r--r--bitbake/lib/progressbar/progressbar.py307
-rw-r--r--bitbake/lib/progressbar/widgets.py355
5 files changed, 807 insertions, 0 deletions
diff --git a/bitbake/lib/progressbar/LICENSE.txt b/bitbake/lib/progressbar/LICENSE.txt
new file mode 100644
index 0000000000..fc8ccdc1c1
--- /dev/null
+++ b/bitbake/lib/progressbar/LICENSE.txt
@@ -0,0 +1,52 @@
1You can redistribute and/or modify this library under the terms of the
2GNU LGPL license or BSD license (or both).
3
4---
5
6progressbar - Text progress bar library for python.
7Copyright (C) 2005 Nilton Volpato
8
9This library is free software; you can redistribute it and/or
10modify it under the terms of the GNU Lesser General Public
11License as published by the Free Software Foundation; either
12version 2.1 of the License, or (at your option) any later version.
13
14This library is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public
20License along with this library; if not, write to the Free Software
21Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
23---
24
25progressbar - Text progress bar library for python
26Copyright (c) 2008 Nilton Volpato
27
28All rights reserved.
29
30Redistribution and use in source and binary forms, with or without
31modification, are permitted provided that the following conditions are met:
32
33 a. Redistributions of source code must retain the above copyright notice,
34 this list of conditions and the following disclaimer.
35 b. Redistributions in binary form must reproduce the above copyright
36 notice, this list of conditions and the following disclaimer in the
37 documentation and/or other materials provided with the distribution.
38 c. Neither the name of the author nor the names of its contributors
39 may be used to endorse or promote products derived from this software
40 without specific prior written permission.
41
42THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
46ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
52DAMAGE.
diff --git a/bitbake/lib/progressbar/__init__.py b/bitbake/lib/progressbar/__init__.py
new file mode 100644
index 0000000000..fbab744ee2
--- /dev/null
+++ b/bitbake/lib/progressbar/__init__.py
@@ -0,0 +1,49 @@
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3#
4# progressbar - Text progress bar library for Python.
5# Copyright (c) 2005 Nilton Volpato
6#
7# This library is free software; you can redistribute it and/or
8# modify it under the terms of the GNU Lesser General Public
9# License as published by the Free Software Foundation; either
10# version 2.1 of the License, or (at your option) any later version.
11#
12# This library is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15# Lesser General Public License for more details.
16#
17# You should have received a copy of the GNU Lesser General Public
18# License along with this library; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
21"""Text progress bar library for Python.
22
23A text progress bar is typically used to display the progress of a long
24running operation, providing a visual cue that processing is underway.
25
26The ProgressBar class manages the current progress, and the format of the line
27is given by a number of widgets. A widget is an object that may display
28differently depending on the state of the progress bar. There are three types
29of widgets:
30 - a string, which always shows itself
31
32 - a ProgressBarWidget, which may return a different value every time its
33 update method is called
34
35 - a ProgressBarWidgetHFill, which is like ProgressBarWidget, except it
36 expands to fill the remaining width of the line.
37
38The progressbar module is very easy to use, yet very powerful. It will also
39automatically enable features like auto-resizing when the system supports it.
40"""
41
42__author__ = 'Nilton Volpato'
43__author_email__ = 'first-name dot last-name @ gmail.com'
44__date__ = '2011-05-14'
45__version__ = '2.3'
46
47from .compat import *
48from .widgets import *
49from .progressbar import *
diff --git a/bitbake/lib/progressbar/compat.py b/bitbake/lib/progressbar/compat.py
new file mode 100644
index 0000000000..a39f4a1f4e
--- /dev/null
+++ b/bitbake/lib/progressbar/compat.py
@@ -0,0 +1,44 @@
1# -*- coding: utf-8 -*-
2#
3# progressbar - Text progress bar library for Python.
4# Copyright (c) 2005 Nilton Volpato
5#
6# This library is free software; you can redistribute it and/or
7# modify it under the terms of the GNU Lesser General Public
8# License as published by the Free Software Foundation; either
9# version 2.1 of the License, or (at your option) any later version.
10#
11# This library is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14# Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public
17# License along with this library; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
20"""Compatibility methods and classes for the progressbar module."""
21
22
23# Python 3.x (and backports) use a modified iterator syntax
24# This will allow 2.x to behave with 3.x iterators
25try:
26 next
27except NameError:
28 def next(iter):
29 try:
30 # Try new style iterators
31 return iter.__next__()
32 except AttributeError:
33 # Fallback in case of a "native" iterator
34 return iter.next()
35
36
37# Python < 2.5 does not have "any"
38try:
39 any
40except NameError:
41 def any(iterator):
42 for item in iterator:
43 if item: return True
44 return False
diff --git a/bitbake/lib/progressbar/progressbar.py b/bitbake/lib/progressbar/progressbar.py
new file mode 100644
index 0000000000..0b9dcf763e
--- /dev/null
+++ b/bitbake/lib/progressbar/progressbar.py
@@ -0,0 +1,307 @@
1# -*- coding: utf-8 -*-
2#
3# progressbar - Text progress bar library for Python.
4# Copyright (c) 2005 Nilton Volpato
5#
6# This library is free software; you can redistribute it and/or
7# modify it under the terms of the GNU Lesser General Public
8# License as published by the Free Software Foundation; either
9# version 2.1 of the License, or (at your option) any later version.
10#
11# This library is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14# Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public
17# License along with this library; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
20"""Main ProgressBar class."""
21
22from __future__ import division
23
24import math
25import os
26import signal
27import sys
28import time
29
30try:
31 from fcntl import ioctl
32 from array import array
33 import termios
34except ImportError:
35 pass
36
37from .compat import * # for: any, next
38from . import widgets
39
40
41class UnknownLength: pass
42
43
44class ProgressBar(object):
45 """The ProgressBar class which updates and prints the bar.
46
47 A common way of using it is like:
48 >>> pbar = ProgressBar().start()
49 >>> for i in range(100):
50 ... # do something
51 ... pbar.update(i+1)
52 ...
53 >>> pbar.finish()
54
55 You can also use a ProgressBar as an iterator:
56 >>> progress = ProgressBar()
57 >>> for i in progress(some_iterable):
58 ... # do something
59 ...
60
61 Since the progress bar is incredibly customizable you can specify
62 different widgets of any type in any order. You can even write your own
63 widgets! However, since there are already a good number of widgets you
64 should probably play around with them before moving on to create your own
65 widgets.
66
67 The term_width parameter represents the current terminal width. If the
68 parameter is set to an integer then the progress bar will use that,
69 otherwise it will attempt to determine the terminal width falling back to
70 80 columns if the width cannot be determined.
71
72 When implementing a widget's update method you are passed a reference to
73 the current progress bar. As a result, you have access to the
74 ProgressBar's methods and attributes. Although there is nothing preventing
75 you from changing the ProgressBar you should treat it as read only.
76
77 Useful methods and attributes include (Public API):
78 - currval: current progress (0 <= currval <= maxval)
79 - maxval: maximum (and final) value
80 - finished: True if the bar has finished (reached 100%)
81 - start_time: the time when start() method of ProgressBar was called
82 - seconds_elapsed: seconds elapsed since start_time and last call to
83 update
84 - percentage(): progress in percent [0..100]
85 """
86
87 __slots__ = ('currval', 'fd', 'finished', 'last_update_time',
88 'left_justify', 'maxval', 'next_update', 'num_intervals',
89 'poll', 'seconds_elapsed', 'signal_set', 'start_time',
90 'term_width', 'update_interval', 'widgets', '_time_sensitive',
91 '__iterable')
92
93 _DEFAULT_MAXVAL = 100
94 _DEFAULT_TERMSIZE = 80
95 _DEFAULT_WIDGETS = [widgets.Percentage(), ' ', widgets.Bar()]
96
97 def __init__(self, maxval=None, widgets=None, term_width=None, poll=1,
98 left_justify=True, fd=sys.stderr):
99 """Initializes a progress bar with sane defaults."""
100
101 # Don't share a reference with any other progress bars
102 if widgets is None:
103 widgets = list(self._DEFAULT_WIDGETS)
104
105 self.maxval = maxval
106 self.widgets = widgets
107 self.fd = fd
108 self.left_justify = left_justify
109
110 self.signal_set = False
111 if term_width is not None:
112 self.term_width = term_width
113 else:
114 try:
115 self._handle_resize(None, None)
116 signal.signal(signal.SIGWINCH, self._handle_resize)
117 self.signal_set = True
118 except (SystemExit, KeyboardInterrupt): raise
119 except Exception as e:
120 print("DEBUG 5 %s" % e)
121 self.term_width = self._env_size()
122
123 self.__iterable = None
124 self._update_widgets()
125 self.currval = 0
126 self.finished = False
127 self.last_update_time = None
128 self.poll = poll
129 self.seconds_elapsed = 0
130 self.start_time = None
131 self.update_interval = 1
132 self.next_update = 0
133
134
135 def __call__(self, iterable):
136 """Use a ProgressBar to iterate through an iterable."""
137
138 try:
139 self.maxval = len(iterable)
140 except:
141 if self.maxval is None:
142 self.maxval = UnknownLength
143
144 self.__iterable = iter(iterable)
145 return self
146
147
148 def __iter__(self):
149 return self
150
151
152 def __next__(self):
153 try:
154 value = next(self.__iterable)
155 if self.start_time is None:
156 self.start()
157 else:
158 self.update(self.currval + 1)
159 return value
160 except StopIteration:
161 if self.start_time is None:
162 self.start()
163 self.finish()
164 raise
165
166
167 # Create an alias so that Python 2.x won't complain about not being
168 # an iterator.
169 next = __next__
170
171
172 def _env_size(self):
173 """Tries to find the term_width from the environment."""
174
175 return int(os.environ.get('COLUMNS', self._DEFAULT_TERMSIZE)) - 1
176
177
178 def _handle_resize(self, signum=None, frame=None):
179 """Tries to catch resize signals sent from the terminal."""
180
181 h, w = array('h', ioctl(self.fd, termios.TIOCGWINSZ, '\0' * 8))[:2]
182 self.term_width = w
183
184
185 def percentage(self):
186 """Returns the progress as a percentage."""
187 if self.currval >= self.maxval:
188 return 100.0
189 return (self.currval * 100.0 / self.maxval) if self.maxval else 100.00
190
191 percent = property(percentage)
192
193
194 def _format_widgets(self):
195 result = []
196 expanding = []
197 width = self.term_width
198
199 for index, widget in enumerate(self.widgets):
200 if isinstance(widget, widgets.WidgetHFill):
201 result.append(widget)
202 expanding.insert(0, index)
203 else:
204 widget = widgets.format_updatable(widget, self)
205 result.append(widget)
206 width -= len(widget)
207
208 count = len(expanding)
209 while count:
210 portion = max(int(math.ceil(width * 1. / count)), 0)
211 index = expanding.pop()
212 count -= 1
213
214 widget = result[index].update(self, portion)
215 width -= len(widget)
216 result[index] = widget
217
218 return result
219
220
221 def _format_line(self):
222 """Joins the widgets and justifies the line."""
223
224 widgets = ''.join(self._format_widgets())
225
226 if self.left_justify: return widgets.ljust(self.term_width)
227 else: return widgets.rjust(self.term_width)
228
229
230 def _need_update(self):
231 """Returns whether the ProgressBar should redraw the line."""
232 if self.currval >= self.next_update or self.finished: return True
233
234 delta = time.time() - self.last_update_time
235 return self._time_sensitive and delta > self.poll
236
237
238 def _update_widgets(self):
239 """Checks all widgets for the time sensitive bit."""
240
241 self._time_sensitive = any(getattr(w, 'TIME_SENSITIVE', False)
242 for w in self.widgets)
243
244
245 def update(self, value=None):
246 """Updates the ProgressBar to a new value."""
247
248 if value is not None and value is not UnknownLength:
249 if (self.maxval is not UnknownLength
250 and not 0 <= value <= self.maxval):
251
252 raise ValueError('Value out of range')
253
254 self.currval = value
255
256
257 if not self._need_update(): return
258 if self.start_time is None:
259 raise RuntimeError('You must call "start" before calling "update"')
260
261 now = time.time()
262 self.seconds_elapsed = now - self.start_time
263 self.next_update = self.currval + self.update_interval
264 self.fd.write(self._format_line() + '\r')
265 self.fd.flush()
266 self.last_update_time = now
267
268
269 def start(self):
270 """Starts measuring time, and prints the bar at 0%.
271
272 It returns self so you can use it like this:
273 >>> pbar = ProgressBar().start()
274 >>> for i in range(100):
275 ... # do something
276 ... pbar.update(i+1)
277 ...
278 >>> pbar.finish()
279 """
280
281 if self.maxval is None:
282 self.maxval = self._DEFAULT_MAXVAL
283
284 self.num_intervals = max(100, self.term_width)
285 self.next_update = 0
286
287 if self.maxval is not UnknownLength:
288 if self.maxval < 0: raise ValueError('Value out of range')
289 self.update_interval = self.maxval / self.num_intervals
290
291
292 self.start_time = self.last_update_time = time.time()
293 self.update(0)
294
295 return self
296
297
298 def finish(self):
299 """Puts the ProgressBar bar in the finished state."""
300
301 if self.finished:
302 return
303 self.finished = True
304 self.update(self.maxval)
305 self.fd.write('\n')
306 if self.signal_set:
307 signal.signal(signal.SIGWINCH, signal.SIG_DFL)
diff --git a/bitbake/lib/progressbar/widgets.py b/bitbake/lib/progressbar/widgets.py
new file mode 100644
index 0000000000..6434ad5591
--- /dev/null
+++ b/bitbake/lib/progressbar/widgets.py
@@ -0,0 +1,355 @@
1# -*- coding: utf-8 -*-
2#
3# progressbar - Text progress bar library for Python.
4# Copyright (c) 2005 Nilton Volpato
5#
6# This library is free software; you can redistribute it and/or
7# modify it under the terms of the GNU Lesser General Public
8# License as published by the Free Software Foundation; either
9# version 2.1 of the License, or (at your option) any later version.
10#
11# This library is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14# Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public
17# License along with this library; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
20"""Default ProgressBar widgets."""
21
22from __future__ import division
23
24import datetime
25import math
26
27try:
28 from abc import ABCMeta, abstractmethod
29except ImportError:
30 AbstractWidget = object
31 abstractmethod = lambda fn: fn
32else:
33 AbstractWidget = ABCMeta('AbstractWidget', (object,), {})
34
35
36def format_updatable(updatable, pbar):
37 if hasattr(updatable, 'update'): return updatable.update(pbar)
38 else: return updatable
39
40
41class Widget(AbstractWidget):
42 """The base class for all widgets.
43
44 The ProgressBar will call the widget's update value when the widget should
45 be updated. The widget's size may change between calls, but the widget may
46 display incorrectly if the size changes drastically and repeatedly.
47
48 The boolean TIME_SENSITIVE informs the ProgressBar that it should be
49 updated more often because it is time sensitive.
50 """
51
52 TIME_SENSITIVE = False
53 __slots__ = ()
54
55 @abstractmethod
56 def update(self, pbar):
57 """Updates the widget.
58
59 pbar - a reference to the calling ProgressBar
60 """
61
62
63class WidgetHFill(Widget):
64 """The base class for all variable width widgets.
65
66 This widget is much like the \\hfill command in TeX, it will expand to
67 fill the line. You can use more than one in the same line, and they will
68 all have the same width, and together will fill the line.
69 """
70
71 @abstractmethod
72 def update(self, pbar, width):
73 """Updates the widget providing the total width the widget must fill.
74
75 pbar - a reference to the calling ProgressBar
76 width - The total width the widget must fill
77 """
78
79
80class Timer(Widget):
81 """Widget which displays the elapsed seconds."""
82
83 __slots__ = ('format_string',)
84 TIME_SENSITIVE = True
85
86 def __init__(self, format='Elapsed Time: %s'):
87 self.format_string = format
88
89 @staticmethod
90 def format_time(seconds):
91 """Formats time as the string "HH:MM:SS"."""
92
93 return str(datetime.timedelta(seconds=int(seconds)))
94
95
96 def update(self, pbar):
97 """Updates the widget to show the elapsed time."""
98
99 return self.format_string % self.format_time(pbar.seconds_elapsed)
100
101
102class ETA(Timer):
103 """Widget which attempts to estimate the time of arrival."""
104
105 TIME_SENSITIVE = True
106
107 def update(self, pbar):
108 """Updates the widget to show the ETA or total time when finished."""
109
110 if pbar.currval == 0:
111 return 'ETA: --:--:--'
112 elif pbar.finished:
113 return 'Time: %s' % self.format_time(pbar.seconds_elapsed)
114 else:
115 elapsed = pbar.seconds_elapsed
116 eta = elapsed * pbar.maxval / pbar.currval - elapsed
117 return 'ETA: %s' % self.format_time(eta)
118
119
120class AdaptiveETA(Timer):
121 """Widget which attempts to estimate the time of arrival.
122
123 Uses a weighted average of two estimates:
124 1) ETA based on the total progress and time elapsed so far
125 2) ETA based on the progress as per the last 10 update reports
126
127 The weight depends on the current progress so that to begin with the
128 total progress is used and at the end only the most recent progress is
129 used.
130 """
131
132 TIME_SENSITIVE = True
133 NUM_SAMPLES = 10
134
135 def _update_samples(self, currval, elapsed):
136 sample = (currval, elapsed)
137 if not hasattr(self, 'samples'):
138 self.samples = [sample] * (self.NUM_SAMPLES + 1)
139 else:
140 self.samples.append(sample)
141 return self.samples.pop(0)
142
143 def _eta(self, maxval, currval, elapsed):
144 return elapsed * maxval / float(currval) - elapsed
145
146 def update(self, pbar):
147 """Updates the widget to show the ETA or total time when finished."""
148 if pbar.currval == 0:
149 return 'ETA: --:--:--'
150 elif pbar.finished:
151 return 'Time: %s' % self.format_time(pbar.seconds_elapsed)
152 else:
153 elapsed = pbar.seconds_elapsed
154 currval1, elapsed1 = self._update_samples(pbar.currval, elapsed)
155 eta = self._eta(pbar.maxval, pbar.currval, elapsed)
156 if pbar.currval > currval1:
157 etasamp = self._eta(pbar.maxval - currval1,
158 pbar.currval - currval1,
159 elapsed - elapsed1)
160 weight = (pbar.currval / float(pbar.maxval)) ** 0.5
161 eta = (1 - weight) * eta + weight * etasamp
162 return 'ETA: %s' % self.format_time(eta)
163
164
165class FileTransferSpeed(Widget):
166 """Widget for showing the transfer speed (useful for file transfers)."""
167
168 FORMAT = '%6.2f %s%s/s'
169 PREFIXES = ' kMGTPEZY'
170 __slots__ = ('unit',)
171
172 def __init__(self, unit='B'):
173 self.unit = unit
174
175 def update(self, pbar):
176 """Updates the widget with the current SI prefixed speed."""
177
178 if pbar.seconds_elapsed < 2e-6 or pbar.currval < 2e-6: # =~ 0
179 scaled = power = 0
180 else:
181 speed = pbar.currval / pbar.seconds_elapsed
182 power = int(math.log(speed, 1000))
183 scaled = speed / 1000.**power
184
185 return self.FORMAT % (scaled, self.PREFIXES[power], self.unit)
186
187
188class AnimatedMarker(Widget):
189 """An animated marker for the progress bar which defaults to appear as if
190 it were rotating.
191 """
192
193 __slots__ = ('markers', 'curmark')
194
195 def __init__(self, markers='|/-\\'):
196 self.markers = markers
197 self.curmark = -1
198
199 def update(self, pbar):
200 """Updates the widget to show the next marker or the first marker when
201 finished"""
202
203 if pbar.finished: return self.markers[0]
204
205 self.curmark = (self.curmark + 1) % len(self.markers)
206 return self.markers[self.curmark]
207
208# Alias for backwards compatibility
209RotatingMarker = AnimatedMarker
210
211
212class Counter(Widget):
213 """Displays the current count."""
214
215 __slots__ = ('format_string',)
216
217 def __init__(self, format='%d'):
218 self.format_string = format
219
220 def update(self, pbar):
221 return self.format_string % pbar.currval
222
223
224class Percentage(Widget):
225 """Displays the current percentage as a number with a percent sign."""
226
227 def update(self, pbar):
228 return '%3d%%' % pbar.percentage()
229
230
231class FormatLabel(Timer):
232 """Displays a formatted label."""
233
234 mapping = {
235 'elapsed': ('seconds_elapsed', Timer.format_time),
236 'finished': ('finished', None),
237 'last_update': ('last_update_time', None),
238 'max': ('maxval', None),
239 'seconds': ('seconds_elapsed', None),
240 'start': ('start_time', None),
241 'value': ('currval', None)
242 }
243
244 __slots__ = ('format_string',)
245 def __init__(self, format):
246 self.format_string = format
247
248 def update(self, pbar):
249 context = {}
250 for name, (key, transform) in self.mapping.items():
251 try:
252 value = getattr(pbar, key)
253
254 if transform is None:
255 context[name] = value
256 else:
257 context[name] = transform(value)
258 except: pass
259
260 return self.format_string % context
261
262
263class SimpleProgress(Widget):
264 """Returns progress as a count of the total (e.g.: "5 of 47")."""
265
266 __slots__ = ('sep',)
267
268 def __init__(self, sep=' of '):
269 self.sep = sep
270
271 def update(self, pbar):
272 return '%d%s%d' % (pbar.currval, self.sep, pbar.maxval)
273
274
275class Bar(WidgetHFill):
276 """A progress bar which stretches to fill the line."""
277
278 __slots__ = ('marker', 'left', 'right', 'fill', 'fill_left')
279
280 def __init__(self, marker='#', left='|', right='|', fill=' ',
281 fill_left=True):
282 """Creates a customizable progress bar.
283
284 marker - string or updatable object to use as a marker
285 left - string or updatable object to use as a left border
286 right - string or updatable object to use as a right border
287 fill - character to use for the empty part of the progress bar
288 fill_left - whether to fill from the left or the right
289 """
290 self.marker = marker
291 self.left = left
292 self.right = right
293 self.fill = fill
294 self.fill_left = fill_left
295
296
297 def update(self, pbar, width):
298 """Updates the progress bar and its subcomponents."""
299
300 left, marked, right = (format_updatable(i, pbar) for i in
301 (self.left, self.marker, self.right))
302
303 width -= len(left) + len(right)
304 # Marked must *always* have length of 1
305 if pbar.maxval:
306 marked *= int(pbar.currval / pbar.maxval * width)
307 else:
308 marked = ''
309
310 if self.fill_left:
311 return '%s%s%s' % (left, marked.ljust(width, self.fill), right)
312 else:
313 return '%s%s%s' % (left, marked.rjust(width, self.fill), right)
314
315
316class ReverseBar(Bar):
317 """A bar which has a marker which bounces from side to side."""
318
319 def __init__(self, marker='#', left='|', right='|', fill=' ',
320 fill_left=False):
321 """Creates a customizable progress bar.
322
323 marker - string or updatable object to use as a marker
324 left - string or updatable object to use as a left border
325 right - string or updatable object to use as a right border
326 fill - character to use for the empty part of the progress bar
327 fill_left - whether to fill from the left or the right
328 """
329 self.marker = marker
330 self.left = left
331 self.right = right
332 self.fill = fill
333 self.fill_left = fill_left
334
335
336class BouncingBar(Bar):
337 def update(self, pbar, width):
338 """Updates the progress bar and its subcomponents."""
339
340 left, marker, right = (format_updatable(i, pbar) for i in
341 (self.left, self.marker, self.right))
342
343 width -= len(left) + len(right)
344
345 if pbar.finished: return '%s%s%s' % (left, width * marker, right)
346
347 position = int(pbar.currval % (width * 2 - 1))
348 if position > width: position = width * 2 - position
349 lpad = self.fill * (position - 1)
350 rpad = self.fill * (width - len(marker) - len(lpad))
351
352 # Swap if we want to bounce the other way
353 if not self.fill_left: rpad, lpad = lpad, rpad
354
355 return '%s%s%s%s%s' % (left, lpad, marker, rpad, right)