size converter done

This commit is contained in:
brent s 2019-10-30 03:29:12 -04:00
parent f96c815d8d
commit f0d93658d0
3 changed files with 152 additions and 45 deletions

View File

@ -0,0 +1,5 @@
from . import block
from . import filesystem
from . import luks
from . import lvm
from . import mdadm

View File

@ -0,0 +1,2 @@
from . import locales
from . import users

View File

@ -1,3 +1,5 @@
import re

def xmlBool(xmlobj): def xmlBool(xmlobj):
# https://bugs.launchpad.net/lxml/+bug/1850221 # https://bugs.launchpad.net/lxml/+bug/1850221
if isinstance(xmlobj, bool): if isinstance(xmlobj, bool):
@ -9,66 +11,164 @@ def xmlBool(xmlobj):
else: else:
return(None) return(None)


class _Sizer(object):
def __init__(self): def collapseKeys(d, keylist = None):
def _getKeys(d, keylist = None):
if not keylist: if not keylist:
keylist = [] keylist = []
for k, v in d.items(): for k, v in d.items():
if isinstance(v, dict): if isinstance(v, dict):
keylist.append(k) keylist.append(k)
keylist = _getKeys(v, keylist = keylist) keylist = collapseKeys(v, keylist = keylist)
else: else:
keylist.append(k) keylist.append(k)
return(keylist) return(keylist)


def collapseValues(d, vallist = None):
if not vallist:
vallist = []
for k, v in d.items():
if isinstance(v, dict):
vallist = collapseValues(v, vallist = vallist)
else:
vallist.append(v)
return(vallist)


class _Sizer(object):
def __init__(self):
# We use different methods for converting between storage and BW, and different multipliers for each subtype. # We use different methods for converting between storage and BW, and different multipliers for each subtype.
# https://stackoverflow.com/questions/5194057/better-way-to-convert-file-sizes-in-python # https://stackoverflow.com/questions/5194057/better-way-to-convert-file-sizes-in-python
# https://en.wikipedia.org/wiki/Orders_of_magnitude_(data) # https://en.wikipedia.org/wiki/Orders_of_magnitude_(data)
# https://en.wikipedia.org/wiki/Binary_prefix # https://en.wikipedia.org/wiki/Binary_prefix
self.storageUnits = {'decimal': {'B': 0, # 'decimal' is base-10, 'binary' is base-2. (Duh.)
'kB': 7, # Kilobyte # "b" = bytes, "n" = given value, and "u" = unit suffix's key in below notes.
'MB': 17, # Megabyte... self.storageUnits = {'decimal': { # n * (10 ** u) = b; b / (10 ** u) = u
'GB': 27, 0: (None, 'B', 'byte'),
'TB': 37}, 3: ('k', 'kB', 'kilobyte'),
'binary': {'KiB': 10, # Kibibyte 6: ('M', 'MB', 'megabyte'),
'MiB': 20, # Mebibyte... 9: ('G', 'GB', 'gigabyte'),
'GiB': 30, 12: ('T', 'TB', 'teraybte'),
'TiB': 40}} 13: ('P', 'PB', 'petabyte'), # yeah, right.
# https://en.wikipedia.org/wiki/Bit#Multiple_bits 15: ('E', 'EB', 'exabyte'),
self.bwUnits = {'b': None, 18: ('Z', 'ZB', 'zettabyte'),
'bit': None, 19: ('Y', 'YB', 'yottabyte')
'k': } },
self.valid_storage = list(self.storageUnits.keys()) 'binary': { # n * (2 ** u) = b; b / (2 ** u) = u
self.valid_storage.insert('nibble') -1: ('nybble', 'nibble', 'nyble', 'half-byte', 'tetrade', 'nibble'),
self.valid_bw = _getKeys(self.bwUnits) 10: ('Ki', 'KiB', 'kibibyte'),
20: ('Mi', 'MiB', 'mebibyte'),
30: ('Gi', 'GiB', 'gibibyte'),
40: ('Ti', 'TiB', 'tebibyte'),
50: ('Pi', 'PiB', 'pebibyte'),
60: ('Ei', 'EiB', 'exbibyte'),
70: ('Zi', 'ZiB', 'zebibyte'),
80: ('Yi', 'YiB', 'yobibyte')
}}
# https://en.wikipedia.org/wiki/Bit#Multiple_bits - note that 8 bits = 1 byte
self.bwUnits = {'decimal': { # n * (10 ** u) = b; b / (10 ** u) = u
0: (None, 'b', 'bit'),
3: ('k', 'kb', 'kilobit'),
6: ('M', 'Mb', 'megabit'),
9: ('G', 'Gb', 'gigabit'),
12: ('T', 'Tb', 'terabit'),
13: ('P', 'Pb', 'petabit'),
15: ('E', 'Eb', 'exabit'),
18: ('Z', 'Zb', 'zettabit'),
19: ('Y', 'Yb', 'yottabit')
},
'binary': { # n * (2 ** u) = b; b / (2 ** u) = u
-1: ('semi-octet', 'quartet', 'quadbit'),
10: ('Ki', 'Kib', 'kibibit'),
20: ('Mi', 'Mib', 'mebibit'),
30: ('Gi', 'Gib', 'gibibit'),
40: ('Ti', 'Tib', 'tebibit'),
50: ('Pi', 'Pib', 'pebibit'),
60: ('Ei', 'Eib', 'exbibit'),
70: ('Zi', 'Zib', 'zebibit'),
80: ('Yi', 'Yib', 'yobibit')
}}
self.valid_storage = []
for unit_type, convpair in self.storageUnits.items():
for f, l in convpair.items():
for suffix in l:
if suffix not in self.valid_storage:
self.valid_storage.append(suffix)
self.valid_bw = []
for unit_type, convpair in self.bwUnits.items():
for f, l in convpair.items():
for suffix in l:
if suffix not in self.valid_bw:
self.valid_bw.append(suffix)


