|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing some utility functions for the Download package. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import QCoreApplication |
|
13 |
|
14 |
|
15 def timeString(timeRemaining): |
|
16 """ |
|
17 Module function to format the given time. |
|
18 |
|
19 @param timeRemaining time to be formatted |
|
20 @type float |
|
21 @return time string |
|
22 @rtype str |
|
23 """ |
|
24 if timeRemaining < 10: |
|
25 return QCoreApplication.translate( |
|
26 "DownloadUtilities", "few seconds remaining") |
|
27 elif timeRemaining < 60: # < 1 minute |
|
28 seconds = int(timeRemaining) |
|
29 return QCoreApplication.translate( |
|
30 "DownloadUtilities", "%n seconds remaining", "", seconds) |
|
31 elif timeRemaining < 3600: # < 1 hour |
|
32 minutes = int(timeRemaining / 60) |
|
33 return QCoreApplication.translate( |
|
34 "DownloadUtilities", "%n minutes remaining", "", minutes) |
|
35 else: |
|
36 hours = int(timeRemaining / 3600) |
|
37 return QCoreApplication.translate( |
|
38 "DownloadUtilities", "%n hours remaining", "", hours) |
|
39 |
|
40 |
|
41 def dataString(size): |
|
42 """ |
|
43 Module function to generate a formatted size string. |
|
44 |
|
45 @param size size to be formatted |
|
46 @type int |
|
47 @return formatted data string |
|
48 @rtype str |
|
49 """ |
|
50 if size < 1024: |
|
51 return QCoreApplication.translate( |
|
52 "DownloadUtilities", "{0:.1f} Bytes").format(size) |
|
53 elif size < 1024 * 1024: |
|
54 size /= 1024 |
|
55 return QCoreApplication.translate( |
|
56 "DownloadUtilities", "{0:.1f} KiB").format(size) |
|
57 elif size < 1024 * 1024 * 1024: |
|
58 size /= 1024 * 1024 |
|
59 return QCoreApplication.translate( |
|
60 "DownloadUtilities", "{0:.2f} MiB").format(size) |
|
61 else: |
|
62 size /= 1024 * 1024 * 1024 |
|
63 return QCoreApplication.translate( |
|
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) |
|
93 |
|
94 return "" |