Esempio n. 1
0
def download_progressbar(total_size):
    """
    Create a progress bar to show in real-time a download status
    """

    # Compute DownaloadProgressBar max value
    if total_size <= 0:
        max_val = progressbar.UnknownLength
    else:
        max_val = int(total_size / CHUNK_SIZE)

    # DownaloadProgressBar settings
    MARKER = '█'
    PREFIXES = ('', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')[1:]
    POLL_INTERVAL = 0.8

    # DownaloadProgressBar spacing
    LEFT_SPACE = 4
    PERCENTAGE_SPACE = 4
    PRE_BAR_SPACE = 1
    BAR_SPACE = 35
    POST_BAR_SPACE = 1
    DATA_SIZE_SPACE = 8
    PRE_SPEED_SPACE = 1
    SPEED_SPACE = 8

    # Compute right spacing, and ensure that is not negative
    try:
        right_space = int(get_terminal_size()[0]) - \
        LEFT_SPACE - PERCENTAGE_SPACE - PRE_BAR_SPACE - BAR_SPACE - \
        POST_BAR_SPACE - DATA_SIZE_SPACE - PRE_SPEED_SPACE - SPEED_SPACE
        if right_space < 0:
            right_space = 0
    except (ValueError, TypeError, ArithmeticError):
        right_space = 0

    # Define DownaloadProgressBar skin
    bar_skin = ([
        LEFT_SPACE * ' ',
        progressbar.Percentage(), PRE_BAR_SPACE * ' ',
        progressbar.Bar(marker=MARKER), POST_BAR_SPACE * ' ',
        progressbar.DataSize(prefixes=PREFIXES), PRE_SPEED_SPACE * ' ',
        progressbar.AdaptiveTransferSpeed(prefixes=PREFIXES), right_space * ' '
    ])

    # Generate DownaloadProgressBar
    return progressbar.ProgressBar(max_value=max_val,
                                   widgets=bar_skin,
                                   poll_interval=POLL_INTERVAL)
Esempio n. 2
0
    def emit_progress_bar(progress: str, index: int, total: int) -> str:
        """
        A progress bar that is continuously updated in Python's standard
        out.
        :param progress: a string printed to stdout that is updated and later
        returned.
        :param index: the current index of the iteration within the tracked
        process.
        :param total: the total length of the tracked process.
        :return: progress string.
        """

        w, h = get_terminal_size()
        sys.stdout.write("\r")
        block_size = int(total / w)
        if index % block_size == 0:
            progress += "="
        percent = index / total
        sys.stdout.write("[ %s ] %.2f%%" % (progress, percent * 100))
        sys.stdout.flush()
        return progress
Esempio n. 3
0
def emit_progress_bar(progress: str,
                      index: int,
                      total: int,
                      percent_offset: float = 1e-9) -> str:
    """
    A progress bar that is continuously updated in Python's standard
    out.
    :param progress: a string printed to stdout that is updated and later
    returned.
    :param index: the current index of the iteration within the tracked
    process.
    :param total: the total length of the tracked process.
    :param percent_offset: an offset in which to start the progress bar. 0.5 starts the
    progress bar at 50%.
    :return: progress string.
    """

    w, h = get_terminal_size()
    if percent_offset > 1e-9:
        w = w * 0.5
    sys.stdout.write("\r")

    if total < w:
        block_size = int(w / total)
    else:
        block_size = int(total / w)

    if index % block_size == 0:
        progress += "="

    percent = index / total
    if percent_offset > 1e-9:
        percent_inverse = percent_offset**-1
        percent = percent_offset + (index / (total * percent_inverse))

    sys.stdout.write("[ %s ] %.2f%%" % (progress, percent * 100))
    sys.stdout.flush()

    return progress
Esempio n. 4
0
import progressbar as pbar
from python_utils.terminal import get_terminal_size

TERM_WIDTH = int(get_terminal_size()[0])

CUSTOM_CHAR_LEN_MAP = {
    '█': 1,
}

PBAR_WIDGETS = [
    pbar.Percentage(), ' (',
    pbar.SimpleProgress(), ') ',
    pbar.Bar(marker='█', left=' |', right='| ', fill='_'), ' ',
    pbar.Timer(), ' | ETA ',
    pbar.ETA(), ' |'
]


def CustomCharLen(value):

    total = 0
    for c in value:
        total += CUSTOM_CHAR_LEN_MAP.get(c, 1)

    return total


PBAR_ARGS = {
    'widgets': PBAR_WIDGETS,
    'term_width': TERM_WIDTH,
    'len_func': CustomCharLen