def convert(self, n, suffix, target = None): def convert(self, n, suffix):
pass conversion = {}
if suffix in self.valid_storage:
conversion.update(self.convertStorage(n, suffix))
b = conversion['B'] * 8
conversion.update(self.convertBW(b, 'b'))
elif suffix in self.valid_bw:
conversion.update(self.convertBW(n, suffix))
b = conversion['b'] / 8
conversion.update(self.convertStorage(b, 'B'))
return(conversion)


def convertBW(self, n, suffix, target = None): def convertBW(self, n, suffix, target = None):
inBits = n
if suffix not in self.valid_bw:
raise ValueError('suffix must be one of {0}'.format(', '.format(self.valid_bw)))
if suffix != 'b':
if self.bwUnits[suffix]:
inBits = n * (10 ** self.bwUnits[suffix])
else:
inBits = None inBits = None
conversion = None
base_factors = []
if suffix not in self.valid_bw:
raise ValueError('suffix is not a valid unit notation for this conversion')
if target and target not in self.valid_bw:
raise ValueError('target is not a valid unit notation for this conversion')
for (_unit_type, _base) in (('decimal', 10), ('binary', 2)):
if target and base_factors:
break
for u, suffixes in self.bwUnits[_unit_type].items():
if all((target, inBits, base_factors)):
break
if suffix in suffixes:
inBits = n * float(_base ** u)
if target and target in suffixes:
base_factors.append((_base, u, suffixes[1]))
elif not target:
base_factors.append((_base, u, suffixes[1]))
if target:
conversion = float(inBits) / float(base_factors[0][0] ** base_factors[0][1])
else:
if not isinstance(conversion, dict):
conversion = {}
for base, factor, suffix in base_factors:
conversion[suffix] = float(inBits) / float(base ** factor)
return(conversion)


def convertStorage(self, n, suffix, target = None): def convertStorage(self, n, suffix, target = None):
inBytes = n inBytes = None
conversion = None
base_factors = []
if suffix not in self.valid_storage: if suffix not in self.valid_storage:
raise ValueError('suffix must be one of {0}'.format(', '.format(self.valid_storage))) raise ValueError('suffix is not a valid unit notation for this conversion')
if suffix == 'nibble': if target and target not in self.valid_storage:
inBytes = n * 0.5 raise ValueError('target is not a valid unit notation for this conversion')
elif suffix != 'B': for (_unit_type, _base) in (('decimal', 10), ('binary', 2)):
inBytes = float(n << self.storageUnits[suffix]) if target and base_factors:
break
for u, suffixes in self.storageUnits[_unit_type].items():
if all((target, inBytes, base_factors)):
break
if suffix in suffixes:
inBytes = n * float(_base ** u)
if target and target in suffixes:
base_factors.append((_base, u, suffixes[1]))
elif not target:
base_factors.append((_base, u, suffixes[1]))
if target: if target:
conversion = float(inBytes / float(1 << self.storageUnits[target])) conversion = float(inBytes) / float(base_factors[0][0] ** base_factors[0][1])
else: else:
if not isinstance(conversion, dict):
conversion = {} conversion = {}
for unit, shifter in self.storageUnits.items(): for base, factor, suffix in base_factors:
conversion[unit] = float(inBytes / float(1 << self.storageUnits[unit])) conversion[suffix] = float(inBytes) / float(base ** factor)
return(conversion) return(conversion)