ucloud-api/specs_parser.py

26 lines
874 B
Python
Raw Normal View History

import bitmath
class SpecsParser(object):
def __init__(self, exceptional_devices, allowed_unit = 10):
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))