python-mylib/mylib/pbar.py
Benjamin Renard 62c3fadf96
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Introduce pyupgrade,isort,black and configure pre-commit hooks to run all testing tools before commit
2023-01-16 12:56:12 +01:00

52 lines
1.2 KiB
Python

""" Progress bar """
import logging
import progressbar
log = logging.getLogger(__name__)
class Pbar: # 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:
log.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()