Show unit and use IEC standard for human readable option

This commit is contained in:
Jerome Lebleu 2013-12-03 17:33:21 +01:00
parent c745298404
commit f3c36b8f38

View file

@ -137,7 +137,7 @@ def monitor_disk(units=None, mountpoint=None, human_readable=False):
del d['device_name'] del d['device_name']
if human_readable: if human_readable:
for i in ['used', 'avail', 'size']: for i in ['used', 'avail', 'size']:
d[i] = _bytes_to_human(d[i]) d[i] = _binary_to_human(d[i]) + 'B'
if len(units) > 1: if len(units) > 1:
result[dn][u] = d result[dn][u] = d
else: else:
@ -185,7 +185,7 @@ def monitor_network(units=None, human_readable=False):
if human_readable: if human_readable:
for k in i.keys(): for k in i.keys():
if k != 'time_since_update': if k != 'time_since_update':
i[k] = _bytes_to_human(i[k], True) i[k] = _binary_to_human(i[k]) + 'B'
result[u][iname] = i result[u][iname] = i
elif u == 'infos': elif u == 'infos':
try: try:
@ -240,10 +240,10 @@ def monitor_system(units=None, human_readable=False):
if human_readable: if human_readable:
for i in ram.keys(): for i in ram.keys():
if i != 'percent': if i != 'percent':
ram[i] = _bytes_to_human(ram[i]) ram[i] = _binary_to_human(ram[i]) + 'B'
for i in swap.keys(): for i in swap.keys():
if i != 'percent': if i != 'percent':
swap[i] = _bytes_to_human(swap[i]) swap[i] = _binary_to_human(swap[i]) + 'B'
result[u] = { result[u] = {
'ram': ram, 'ram': ram,
'swap': swap 'swap': swap
@ -332,23 +332,23 @@ def _extract_inet(string):
return None return None
def _bytes_to_human(n, bits=False): def _binary_to_human(n, customary=False):
""" """
Convert bytes (or bits) into human readable format Convert bytes or bits into human readable format with binary prefix
Keyword argument: Keyword argument:
n -- Number to convert n -- Number to convert
bits -- Process n as bits customary -- Use customary symbol instead of IEC standard
""" """
symbols = ('B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y') symbols = ('Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi')
if bits: if customary:
symbols = ('b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb') symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
prefix = {} prefix = {}
for i, s in enumerate(symbols): for i, s in enumerate(symbols):
prefix[s] = 1 << i*10 prefix[s] = 1 << (i+1)*10
for s in reversed(symbols): for s in reversed(symbols):
if n >= prefix[s]: if n >= prefix[s]:
value = float(n) / prefix[s] value = float(n) / prefix[s]
return '%.1f%s' % (value, s) return '%.1f%s' % (value, s)
return "%s%s" % (n, symbols[0]) return "%s" % n