示例#1
0
 def __init__(self, denominator, filename, max_width=None):
     super(ProgressBarYum, self).__init__(denominator, max_width=max_width)
     self.filename = filename
     self.template = '{filename} {percent:>4s} {bar} {rate:>9s} | {numerator:>7s}  {eta:<12s}'
     self.template_completed = '{filename} | {numerator:>7s}  {eta:<12s}'
     if self.undefined:
         self.bar = BarUndefinedEmpty()
     else:
         self.bar = BarDoubled()
示例#2
0
 def __init__(self, denominator, filename, max_width=None):
     super(ProgressBarYum, self).__init__(denominator, max_width=max_width)
     self.filename = filename
     self.template = '{filename} {percent:>4s} {bar} {rate:>9s} | {numerator:>7s}  {eta:<12s}'
     self.template_completed = '{filename} | {numerator:>7s}  {eta:<12s}'
     if self.undefined:
         self.bar = BarUndefinedEmpty()
     else:
         self.bar = BarDoubled()
示例#3
0
def test_bar_doubled():
    bar = BarDoubled()

    assert '[          ]' == bar.bar(12, 0)
    assert '[-         ]' == bar.bar(12, 9)
    assert '[=         ]' == bar.bar(12, 10)
    assert '[=-        ]' == bar.bar(12, 15)
    assert '[==        ]' == bar.bar(12, 20)
    assert '[=====     ]' == bar.bar(12, 50)
    assert '[=========-]' == bar.bar(12, 99)
    assert '[==========]' == bar.bar(12, 100)
def test_bar_doubled():
    bar = BarDoubled()

    assert '[          ]' == bar.bar(12, 0)
    assert '[-         ]' == bar.bar(12, 9)
    assert '[=         ]' == bar.bar(12, 10)
    assert '[=-        ]' == bar.bar(12, 15)
    assert '[==        ]' == bar.bar(12, 20)
    assert '[=====     ]' == bar.bar(12, 50)
    assert '[=========-]' == bar.bar(12, 99)
    assert '[==========]' == bar.bar(12, 100)
示例#5
0
class ProgressBarYum(BaseProgressBar):
    """Progress bar modeled after the one in YUM.

    Looks like one of these:
    CentOS-7.0  27% [===-         ] 265 MiB/s | 1.8 GiB  00:00:19 ETA
    CentOS-7.0-1406-x86_64-Everything.iso     | 6.6 GiB  00:00:26
    CentOS-7.0      [             ] 265 MiB/s | 2.8 GiB

    Positional arguments:
    denominator -- the final/total number of units (like the expected file size of a download). 0 if unknown.
    filename -- the string to display before the progress bar. Limited to whatever space is available in the terminal.

    Keyword arguments:
    max_with -- limit number of characters shown (by default the full progress bar takes up the entire terminal width).

    Instance variables:
    template -- string template of the full progress bar.
    template_completed -- string template of the full progress bar at 100% or force_done = True.
    bar -- class instance of the 'bar' part of the full progress bar.

    More instance variables in etaprogress.components.base_progress_bar.BaseProgressBar.
    """
    def __init__(self, denominator, filename, max_width=None):
        super(ProgressBarYum, self).__init__(denominator, max_width=max_width)
        self.filename = filename
        self.template = '{filename} {percent:>4s} {bar} {rate:>9s} | {numerator:>7s}  {eta:<12s}'
        self.template_completed = '{filename} | {numerator:>7s}  {eta:<12s}'
        if self.undefined:
            self.bar = BarUndefinedEmpty()
        else:
            self.bar = BarDoubled()

    def __str__(self):
        """Returns the fully-built progress bar and other data."""
        # Partially build out template.
        filename = '{filename}'
        numerator = self.str_numerator
        eta = self.str_eta
        if self.done:
            template = self.template_completed.format(filename=filename,
                                                      numerator=numerator,
                                                      eta=eta)
        else:
            bar = '{bar}'
            percent = '' if self.undefined else '{0}%'.format(int(
                self.percent))
            rate = self.str_rate
            template = self.template.format(filename=filename,
                                            percent=percent,
                                            bar=bar,
                                            rate=rate,
                                            numerator=numerator,
                                            eta=eta)
        width = get_remaining_width(template.format(bar='', filename=''),
                                    self.max_width or None)

        # Filename will have 40% of the available width if not done.
        if self.done:
            filename = self.filename[:width].ljust(width) if width > 0 else ''
            bar = None
        else:
            width_filename = int(width * 0.4)
            filename = self.filename[:width_filename].ljust(
                width_filename) if width_filename > 0 else ''
            bar = self.bar.bar(width - width_filename, percent=self.percent)
        return template.format(bar=bar, filename=filename)

    @staticmethod
    def _generate_eta(seconds):
        """Returns a human readable ETA string."""
        return '' if seconds is None else eta_hms(
            seconds, always_show_hours=True, hours_leading_zero=True)

    @property
    def str_eta(self):
        """Returns a formatted ETA value for the progress bar."""
        if self.done:
            return eta_hms(self._eta.elapsed,
                           always_show_hours=True,
                           hours_leading_zero=True)
        if not self._eta_string:
            return ''
        return '{0} ETA'.format(self._eta_string)

    @property
    def str_numerator(self):
        """Returns the numerator with formatting."""
        unit_numerator, unit = UnitByte(self.numerator).auto_no_thousands
        if unit_numerator >= 10:
            formatter = '%d'
        else:
            formatter = '%0.1f'
        return '{0} {1}'.format(
            locale.format(formatter, unit_numerator, grouping=False), unit)

    @property
    def str_rate(self):
        """Returns the rate with formatting."""
        # Handle special cases.
        if not self._eta.started or self._eta.stalled or not self.rate:
            return '--- KiB/s'

        unit_rate, unit = UnitByte(self.rate).auto_no_thousands
        if unit_rate >= 10:
            formatter = '%d'
        else:
            formatter = '%0.1f'
        return '{0} {1}/s'.format(
            locale.format(formatter, unit_rate, grouping=False), unit)
