Blame dnf/cli/format.py

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