From: John Mulligan Date: Thu, 17 Aug 2023 18:27:09 +0000 (-0400) Subject: cephadm: move byte string manipulation funcs to data_utils.py X-Git-Tag: v19.0.0~561^2~14 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=8e6cbe86a28a99c654306162e5f81ff15853ec00;p=ceph.git cephadm: move byte string manipulation funcs to data_utils.py Signed-off-by: John Mulligan Pair-programmed-with: Adam King Co-authored-by: Adam King --- diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index 9fb67e9ab117..3a66041ac245 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -107,7 +107,12 @@ from cephadmlib.container_engines import ( check_container_engine, find_container_engine, ) -from cephadmlib.data_utils import dict_get, dict_get_join +from cephadmlib.data_utils import ( + bytes_to_human, + dict_get, + dict_get_join, + with_units_to_int, +) from cephadmlib.file_utils import ( makedirs, populate_files, @@ -6667,27 +6672,6 @@ def command_ls(ctx): print(json.dumps(ls, indent=4)) -def with_units_to_int(v: str) -> int: - if v.endswith('iB'): - v = v[:-2] - elif v.endswith('B'): - v = v[:-1] - mult = 1 - if v[-1].upper() == 'K': - mult = 1024 - v = v[:-1] - elif v[-1].upper() == 'M': - mult = 1024 * 1024 - v = v[:-1] - elif v[-1].upper() == 'G': - mult = 1024 * 1024 * 1024 - v = v[:-1] - elif v[-1].upper() == 'T': - mult = 1024 * 1024 * 1024 * 1024 - v = v[:-1] - return int(float(v) * mult) - - def list_daemons(ctx, detail=True, legacy_dir=None): # type: (CephadmContext, bool, Optional[str]) -> List[Dict[str, str]] host_version: Optional[str] = None @@ -8470,32 +8454,6 @@ def command_rescan_disks(ctx: CephadmContext) -> str: ################################## -def bytes_to_human(num, mode='decimal'): - # type: (float, str) -> str - """Convert a bytes value into it's human-readable form. - - :param num: number, in bytes, to convert - :param mode: Either decimal (default) or binary to determine divisor - :returns: string representing the bytes value in a more readable format - """ - unit_list = ['', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB'] - divisor = 1000.0 - yotta = 'YB' - - if mode == 'binary': - unit_list = ['', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB'] - divisor = 1024.0 - yotta = 'YiB' - - for unit in unit_list: - if abs(num) < divisor: - return '%3.1f%s' % (num, unit) - num /= divisor - return '%.1f%s' % (num, yotta) - -################################## - - class Enclosure: def __init__(self, enc_id: str, enc_path: str, dev_path: str): """External disk enclosure metadata diff --git a/src/cephadm/cephadmlib/data_utils.py b/src/cephadm/cephadmlib/data_utils.py index 9453f77398f2..c5c33f8e142e 100644 --- a/src/cephadm/cephadmlib/data_utils.py +++ b/src/cephadm/cephadmlib/data_utils.py @@ -39,3 +39,48 @@ def dict_get_join(d: Dict[str, Any], key: str) -> Any: if isinstance(value, list): value = '\n'.join(map(str, value)) return value + + +def bytes_to_human(num, mode='decimal'): + # type: (float, str) -> str + """Convert a bytes value into it's human-readable form. + + :param num: number, in bytes, to convert + :param mode: Either decimal (default) or binary to determine divisor + :returns: string representing the bytes value in a more readable format + """ + unit_list = ['', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB'] + divisor = 1000.0 + yotta = 'YB' + + if mode == 'binary': + unit_list = ['', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB'] + divisor = 1024.0 + yotta = 'YiB' + + for unit in unit_list: + if abs(num) < divisor: + return '%3.1f%s' % (num, unit) + num /= divisor + return '%.1f%s' % (num, yotta) + + +def with_units_to_int(v: str) -> int: + if v.endswith('iB'): + v = v[:-2] + elif v.endswith('B'): + v = v[:-1] + mult = 1 + if v[-1].upper() == 'K': + mult = 1024 + v = v[:-1] + elif v[-1].upper() == 'M': + mult = 1024 * 1024 + v = v[:-1] + elif v[-1].upper() == 'G': + mult = 1024 * 1024 * 1024 + v = v[:-1] + elif v[-1].upper() == 'T': + mult = 1024 * 1024 * 1024 * 1024 + v = v[:-1] + return int(float(v) * mult)