python-mylib/mylib/pbar.py

53 lines
1.3 KiB
Python

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