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
|
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2021 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""Command line interface utilities"""
__authors__ = ["Valentin Valls", "Jérôme Kieffer"]
__license__ = "MIT"
__date__ = "23/04/2021"
import sys
import codecs
import glob
def expand_args(args):
"""
Takes an argv and expand it (under Windows, cmd does not convert *.tif into
a list of files.
:param list args: list of files or wildcards
:return: list of actual args
"""
new = []
for afile in args:
if glob.has_magic(afile):
new += glob.glob(afile)
else:
new.append(afile)
return new
class ProgressBar:
"""
Progress bar in shell mode
"""
def __init__(self, title, max_value, bar_width):
"""
Create a progress bar using a title, a maximum value and a graphical size.
The display is done with stdout using carriage return to to hide the
previous progress. It is not possible to use stdout for something else
whill a progress bar is in use.
The result looks like:
.. code-block:: none
Title [■■■■■■ ] 50% Message
:param str title: Title displayed before the progress bar
:param float max_value: The maximum value of the progress bar
:param int bar_width: Size of the progressbar in the screen
"""
self.title = title
self.max_value = max_value
self.bar_width = bar_width
self.last_size = 0
self._message = ""
self._value = 0.0
encoding = None
if hasattr(sys.stdout, "encoding"):
# sys.stdout.encoding can't be used in unittest context with some
# configurations of TestRunner. It does not exists in Python2
# StringIO and is None in Python3 StringIO.
encoding = sys.stdout.encoding
if encoding is None:
# We uses the safer aproch: a valid ASCII character.
self.progress_char = '#'
else:
try:
import datetime
if str(datetime.datetime.now())[5:10] == "02-14":
self.progress_char = u'\u2665'
else:
self.progress_char = u'\u25A0'
_byte = codecs.encode(self.progress_char, encoding)
except (ValueError, TypeError, LookupError):
# In case the char is not supported by the encoding,
# or if the encoding does not exists
self.progress_char = '#'
def clear(self):
"""
Remove the progress bar from the display and move the cursor
at the beginning of the line using carriage return.
"""
sys.stdout.write('\r' + " " * self.last_size + "\r")
sys.stdout.flush()
def display(self):
"""
Display the progress bar to stdout
"""
self.update(self._value, self._message)
def update(self, value, message="", max_value=None):
"""
Update the progrss bar with the progress bar's current value.
Set the progress bar's current value, compute the percentage
of progress and update the screen with. Carriage return is used
first and then the content of the progress bar. The cursor is
at the begining of the line.
:param float value: progress bar's current value
:param str message: message displayed after the progress bar
:param float max_value: If not none, update the maximum value of the
progress bar
"""
if max_value is not None:
self.max_value = max_value
self._message = message
self._value = value
if self.max_value == 0:
coef = 1.0
else:
coef = (1.0 * value) / self.max_value
percent = round(coef * 100)
bar_position = int(coef * self.bar_width)
if bar_position > self.bar_width:
bar_position = self.bar_width
# line to display
line = '\r%15s [%s%s] % 3d%% %s' % (self.title, self.progress_char * bar_position, ' ' * (self.bar_width - bar_position), percent, message)
# trailing to mask the previous message
line_size = len(line)
clean_size = self.last_size - line_size
if clean_size < 0:
clean_size = 0
self.last_size = line_size
sys.stdout.write(line + " " * clean_size + "\r")
sys.stdout.flush()
|