]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cephadm: move byte string manipulation funcs to data_utils.py
authorJohn Mulligan <jmulligan@redhat.com>
Thu, 17 Aug 2023 18:27:09 +0000 (14:27 -0400)
committerJohn Mulligan <jmulligan@redhat.com>
Wed, 30 Aug 2023 18:00:47 +0000 (14:00 -0400)
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Pair-programmed-with: Adam King <adking@redhat.com>
Co-authored-by: Adam King <adking@redhat.com>
src/cephadm/cephadm.py
src/cephadm/cephadmlib/data_utils.py

index 9fb67e9ab1177253ca1f07219a73f81bce403cde..3a66041ac245ab59c7519760e76398d42aa0e25a 100755 (executable)
@@ -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
index 9453f77398f266746c9ea2967746be72f12b2e6e..c5c33f8e142e2b284de388c73ff070c60fb3755c 100644 (file)
@@ -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)