]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/util/abbreviate.py
remove humanize, use internal method, teach internal method to understand timedelta
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / util / abbreviate.py
1
2 import re
3 from datetime import timedelta
4
5 HOUR = 3600
6 DAY = 24*3600
7 WEEK = 7*DAY
8 MONTH = 30*DAY
9 YEAR = 365*DAY
10
11 def abbreviate_time(s):
12     postfix = ''
13     if isinstance(s, timedelta):
14         s = s.total_seconds()
15         if s >= 0.0:
16             postfix = ' ago'
17         else:
18             postfix = ' in the future'
19     def _plural(count, unit):
20         count = int(count)
21         if count == 1:
22             return "%d %s%s" % (count, unit, postfix)
23         return "%d %ss%s" % (count, unit, postfix)
24     if s is None:
25         return "unknown"
26     if s < 120:
27         return _plural(s, "second")
28     if s < 3*HOUR:
29         return _plural(s/60, "minute")
30     if s < 2*DAY:
31         return _plural(s/HOUR, "hour")
32     if s < 2*MONTH:
33         return _plural(s/DAY, "day")
34     if s < 4*YEAR:
35         return _plural(s/MONTH, "month")
36     return _plural(s/YEAR, "year")
37
38 def abbreviate_space(s, SI=True):
39     if s is None:
40         return "unknown"
41     if SI:
42         U = 1000.0
43         isuffix = "B"
44     else:
45         U = 1024.0
46         isuffix = "iB"
47     def r(count, suffix):
48         return "%.2f %s%s" % (count, suffix, isuffix)
49
50     if s < 1024: # 1000-1023 get emitted as bytes, even in SI mode
51         return "%d B" % s
52     if s < U*U:
53         return r(s/U, "k")
54     if s < U*U*U:
55         return r(s/(U*U), "M")
56     if s < U*U*U*U:
57         return r(s/(U*U*U), "G")
58     if s < U*U*U*U*U:
59         return r(s/(U*U*U*U), "T")
60     if s < U*U*U*U*U*U:
61         return r(s/(U*U*U*U*U), "P")
62     return r(s/(U*U*U*U*U*U), "E")
63
64 def abbreviate_space_both(s):
65     return "(%s, %s)" % (abbreviate_space(s, True),
66                          abbreviate_space(s, False))
67
68 def parse_abbreviated_size(s):
69     if s is None or s == "":
70         return None
71     m = re.match(r"^(\d+)([KMGTPE]?[I]?[B]?)$", s.upper())
72     if not m:
73         raise ValueError("unparseable value %s" % s)
74     number, suffix = m.groups()
75     if suffix.endswith("B"):
76         suffix = suffix[:-1]
77     multiplier = {"":   1,
78                   "I":  1,
79                   "K":  1000,
80                   "M":  1000 * 1000,
81                   "G":  1000 * 1000 * 1000,
82                   "T":  1000 * 1000 * 1000 * 1000,
83                   "P":  1000 * 1000 * 1000 * 1000 * 1000,
84                   "E":  1000 * 1000 * 1000 * 1000 * 1000 * 1000,
85                   "KI": 1024,
86                   "MI": 1024 * 1024,
87                   "GI": 1024 * 1024 * 1024,
88                   "TI": 1024 * 1024 * 1024 * 1024,
89                   "PI": 1024 * 1024 * 1024 * 1024 * 1024,
90                   "EI": 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
91                   }[suffix]
92     return int(number) * multiplier