pyocf: Parse size from string

Signed-off-by: Jan Musial <jan.musial@intel.com>
This commit is contained in:
Jan Musial 2022-03-29 09:05:27 +02:00
parent eb5f4f79e4
commit eac7d64456

View File

@ -61,12 +61,29 @@ class Size:
_SECTOR_SIZE = 512 _SECTOR_SIZE = 512
_PAGE_SIZE = 4096 _PAGE_SIZE = 4096
_unit_mapping = {
"B": 1,
"kiB": _KiB,
"MiB": _MiB,
"GiB": _GiB,
"TiB": _TiB,
}
def __init__(self, b: int, sector_aligned: bool = False): def __init__(self, b: int, sector_aligned: bool = False):
if sector_aligned: if sector_aligned:
self.bytes = int(((b + self._SECTOR_SIZE - 1) // self._SECTOR_SIZE) * self._SECTOR_SIZE) self.bytes = int(((b + self._SECTOR_SIZE - 1) // self._SECTOR_SIZE) * self._SECTOR_SIZE)
else: else:
self.bytes = int(b) self.bytes = int(b)
@classmethod
def from_string(cls, string):
string = string.strip()
number, unit = string.split(" ")
number = float(number)
unit = cls._unit_mapping[unit]
return cls(int(number * unit))
def __lt__(self, other): def __lt__(self, other):
return int(self) < int(other) return int(self) < int(other)