2019-07-11 08:34:21 +00:00
|
|
|
import bitmath
|
|
|
|
|
2019-07-18 12:10:17 +00:00
|
|
|
|
2019-07-11 08:34:21 +00:00
|
|
|
class SpecsParser(object):
|
2019-07-18 12:10:17 +00:00
|
|
|
def __init__(self, exceptional_devices, allowed_unit=10):
|
2019-07-11 08:34:21 +00:00
|
|
|
self.exceptional_devices = exceptional_devices
|
|
|
|
self.allowed_unit = allowed_unit
|
|
|
|
|
|
|
|
def transform_specs(self, specs):
|
|
|
|
try:
|
|
|
|
for device in filter(lambda x: x not in self.exceptional_devices, specs):
|
|
|
|
parsed = bitmath.parse_string_unsafe(specs[device])
|
|
|
|
if parsed.base != self.allowed_unit:
|
|
|
|
return False
|
|
|
|
specs[device] = int(parsed.to_Byte())
|
|
|
|
return True
|
|
|
|
except ValueError as _:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_allowed_units(self):
|
|
|
|
if self.allowed_unit == 10:
|
|
|
|
unit_prefix = bitmath.SI_PREFIXES
|
|
|
|
else:
|
|
|
|
unit_prefix = bitmath.NIST_PREFIXES
|
|
|
|
|
|
|
|
return list(map(lambda u: u.upper() + "B", unit_prefix))
|