示例#6
0
class ProgressBarYum(BaseProgressBar):
    """Progress bar modeled after the one in YUM.

    Looks like one of these:
    CentOS-7.0  27% [===-         ] 265 MiB/s | 1.8 GiB  00:00:19 ETA
    CentOS-7.0-1406-x86_64-Everything.iso     | 6.6 GiB  00:00:26
    CentOS-7.0      [             ] 265 MiB/s | 2.8 GiB

    Positional arguments:
    denominator -- the final/total number of units (like the expected file size of a download). 0 if unknown.
    filename -- the string to display before the progress bar. Limited to whatever space is available in the terminal.

    Keyword arguments:
    max_with -- limit number of characters shown (by default the full progress bar takes up the entire terminal width).

    Instance variables:
    template -- string template of the full progress bar.
    template_completed -- string template of the full progress bar at 100% or force_done = True.
    bar -- class instance of the 'bar' part of the full progress bar.

    More instance variables in etaprogress.components.base_progress_bar.BaseProgressBar.
    """

    def __init__(self, denominator, filename, max_width=None):
        super(ProgressBarYum, self).__init__(denominator, max_width=max_width)
        self.filename = filename
        self.template = '{filename} {percent:>4s} {bar} {rate:>9s} | {numerator:>7s}  {eta:<12s}'
        self.template_completed = '{filename} | {numerator:>7s}  {eta:<12s}'
        if self.undefined:
            self.bar = BarUndefinedEmpty()
        else:
            self.bar = BarDoubled()

    def __str__(self):
        """Returns the fully-built progress bar and other data."""
        # Partially build out template.
        filename = '{filename}'
        numerator = self.str_numerator
        eta = self.str_eta
        if self.done:
            template = self.template_completed.format(filename=filename, numerator=numerator, eta=eta)
        else:
            bar = '{bar}'
            percent = '' if self.undefined else '{0}%'.format(int(self.percent))
            rate = self.str_rate
            template = self.template.format(filename=filename, percent=percent, bar=bar, rate=rate, numerator=numerator,
                                            eta=eta)
        width = get_remaining_width(template.format(bar='', filename=''), self.max_width or None)

        # Filename will have 40% of the available width if not done.
        if self.done:
            filename = self.filename[:width].ljust(width) if width > 0 else ''
            bar = None
        else:
            width_filename = int(width * 0.4)
            filename = self.filename[:width_filename].ljust(width_filename) if width_filename > 0 else ''
            bar = self.bar.bar(width - width_filename, percent=self.percent)
        return template.format(bar=bar, filename=filename)

    @staticmethod
    def _generate_eta(seconds):
        """Returns a human readable ETA string."""
        return '' if seconds is None else eta_hms(seconds, always_show_hours=True, hours_leading_zero=True)

    @property
    def str_eta(self):
        """Returns a formatted ETA value for the progress bar."""
        if self.done:
            return eta_hms(self._eta.elapsed, always_show_hours=True, hours_leading_zero=True)
        if not self._eta_string:
            return ''
        return '{0} ETA'.format(self._eta_string)

    @property
    def str_numerator(self):
        """Returns the numerator with formatting."""
        unit_numerator, unit = UnitByte(self.numerator).auto_no_thousands
        if unit_numerator >= 10:
            formatter = '%d'
        else:
            formatter = '%0.1f'
        return '{0} {1}'.format(locale.format(formatter, unit_numerator, grouping=False), unit)

    @property
    def str_rate(self):
        """Returns the rate with formatting."""
        # Handle special cases.
        if not self._eta.started or self._eta.stalled or not self.rate:
            return '--- KiB/s'

        unit_rate, unit = UnitByte(self.rate).auto_no_thousands
        if unit_rate >= 10:
            formatter = '%d'
        else:
            formatter = '%0.1f'
        return '{0} {1}/s'.format(locale.format(formatter, unit_rate, grouping=False), unit)