root / snf-common / synnefo / util / units.py @ 362dadaa
History | View | Annotate | Download (4.2 kB)
1 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from synnefo.lib.ordereddict import OrderedDict |
35 |
import re |
36 |
|
37 |
PRACTICALLY_INFINITE = 2**63 - 1 |
38 |
DEFAULT_PARSE_BASE = 1024
|
39 |
PARSE_EXPONENTS = { |
40 |
'': 0, |
41 |
'bytes': 0, |
42 |
'K': 1, |
43 |
'KB': 1, |
44 |
'KIB': 1, |
45 |
'M': 2, |
46 |
'MB': 2, |
47 |
'MIB': 2, |
48 |
'G': 3, |
49 |
'GB': 3, |
50 |
'GIB': 3, |
51 |
'T': 4, |
52 |
'TB': 4, |
53 |
'TIB': 4, |
54 |
'P': 5, |
55 |
'PB': 5, |
56 |
'PIB': 5, |
57 |
} |
58 |
|
59 |
_MATCHER = re.compile('^(\d+\.?\d*)(.*)$')
|
60 |
|
61 |
|
62 |
class ParseError(Exception): |
63 |
pass
|
64 |
|
65 |
|
66 |
def _parse_number_with_unit(s): |
67 |
match = _MATCHER.match(s) |
68 |
if not match: |
69 |
raise ParseError()
|
70 |
number, unit = match.groups() |
71 |
try:
|
72 |
number = long(number)
|
73 |
except ValueError: |
74 |
number = float(number)
|
75 |
|
76 |
return number, unit.strip().upper()
|
77 |
|
78 |
|
79 |
def parse_with_style(s): |
80 |
if isinstance(s, (int, long)): |
81 |
return s, 0 |
82 |
|
83 |
if s in ['inf', 'infinite']: |
84 |
return PRACTICALLY_INFINITE, 0 |
85 |
|
86 |
n, unit = _parse_number_with_unit(s) |
87 |
try:
|
88 |
exponent = PARSE_EXPONENTS[unit] |
89 |
except KeyError: |
90 |
raise ParseError()
|
91 |
multiplier = DEFAULT_PARSE_BASE ** exponent |
92 |
return long(n * multiplier), exponent |
93 |
|
94 |
|
95 |
def parse(s): |
96 |
n, _ = parse_with_style(s) |
97 |
return n
|
98 |
|
99 |
|
100 |
UNITS = { |
101 |
'bytes': {
|
102 |
'DISPLAY': ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'], |
103 |
'BASE': 1024, |
104 |
} |
105 |
} |
106 |
|
107 |
STYLE_TO_EXP = OrderedDict( |
108 |
[('b', 0), |
109 |
('kb', 1), |
110 |
('mb', 2), |
111 |
('gb', 3), |
112 |
('tb', 4), |
113 |
('pb', 5), |
114 |
] |
115 |
) |
116 |
|
117 |
STYLES = STYLE_TO_EXP.keys() + ['auto', 'none'] |
118 |
|
119 |
|
120 |
class StyleError(Exception): |
121 |
pass
|
122 |
|
123 |
|
124 |
def show_float(n): |
125 |
if n < 1: |
126 |
return "%.3f" % n |
127 |
if n < 10: |
128 |
return "%.2f" % n |
129 |
return "%.1f" % n |
130 |
|
131 |
|
132 |
def get_exponent(style): |
133 |
if isinstance(style, (int, long)): |
134 |
if style in STYLE_TO_EXP.values(): |
135 |
return style
|
136 |
else:
|
137 |
raise StyleError()
|
138 |
else:
|
139 |
try:
|
140 |
return STYLE_TO_EXP[style]
|
141 |
except KeyError: |
142 |
raise StyleError()
|
143 |
|
144 |
|
145 |
def show(n, unit, style=None): |
146 |
if style == 'none': |
147 |
return str(n) |
148 |
|
149 |
if n == PRACTICALLY_INFINITE:
|
150 |
return 'inf' |
151 |
|
152 |
try:
|
153 |
unit_dict = UNITS[unit] |
154 |
except KeyError: |
155 |
return str(n) |
156 |
|
157 |
BASE = unit_dict['BASE']
|
158 |
DISPLAY = unit_dict['DISPLAY']
|
159 |
|
160 |
if style is None or style == 'auto': |
161 |
if n < BASE:
|
162 |
return "%d %s" % (n, DISPLAY[0]) |
163 |
n = float(n)
|
164 |
for i in DISPLAY[1:]: |
165 |
n = n / BASE |
166 |
if n < BASE:
|
167 |
break
|
168 |
return "%s %s" % (show_float(n), i) |
169 |
|
170 |
exponent = get_exponent(style) |
171 |
unit_display = DISPLAY[exponent] |
172 |
if exponent == 0: |
173 |
return "%d %s" % (n, unit_display) |
174 |
|
175 |
divisor = BASE ** exponent |
176 |
n = float(n)
|
177 |
n = n / divisor |
178 |
return "%s %s" % (show_float(n), unit_display) |