File size: 11,323 Bytes
b554a07 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | # Human friendly input/output in Python.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: March 1, 2020
# URL: https://humanfriendly.readthedocs.io
"""
Support for spinners that represent progress on interactive terminals.
The :class:`Spinner` class shows a "spinner" on the terminal to let the user
know that something is happening during long running operations that would
otherwise be silent (leaving the user to wonder what they're waiting for).
Below are some visual examples that should illustrate the point.
**Simple spinners:**
Here's a screen capture that shows the simplest form of spinner:
.. image:: images/spinner-basic.gif
:alt: Animated screen capture of a simple spinner.
The following code was used to create the spinner above:
.. code-block:: python
import itertools
import time
from humanfriendly import Spinner
with Spinner(label="Downloading") as spinner:
for i in itertools.count():
# Do something useful here.
time.sleep(0.1)
# Advance the spinner.
spinner.step()
**Spinners that show elapsed time:**
Here's a spinner that shows the elapsed time since it started:
.. image:: images/spinner-with-timer.gif
:alt: Animated screen capture of a spinner showing elapsed time.
The following code was used to create the spinner above:
.. code-block:: python
import itertools
import time
from humanfriendly import Spinner, Timer
with Spinner(label="Downloading", timer=Timer()) as spinner:
for i in itertools.count():
# Do something useful here.
time.sleep(0.1)
# Advance the spinner.
spinner.step()
**Spinners that show progress:**
Here's a spinner that shows a progress percentage:
.. image:: images/spinner-with-progress.gif
:alt: Animated screen capture of spinner showing progress.
The following code was used to create the spinner above:
.. code-block:: python
import itertools
import random
import time
from humanfriendly import Spinner, Timer
with Spinner(label="Downloading", total=100) as spinner:
progress = 0
while progress < 100:
# Do something useful here.
time.sleep(0.1)
# Advance the spinner.
spinner.step(progress)
# Determine the new progress value.
progress += random.random() * 5
If you want to provide user feedback during a long running operation but it's
not practical to periodically call the :func:`~Spinner.step()` method consider
using :class:`AutomaticSpinner` instead.
As you may already have noticed in the examples above, :class:`Spinner` objects
can be used as context managers to automatically call :func:`Spinner.clear()`
when the spinner ends.
"""
# Standard library modules.
import multiprocessing
import sys
import time
# Modules included in our package.
from humanfriendly import Timer
from humanfriendly.deprecation import deprecated_args
from humanfriendly.terminal import ANSI_ERASE_LINE
# Public identifiers that require documentation.
__all__ = ("AutomaticSpinner", "GLYPHS", "MINIMUM_INTERVAL", "Spinner")
GLYPHS = ["-", "\\", "|", "/"]
"""A list of strings with characters that together form a crude animation :-)."""
MINIMUM_INTERVAL = 0.2
"""Spinners are redrawn with a frequency no higher than this number (a floating point number of seconds)."""
class Spinner(object):
"""Show a spinner on the terminal as a simple means of feedback to the user."""
@deprecated_args('label', 'total', 'stream', 'interactive', 'timer')
def __init__(self, **options):
"""
Initialize a :class:`Spinner` object.
:param label:
The label for the spinner (a string or :data:`None`, defaults to
:data:`None`).
:param total:
The expected number of steps (an integer or :data:`None`). If this is
provided the spinner will show a progress percentage.
:param stream:
The output stream to show the spinner on (a file-like object,
defaults to :data:`sys.stderr`).
:param interactive:
:data:`True` to enable rendering of the spinner, :data:`False` to
disable (defaults to the result of ``stream.isatty()``).
:param timer:
A :class:`.Timer` object (optional). If this is given the spinner
will show the elapsed time according to the timer.
:param interval:
The spinner will be updated at most once every this many seconds
(a floating point number, defaults to :data:`MINIMUM_INTERVAL`).
:param glyphs:
A list of strings with single characters that are drawn in the same
place in succession to implement a simple animated effect (defaults
to :data:`GLYPHS`).
"""
# Store initializer arguments.
self.interactive = options.get('interactive')
self.interval = options.get('interval', MINIMUM_INTERVAL)
self.label = options.get('label')
self.states = options.get('glyphs', GLYPHS)
self.stream = options.get('stream', sys.stderr)
self.timer = options.get('timer')
self.total = options.get('total')
# Define instance variables.
self.counter = 0
self.last_update = 0
# Try to automatically discover whether the stream is connected to
# a terminal, but don't fail if no isatty() method is available.
if self.interactive is None:
try:
self.interactive = self.stream.isatty()
except Exception:
self.interactive = False
def step(self, progress=0, label=None):
"""
Advance the spinner by one step and redraw it.
:param progress: The number of the current step, relative to the total
given to the :class:`Spinner` constructor (an integer,
optional). If not provided the spinner will not show
progress.
:param label: The label to use while redrawing (a string, optional). If
not provided the label given to the :class:`Spinner`
constructor is used instead.
This method advances the spinner by one step without starting a new
line, causing an animated effect which is very simple but much nicer
than waiting for a prompt which is completely silent for a long time.
.. note:: This method uses time based rate limiting to avoid redrawing
the spinner too frequently. If you know you're dealing with
code that will call :func:`step()` at a high frequency,
consider using :func:`sleep()` to avoid creating the
equivalent of a busy loop that's rate limiting the spinner
99% of the time.
"""
if self.interactive:
time_now = time.time()
if time_now - self.last_update >= self.interval:
self.last_update = time_now
state = self.states[self.counter % len(self.states)]
label = label or self.label
if not label:
raise Exception("No label set for spinner!")
elif self.total and progress:
label = "%s: %.2f%%" % (label, progress / (self.total / 100.0))
elif self.timer and self.timer.elapsed_time > 2:
label = "%s (%s)" % (label, self.timer.rounded)
self.stream.write("%s %s %s ..\r" % (ANSI_ERASE_LINE, state, label))
self.counter += 1
def sleep(self):
"""
Sleep for a short period before redrawing the spinner.
This method is useful when you know you're dealing with code that will
call :func:`step()` at a high frequency. It will sleep for the interval
with which the spinner is redrawn (less than a second). This avoids
creating the equivalent of a busy loop that's rate limiting the
spinner 99% of the time.
This method doesn't redraw the spinner, you still have to call
:func:`step()` in order to do that.
"""
time.sleep(MINIMUM_INTERVAL)
def clear(self):
"""
Clear the spinner.
The next line which is shown on the standard output or error stream
after calling this method will overwrite the line that used to show the
spinner.
"""
if self.interactive:
self.stream.write(ANSI_ERASE_LINE)
def __enter__(self):
"""
Enable the use of spinners as context managers.
:returns: The :class:`Spinner` object.
"""
return self
def __exit__(self, exc_type=None, exc_value=None, traceback=None):
"""Clear the spinner when leaving the context."""
self.clear()
class AutomaticSpinner(object):
"""
Show a spinner on the terminal that automatically starts animating.
This class shows a spinner on the terminal (just like :class:`Spinner`
does) that automatically starts animating. This class should be used as a
context manager using the :keyword:`with` statement. The animation
continues for as long as the context is active.
:class:`AutomaticSpinner` provides an alternative to :class:`Spinner`
for situations where it is not practical for the caller to periodically
call :func:`~Spinner.step()` to advance the animation, e.g. because
you're performing a blocking call and don't fancy implementing threading or
subprocess handling just to provide some user feedback.
This works using the :mod:`multiprocessing` module by spawning a
subprocess to render the spinner while the main process is busy doing
something more useful. By using the :keyword:`with` statement you're
guaranteed that the subprocess is properly terminated at the appropriate
time.
"""
def __init__(self, label, show_time=True):
"""
Initialize an automatic spinner.
:param label: The label for the spinner (a string).
:param show_time: If this is :data:`True` (the default) then the spinner
shows elapsed time.
"""
self.label = label
self.show_time = show_time
self.shutdown_event = multiprocessing.Event()
self.subprocess = multiprocessing.Process(target=self._target)
def __enter__(self):
"""Enable the use of automatic spinners as context managers."""
self.subprocess.start()
def __exit__(self, exc_type=None, exc_value=None, traceback=None):
"""Enable the use of automatic spinners as context managers."""
self.shutdown_event.set()
self.subprocess.join()
def _target(self):
try:
timer = Timer() if self.show_time else None
with Spinner(label=self.label, timer=timer) as spinner:
while not self.shutdown_event.is_set():
spinner.step()
spinner.sleep()
except KeyboardInterrupt:
# Swallow Control-C signals without producing a nasty traceback that
# won't make any sense to the average user.
pass
|