From eac7d64456d9aef9461ec98e6c6a185faaaaf621 Mon Sep 17 00:00:00 2001 From: Jan Musial Date: Tue, 29 Mar 2022 09:05:27 +0200 Subject: [PATCH] pyocf: Parse size from string Signed-off-by: Jan Musial --- tests/functional/pyocf/utils.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/functional/pyocf/utils.py b/tests/functional/pyocf/utils.py index bb33017..5497e9e 100644 --- a/tests/functional/pyocf/utils.py +++ b/tests/functional/pyocf/utils.py @@ -61,12 +61,29 @@ class Size: _SECTOR_SIZE = 512 _PAGE_SIZE = 4096 + _unit_mapping = { + "B": 1, + "kiB": _KiB, + "MiB": _MiB, + "GiB": _GiB, + "TiB": _TiB, + } + def __init__(self, b: int, sector_aligned: bool = False): if sector_aligned: self.bytes = int(((b + self._SECTOR_SIZE - 1) // self._SECTOR_SIZE) * self._SECTOR_SIZE) else: 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): return int(self) < int(other)