[Yum] Yum article at LWN.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



seth vidal wrote:

>His complaint about the lack of a progress bar for downloads is
>legitimate and it's my fault.
>  
>
In attachment good (i think) class for display progressbars.

-- 
..............................................................
IRC: irc.freenode.net #asplinux                Grigory Bakunov
ICQ: 51369901                        ASPLinux Development Team
Life would be so much easier if
                        we could just look at the source code.

-------------- next part --------------
#!/usr/bin/env python
"""progressbar abstraction

License: LGPL
Authors: Vladimir Bormotov <bor@xxxxxxxx>, Grigory Bakunov <black@xxxxxxxxxxx>

"""
import sys

HASHES = 32
FMT_PROGRESS = '\r [%3d%%] %s %s'
    # example: ' [100%] #####----------- extrastring'

class progress:
    """progressbar base class

    sys.stdout support
    """
    def __init__(self, total=None, amount=0, hashes=HASHES):
        """init progressbar with given total valie

        optional values:
          amount - initial progress amount
          hashes - max hashes in bar, default HASHES
        """
        self.fmt = FMT_PROGRESS
        self.hashes = hashes
        self.total = total
        self._index = amount
        if not(total is None):
            self.delta = self.total / self.hashes
            self.next(amount)
        # else You can init self.total, self.delta manually

    def inc(self, extra=''):
        """move to next step and redraw progressbar"""
        self._index = self._index + 1
        self.draw(self._index, extra)

    def next(self, amount):
        """evalute percentage and number of hashes"""
        self.amount = amount
        self.percent = (self.amount * 100L) / self.total
        self.graph = int(self.hashes / 100.0 * self.percent)

    def need_redraw(self, amount):
        """true if need redraw progressbar

        check new amount iz bigger then last amount on delta
        """
        return (amount - self.amount) > self.delta

    def draw_next(self, extra=''):
        """draw next progressbar state"""
        _p_bar = '#' * self.graph + '-' * (self.hashes - self.graph)
        sys.stdout.write(self.fmt % (self.percent, _p_bar, extra))
        sys.stdout.flush()

    def draw_final(self):
        """draw newline character"""
        sys.stdout.write('\n')
        sys.stdout.flush()

    def draw(self, amount, extra=''):
        """draw progressbar with given amount"""
        if self.need_redraw(amount):
            self.next(amount)
            self.draw_next(extra)
        if amount >= self.total:
            self.next(amount)
            self.draw_next(extra)
            self.draw_final()


class progress_debug_final(progress):
    """progressbar with debug output at final"""

    def draw_final(self):
        """write self.amount:self.total at final"""
        sys.stdout.write(' %s:%s' % (self.amount, self.total))
        progress.draw_final(self)


if __name__ == '__main__':
    import time
    print 'test for progress() class'
    _pr = progress(199)
    for _idx in range(200):
        _pr.draw(_idx, 'progress label')
        time.sleep(0.02)
    _pr.draw_final()


# vim: set et ts=4 :

[Index of Archives]     [Fedora Users]     [Fedora Legacy List]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]

  Powered by Linux