This commit is contained in:
Benjamin Renard 2021-03-24 18:49:59 +01:00
parent aa2e1ee99f
commit 0229cc9ab4

123
Pbar.py Normal file
View file

@ -0,0 +1,123 @@
#!/usr/bin/python
# coding: utf8
""" Progress bar """
import logging
import progressbar
class Pbar(object): # pylint: disable=useless-object-inheritance
"""
Progress bar
This class abstract a progress bar that could be enable/disable by
configuration/script parameters.
"""
__pbar = None
__count = None
def __init__(self, name, maxval, enabled=True):
if enabled and maxval:
self.__count = 0
self.__pbar = progressbar.ProgressBar(
widgets=[
name + ': ',
progressbar.Percentage(),
' ',
progressbar.Bar(),
' ',
progressbar.SimpleProgress(),
progressbar.ETA()
],
maxval=maxval
).start()
else:
logging.info(name)
def increment(self, step=None):
"""
Increment the progress bar
:param step: The step (optional, default: 1)
"""
if self.__pbar:
self.__count += step if step else 1
self.__pbar.update(self.__count)
def finish(self):
""" Finish the progress bar """
if self.__pbar:
self.__pbar.finish()
if __name__ == '__main__':
# Run tests
import time
import argparse
default_max_val = 10
# Options parser
parser = argparse.ArgumentParser()
parser.add_argument(
'-v', '--verbose',
action="store_true",
dest="verbose",
help="Enable verbose mode"
)
parser.add_argument(
'-d', '--debug',
action="store_true",
dest="debug",
help="Enable debug mode"
)
parser.add_argument(
'-l', '--log-file',
action="store",
type=str,
dest="logfile",
help="Log file path"
)
parser.add_argument(
'-p', '--progress',
action="store_true",
dest="progress",
help="Enable progress bar"
)
parser.add_argument(
'-C', '--count',
action="store",
type=int,
dest="count",
help="Progress bar max value (default: %s)" % default_max_val,
default=default_max_val
)
options = parser.parse_args()
# Initialize logs
logformat = '%(asctime)s - Test Pbar - %(levelname)s - %(message)s'
if options.debug:
loglevel = logging.DEBUG
elif options.verbose:
loglevel = logging.INFO
else:
loglevel = logging.WARNING
if options.logfile:
logging.basicConfig(filename=options.logfile, level=loglevel, format=logformat)
else:
logging.basicConfig(level=loglevel, format=logformat)
pbar = Pbar('Test', options.count, enabled=options.progress)
for idx in range(0, options.count): # pylint: disable=unused-variable
pbar.increment()
time.sleep(0.3)
pbar.finish()