Blame dnf/cli/format.py

Packit 6f3914
# Copyright (C) 2013-2016 Red Hat, Inc.
Packit 6f3914
#
Packit 6f3914
# This copyrighted material is made available to anyone wishing to use,
Packit 6f3914
# modify, copy, or redistribute it subject to the terms and conditions of
Packit 6f3914
# the GNU General Public License v.2, or (at your option) any later version.
Packit 6f3914
# This program is distributed in the hope that it will be useful, but WITHOUT
Packit 6f3914
# ANY WARRANTY expressed or implied, including the implied warranties of
Packit 6f3914
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit 6f3914
# Public License for more details.  You should have received a copy of the
Packit 6f3914
# GNU General Public License along with this program; if not, write to the
Packit 6f3914
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 6f3914
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
Packit 6f3914
# source code or documentation are not subject to the GNU General Public
Packit 6f3914
# License and may only be used or replicated with the express permission of
Packit 6f3914
# Red Hat, Inc.
Packit 6f3914
Packit 6f3914
from __future__ import unicode_literals
Packit 6f3914
from dnf.pycomp import long
Packit 6f3914
Packit 6f3914
def format_number(number, SI=0, space=' '):
Packit 6f3914
    """Return a human-readable metric-like string representation
Packit 6f3914
    of a number.
Packit 6f3914
Packit 6f3914
    :param number: the number to be converted to a human-readable form
Packit 6f3914
    :param SI: If is 0, this function will use the convention
Packit 6f3914
       that 1 kilobyte = 1024 bytes, otherwise, the convention
Packit 6f3914
       that 1 kilobyte = 1000 bytes will be used
Packit 6f3914
    :param space: string that will be placed between the number
Packit 6f3914
       and the SI prefix
Packit 6f3914
    :return: a human-readable metric-like string representation of
Packit 6f3914
       *number*
Packit 6f3914
    """
Packit 6f3914
Packit 6f3914
    # copied from from urlgrabber.progress
Packit 6f3914
    symbols = [ ' ', # (none)
Packit 6f3914
                'k', # kilo
Packit 6f3914
                'M', # mega
Packit 6f3914
                'G', # giga
Packit 6f3914
                'T', # tera
Packit 6f3914
                'P', # peta
Packit 6f3914
                'E', # exa
Packit 6f3914
                'Z', # zetta
Packit 6f3914
                'Y'] # yotta
Packit 6f3914
Packit 6f3914
    if SI: step = 1000.0
Packit 6f3914
    else: step = 1024.0
Packit 6f3914
Packit 6f3914
    thresh = 999
Packit 6f3914
    depth = 0
Packit 6f3914
    max_depth = len(symbols) - 1
Packit 6f3914
Packit 6f3914
    if number is None:
Packit 6f3914
        number = 0.0
Packit 6f3914
Packit 6f3914
    # we want numbers between 0 and thresh, but don't exceed the length
Packit 6f3914
    # of our list.  In that event, the formatting will be screwed up,
Packit 6f3914
    # but it'll still show the right number.
Packit 6f3914
    while number > thresh and depth < max_depth:
Packit 6f3914
        depth  = depth + 1
Packit 6f3914
        number = number / step
Packit 6f3914
Packit 6f3914
    if isinstance(number, int) or isinstance(number, long):
Packit 6f3914
        format = '%i%s%s'
Packit 6f3914
    elif number < 9.95:
Packit 6f3914
        # must use 9.95 for proper sizing.  For example, 9.99 will be
Packit 6f3914
        # rounded to 10.0 with the .1f format string (which is too long)
Packit 6f3914
        format = '%.1f%s%s'
Packit 6f3914
    else:
Packit 6f3914
        format = '%.0f%s%s'
Packit 6f3914
Packit 6f3914
    return(format % (float(number or 0), space, symbols[depth]))
Packit 6f3914
Packit 6f3914
def format_time(seconds, use_hours=0):
Packit 6f3914
    """Return a human-readable string representation of a number
Packit 6f3914
    of seconds.  The string will show seconds, minutes, and
Packit 6f3914
    optionally hours.
Packit 6f3914
Packit 6f3914
    :param seconds: the number of seconds to convert to a
Packit 6f3914
       human-readable form
Packit 6f3914
    :param use_hours: If use_hours is 0, the representation will
Packit 6f3914
       be in minutes and seconds. Otherwise, it will be in hours,
Packit 6f3914
       minutes, and seconds
Packit 6f3914
    :return: a human-readable string representation of *seconds*
Packit 6f3914
    """
Packit 6f3914
Packit 6f3914
    # copied from from urlgrabber.progress
Packit 6f3914
    if seconds is None or seconds < 0:
Packit 6f3914
        if use_hours: return '--:--:--'
Packit 6f3914
        else:         return '--:--'
Packit 6f3914
    elif seconds == float('inf'):
Packit 6f3914
        return 'Infinite'
Packit 6f3914
    else:
Packit 6f3914
        seconds = int(seconds)
Packit 6f3914
        minutes = seconds // 60
Packit 6f3914
        seconds = seconds % 60
Packit 6f3914
        if use_hours:
Packit 6f3914
            hours = minutes // 60
Packit 6f3914
            minutes = minutes % 60
Packit 6f3914
            return '%02i:%02i:%02i' % (hours, minutes, seconds)
Packit 6f3914
        else:
Packit 6f3914
            return '%02i:%02i' % (minutes, seconds)
Packit 6f3914
Packit 6f3914
def indent_block(s):
Packit 6f3914
    return '\n'.join('  ' + s for s in s.splitlines())