Revision d0c8c01d lib/utils/text.py
b/lib/utils/text.py | ||
---|---|---|
35 | 35 |
_PARSEUNIT_REGEX = re.compile(r"^([.\d]+)\s*([a-zA-Z]+)?$") |
36 | 36 |
|
37 | 37 |
#: Characters which don't need to be quoted for shell commands |
38 |
_SHELL_UNQUOTED_RE = re.compile('^[-.,=:/_+@A-Za-z0-9]+$')
|
|
38 |
_SHELL_UNQUOTED_RE = re.compile("^[-.,=:/_+@A-Za-z0-9]+$")
|
|
39 | 39 |
|
40 | 40 |
#: MAC checker regexp |
41 | 41 |
_MAC_CHECK_RE = re.compile("^([0-9a-f]{2}:){5}[0-9a-f]{2}$", re.I) |
... | ... | |
143 | 143 |
@return: the formatted value (with suffix) |
144 | 144 |
|
145 | 145 |
""" |
146 |
if units not in ('m', 'g', 't', 'h'):
|
|
146 |
if units not in ("m", "g", "t", "h"):
|
|
147 | 147 |
raise errors.ProgrammerError("Invalid unit specified '%s'" % str(units)) |
148 | 148 |
|
149 |
suffix = ''
|
|
149 |
suffix = ""
|
|
150 | 150 |
|
151 |
if units == 'm' or (units == 'h' and value < 1024):
|
|
152 |
if units == 'h':
|
|
153 |
suffix = 'M'
|
|
151 |
if units == "m" or (units == "h" and value < 1024):
|
|
152 |
if units == "h":
|
|
153 |
suffix = "M"
|
|
154 | 154 |
return "%d%s" % (round(value, 0), suffix) |
155 | 155 |
|
156 |
elif units == 'g' or (units == 'h' and value < (1024 * 1024)):
|
|
157 |
if units == 'h':
|
|
158 |
suffix = 'G'
|
|
156 |
elif units == "g" or (units == "h" and value < (1024 * 1024)):
|
|
157 |
if units == "h":
|
|
158 |
suffix = "G"
|
|
159 | 159 |
return "%0.1f%s" % (round(float(value) / 1024, 1), suffix) |
160 | 160 |
|
161 | 161 |
else: |
162 |
if units == 'h':
|
|
163 |
suffix = 'T'
|
|
162 |
if units == "h":
|
|
163 |
suffix = "T"
|
|
164 | 164 |
return "%0.1f%s" % (round(float(value) / 1024 / 1024, 1), suffix) |
165 | 165 |
|
166 | 166 |
|
... | ... | |
182 | 182 |
if unit: |
183 | 183 |
lcunit = unit.lower() |
184 | 184 |
else: |
185 |
lcunit = 'm'
|
|
185 |
lcunit = "m"
|
|
186 | 186 |
|
187 |
if lcunit in ('m', 'mb', 'mib'):
|
|
187 |
if lcunit in ("m", "mb", "mib"):
|
|
188 | 188 |
# Value already in MiB |
189 | 189 |
pass |
190 | 190 |
|
191 |
elif lcunit in ('g', 'gb', 'gib'):
|
|
191 |
elif lcunit in ("g", "gb", "gib"):
|
|
192 | 192 |
value *= 1024 |
193 | 193 |
|
194 |
elif lcunit in ('t', 'tb', 'tib'):
|
|
194 |
elif lcunit in ("t", "tb", "tib"):
|
|
195 | 195 |
value *= 1024 * 1024 |
196 | 196 |
|
197 | 197 |
else: |
... | ... | |
334 | 334 |
""" |
335 | 335 |
if isinstance(text, unicode): |
336 | 336 |
# only if unicode; if str already, we handle it below |
337 |
text = text.encode('ascii', 'backslashreplace')
|
|
337 |
text = text.encode("ascii", "backslashreplace")
|
|
338 | 338 |
resu = "" |
339 | 339 |
for char in text: |
340 | 340 |
c = ord(char) |
341 |
if char == '\t':
|
|
342 |
resu += r'\t'
|
|
343 |
elif char == '\n':
|
|
344 |
resu += r'\n'
|
|
345 |
elif char == '\r':
|
|
341 |
if char == "\t":
|
|
342 |
resu += r"\t"
|
|
343 |
elif char == "\n":
|
|
344 |
resu += r"\n"
|
|
345 |
elif char == "\r":
|
|
346 | 346 |
resu += r'\'r' |
347 | 347 |
elif c < 32 or c >= 127: # non-printable |
348 | 348 |
resu += "\\x%02x" % (c & 0xff) |
Also available in: Unified diff