|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing some utility functions for the Download package. |
|
8 """ |
|
9 |
|
10 from PyQt5.QtCore import QCoreApplication |
|
11 |
|
12 |
|
13 def timeString(timeRemaining): |
|
14 """ |
|
15 Module function to format the given time. |
|
16 |
|
17 @param timeRemaining time to be formatted |
|
18 @type float |
|
19 @return time string |
|
20 @rtype str |
|
21 """ |
|
22 if timeRemaining < 10: |
|
23 return QCoreApplication.translate( |
|
24 "DownloadUtilities", "few seconds remaining") |
|
25 elif timeRemaining < 60: # < 1 minute |
|
26 seconds = int(timeRemaining) |
|
27 return QCoreApplication.translate( |
|
28 "DownloadUtilities", "%n seconds remaining", "", seconds) |
|
29 elif timeRemaining < 3600: # < 1 hour |
|
30 minutes = int(timeRemaining / 60) |
|
31 return QCoreApplication.translate( |
|
32 "DownloadUtilities", "%n minutes remaining", "", minutes) |
|
33 else: |
|
34 hours = int(timeRemaining / 3600) |
|
35 return QCoreApplication.translate( |
|
36 "DownloadUtilities", "%n hours remaining", "", hours) |
|
37 |
|
38 |
|
39 def dataString(size): |
|
40 """ |
|
41 Module function to generate a formatted size string. |
|
42 |
|
43 @param size size to be formatted |
|
44 @type int |
|
45 @return formatted data string |
|
46 @rtype str |
|
47 """ |
|
48 if size < 1024: |
|
49 return QCoreApplication.translate( |
|
50 "DownloadUtilities", "{0:.1f} Bytes").format(size) |
|
51 elif size < 1024 * 1024: |
|
52 size /= 1024 |
|
53 return QCoreApplication.translate( |
|
54 "DownloadUtilities", "{0:.1f} KiB").format(size) |
|
55 elif size < 1024 * 1024 * 1024: |
|
56 size /= 1024 * 1024 |
|
57 return QCoreApplication.translate( |
|
58 "DownloadUtilities", "{0:.2f} MiB").format(size) |
|
59 else: |
|
60 size /= 1024 * 1024 * 1024 |
|
61 return QCoreApplication.translate( |
|
62 "DownloadUtilities", "{0:.2f} GiB").format(size) |
|
63 |
|
64 |
|
65 def speedString(speed): |
|
66 """ |
|
67 Module function to generate a formatted speed string. |
|
68 |
|
69 @param speed speed to be formatted |
|
70 @type float |
|
71 @return formatted speed string |
|
72 @rtype str |
|
73 """ |
|
74 if speed < 0: |
|
75 return QCoreApplication.translate("DownloadUtilities", "Unknown speed") |
|
76 |
|
77 speed /= 1024 # kB |
|
78 if speed < 1024: |
|
79 return QCoreApplication.translate( |
|
80 "DownloadUtilities", "{0:.1f} KiB/s").format(speed) |
|
81 |
|
82 speed /= 1024 # MB |
|
83 if speed < 1024: |
|
84 return QCoreApplication.translate( |
|
85 "DownloadUtilities", "{0:.2f} MiB/s").format(speed) |
|
86 |
|
87 speed /= 1024 # GB |
|
88 if speed < 1024: |
|
89 return QCoreApplication.translate( |
|
90 "DownloadUtilities", "{0:.2f} GiB/s").format(speed) |
|
91 |
|
92 return "" |