WebBrowser/Download/DownloadUtilities.py

Sun, 08 Apr 2018 15:54:34 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 08 Apr 2018 15:54:34 +0200
changeset 6221
35ec993034e1
parent 6048
82ad8ec9548c
child 6226
bb6b83b72a5d
permissions
-rw-r--r--

Web Browser (NG): improvement of the download manager

# -*- coding: utf-8 -*-

# Copyright (c) 2010 - 2018 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing some utility functions for the Download package.
"""

from __future__ import unicode_literals

from PyQt5.QtCore import QCoreApplication


def timeString(timeRemaining):
    """
    Module function to format the given time.
    
    @param timeRemaining time to be formatted
    @type float
    @return time string
    @rtype str
    """
    if timeRemaining < 10:
        return QCoreApplication.translate(
            "DownloadUtilities", "few seconds remaining")
    elif timeRemaining < 60:    # < 1 minute
        return QCoreApplication.translate(
            "DownloadUtilities",
            "%n seconds remaining", "", int(timeRemaining))
    elif timeRemaining < 3600:  # < 1 hour
        return QCoreApplication.translate(
            "DownloadUtilities",
            "%n minutes remaining", "", int(timeRemaining / 60))
    else:
        QCoreApplication.translate(
            "DownloadUtilities",
            "%n hours remaining", "", int(timeRemaining / 3600))


def dataString(size):
    """
    Module function to generate a formatted size string.
    
    @param size size to be formatted
    @type int
    @return formatted data string
    @rtype str
    """
    if size < 1024:
        return QCoreApplication.translate(
            "DownloadUtilities", "{0:.1f} Bytes").format(size)
    elif size < 1024 * 1024:
        size /= 1024
        return QCoreApplication.translate(
            "DownloadUtilities", "{0:.1f} KiB").format(size)
    elif size < 1024 * 1024 * 1024:
        size /= 1024 * 1024
        return QCoreApplication.translate(
            "DownloadUtilities", "{0:.2f} MiB").format(size)
    else:
        size /= 1024 * 1024 * 1024
        return QCoreApplication.translate(
            "DownloadUtilities", "{0:.2f} GiB").format(size)


def speedString(speed):
    """
    Module function to generate a formatted speed string.
    
    @param speed speed to be formatted
    @type float
    @return formatted speed string
    @rtype str
    """
    if speed < 0:
        return QCoreApplication.translate("DownloadUtilities", "Unknown speed")
    
    speed /= 1024       # kB
    if speed < 1024:
        return QCoreApplication.translate(
            "DownloadUtilities", "{0:.1f} KiB/s").format(speed)
    
    speed /= 1024       # MB
    if speed < 1024:
        return QCoreApplication.translate(
            "DownloadUtilities", "{0:.2f} MiB/s").format(speed)
    
    speed /= 1024       # GB
    if speed < 1024:
        return QCoreApplication.translate(
            "DownloadUtilities", "{0:.2f} GiB/s").format(speed)

eric ide

mercurial