python-mylib/mylib/pbar.py
Benjamin Renard 6bbacce38a Code cleaning
2021-05-19 19:19:57 +02:00

55 lines
1.3 KiB
Python

# coding: utf8
""" 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()