]> git.ipfire.org Git - ipfire.org.git/blob - src/backend/util.py
Merge remote-tracking branch 'origin/master'
[ipfire.org.git] / src / backend / util.py
1 #!/usr/bin/python
2
3 import random
4 import string
5
6 def format_size(s, max_unit=None):
7 units = ("B", "kB", "MB", "GB", "TB")
8
9 i = 0
10 while s >= 1024 and i < len(units) - 1:
11 s /= 1024
12 i += 1
13
14 if max_unit and units[i] == max_unit:
15 break
16
17 return "%.0f%s" % (s, units[i])
18
19 def format_time(s, shorter=True):
20 #_ = handler.locale.translate
21 _ = lambda x: x
22
23 hrs, s = divmod(s, 3600)
24 min, s = divmod(s, 60)
25
26 if s >= 30:
27 min += 1
28
29 if shorter and not hrs:
30 return _("%(min)d min") % { "min" : min }
31
32 return _("%(hrs)d:%(min)02d hrs") % {"hrs" : hrs, "min" : min}
33
34 def random_string(length=8):
35 input_chars = string.ascii_letters + string.digits
36
37 r = (random.choice(input_chars) for i in range(length))
38
39 return "".join(r)