WebBrowser/Download/DownloadUtilities.py

changeset 6221
35ec993034e1
parent 6048
82ad8ec9548c
child 6226
bb6b83b72a5d
equal deleted inserted replaced
6220:dd4a8b507144 6221:35ec993034e1
9 9
10 from __future__ import unicode_literals 10 from __future__ import unicode_literals
11 11
12 from PyQt5.QtCore import QCoreApplication 12 from PyQt5.QtCore import QCoreApplication
13 13
14 from Globals import translate
15
16 14
17 def timeString(timeRemaining): 15 def timeString(timeRemaining):
18 """ 16 """
19 Module function to format the given time. 17 Module function to format the given time.
20 18
21 @param timeRemaining time to be formatted (float) 19 @param timeRemaining time to be formatted
22 @return time string (string) 20 @type float
21 @return time string
22 @rtype str
23 """ 23 """
24 if timeRemaining > 60: 24 if timeRemaining < 10:
25 minutes = int(timeRemaining / 60) 25 return QCoreApplication.translate(
26 seconds = int(timeRemaining % 60) 26 "DownloadUtilities", "few seconds remaining")
27 remaining = translate( 27 elif timeRemaining < 60: # < 1 minute
28 return QCoreApplication.translate(
28 "DownloadUtilities", 29 "DownloadUtilities",
29 "%n:{0:02} minutes remaining", "", 30 "%n seconds remaining", "", int(timeRemaining))
30 minutes).format(seconds) 31 elif timeRemaining < 3600: # < 1 hour
32 return QCoreApplication.translate(
33 "DownloadUtilities",
34 "%n minutes remaining", "", int(timeRemaining / 60))
31 else: 35 else:
32 seconds = int(timeRemaining) 36 QCoreApplication.translate(
33 remaining = translate(
34 "DownloadUtilities", 37 "DownloadUtilities",
35 "%n seconds remaining", "", seconds) 38 "%n hours remaining", "", int(timeRemaining / 3600))
36
37 return remaining
38 39
39 40
40 def dataString(size): 41 def dataString(size):
41 """ 42 """
42 Module function to generate a formatted size string. 43 Module function to generate a formatted size string.
43 44
44 @param size size to be formatted (integer) 45 @param size size to be formatted
45 @return formatted data string (string) 46 @type int
47 @return formatted data string
48 @rtype str
46 """ 49 """
47 unit = ""
48 if size < 1024: 50 if size < 1024:
49 unit = QCoreApplication.translate("DownloadUtilities", "Bytes") 51 return QCoreApplication.translate(
52 "DownloadUtilities", "{0:.1f} Bytes").format(size)
50 elif size < 1024 * 1024: 53 elif size < 1024 * 1024:
51 size /= 1024 54 size /= 1024
52 unit = QCoreApplication.translate("DownloadUtilities", "KiB") 55 return QCoreApplication.translate(
56 "DownloadUtilities", "{0:.1f} KiB").format(size)
53 elif size < 1024 * 1024 * 1024: 57 elif size < 1024 * 1024 * 1024:
54 size /= 1024 * 1024 58 size /= 1024 * 1024
55 unit = QCoreApplication.translate("DownloadUtilities", "MiB") 59 return QCoreApplication.translate(
60 "DownloadUtilities", "{0:.2f} MiB").format(size)
56 else: 61 else:
57 size /= 1024 * 1024 * 1024 62 size /= 1024 * 1024 * 1024
58 unit = QCoreApplication.translate("DownloadUtilities", "GiB") 63 return QCoreApplication.translate(
59 return "{0:.1f} {1}".format(size, unit) 64 "DownloadUtilities", "{0:.2f} GiB").format(size)
65
66
67 def speedString(speed):
68 """
69 Module function to generate a formatted speed string.
70
71 @param speed speed to be formatted
72 @type float
73 @return formatted speed string
74 @rtype str
75 """
76 if speed < 0:
77 return QCoreApplication.translate("DownloadUtilities", "Unknown speed")
78
79 speed /= 1024 # kB
80 if speed < 1024:
81 return QCoreApplication.translate(
82 "DownloadUtilities", "{0:.1f} KiB/s").format(speed)
83
84 speed /= 1024 # MB
85 if speed < 1024:
86 return QCoreApplication.translate(
87 "DownloadUtilities", "{0:.2f} MiB/s").format(speed)
88
89 speed /= 1024 # GB
90 if speed < 1024:
91 return QCoreApplication.translate(
92 "DownloadUtilities", "{0:.2f} GiB/s").format(speed)

eric ide

mercurial