|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 - 2016 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 (float) |
|
20 @return time string (string) |
|
21 """ |
|
22 if timeRemaining > 60: |
|
23 minutes = int(timeRemaining / 60) |
|
24 seconds = int(timeRemaining % 60) |
|
25 remaining = QCoreApplication.translate( |
|
26 "DownloadUtilities", |
|
27 "%n:{0:02} minutes remaining""", "", |
|
28 minutes).format(seconds) |
|
29 else: |
|
30 seconds = int(timeRemaining) |
|
31 remaining = QCoreApplication.translate( |
|
32 "DownloadUtilities", |
|
33 "%n seconds remaining", "", seconds) |
|
34 |
|
35 return remaining |
|
36 |
|
37 |
|
38 def dataString(size): |
|
39 """ |
|
40 Module function to generate a formatted size string. |
|
41 |
|
42 @param size size to be formatted (integer) |
|
43 @return formatted data string (string) |
|
44 """ |
|
45 unit = "" |
|
46 if size < 1024: |
|
47 unit = QCoreApplication.translate("DownloadUtilities", "Bytes") |
|
48 elif size < 1024 * 1024: |
|
49 size /= 1024 |
|
50 unit = QCoreApplication.translate("DownloadUtilities", "KiB") |
|
51 elif size < 1024 * 1024 * 1024: |
|
52 size /= 1024 * 1024 |
|
53 unit = QCoreApplication.translate("DownloadUtilities", "MiB") |
|
54 else: |
|
55 size /= 1024 * 1024 * 1024 |
|
56 unit = QCoreApplication.translate("DownloadUtilities", "GiB") |
|
57 return "{0:.1f} {1}".format(size, unit) |