WebBrowser/Download/DownloadUtilities.py

changeset 6221
35ec993034e1
parent 6048
82ad8ec9548c
child 6226
bb6b83b72a5d
--- a/WebBrowser/Download/DownloadUtilities.py	Sat Apr 07 16:40:21 2018 +0200
+++ b/WebBrowser/Download/DownloadUtilities.py	Sun Apr 08 15:54:34 2018 +0200
@@ -11,49 +11,82 @@
 
 from PyQt5.QtCore import QCoreApplication
 
-from Globals import translate
-
 
 def timeString(timeRemaining):
     """
     Module function to format the given time.
     
-    @param timeRemaining time to be formatted (float)
-    @return time string (string)
+    @param timeRemaining time to be formatted
+    @type float
+    @return time string
+    @rtype str
     """
-    if timeRemaining > 60:
-        minutes = int(timeRemaining / 60)
-        seconds = int(timeRemaining % 60)
-        remaining = translate(
+    if timeRemaining < 10:
+        return QCoreApplication.translate(
+            "DownloadUtilities", "few seconds remaining")
+    elif timeRemaining < 60:    # < 1 minute
+        return QCoreApplication.translate(
             "DownloadUtilities",
-            "%n:{0:02} minutes remaining", "",
-            minutes).format(seconds)
+            "%n seconds remaining", "", int(timeRemaining))
+    elif timeRemaining < 3600:  # < 1 hour
+        return QCoreApplication.translate(
+            "DownloadUtilities",
+            "%n minutes remaining", "", int(timeRemaining / 60))
     else:
-        seconds = int(timeRemaining)
-        remaining = translate(
+        QCoreApplication.translate(
             "DownloadUtilities",
-            "%n seconds remaining", "", seconds)
-    
-    return remaining
+            "%n hours remaining", "", int(timeRemaining / 3600))
 
 
 def dataString(size):
     """
     Module function to generate a formatted size string.
     
-    @param size size to be formatted (integer)
-    @return formatted data string (string)
+    @param size size to be formatted
+    @type int
+    @return formatted data string
+    @rtype str
     """
-    unit = ""
     if size < 1024:
-        unit = QCoreApplication.translate("DownloadUtilities", "Bytes")
+        return QCoreApplication.translate(
+            "DownloadUtilities", "{0:.1f} Bytes").format(size)
     elif size < 1024 * 1024:
         size /= 1024
-        unit = QCoreApplication.translate("DownloadUtilities", "KiB")
+        return QCoreApplication.translate(
+            "DownloadUtilities", "{0:.1f} KiB").format(size)
     elif size < 1024 * 1024 * 1024:
         size /= 1024 * 1024
-        unit = QCoreApplication.translate("DownloadUtilities", "MiB")
+        return QCoreApplication.translate(
+            "DownloadUtilities", "{0:.2f} MiB").format(size)
     else:
         size /= 1024 * 1024 * 1024
-        unit = QCoreApplication.translate("DownloadUtilities", "GiB")
-    return "{0:.1f} {1}".format(size, unit)
+        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