]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
ceph-volume: add function to convert from a readable string to Size
authorMohamad Gebai <mgebai@suse.com>
Sun, 31 Mar 2019 17:06:43 +0000 (13:06 -0400)
committerMohamad Gebai <mgebai@suse.com>
Fri, 12 Apr 2019 16:54:08 +0000 (12:54 -0400)
Signed-off-by: Mohamad Gebai <mgebai@suse.com>
src/ceph-volume/ceph_volume/util/disk.py

index 95d13ac7962be64885415f4e137535c752858dd8..98b482be3b3b0767702d5e8bd2d3d507c95ac8b4 100644 (file)
@@ -683,6 +683,26 @@ def human_readable_size(size):
         suffix=suffixes[suffix_index])
 
 
+def size_from_human_readable(s):
+    """
+    Takes a human readable string and converts into a Size. If no unit is
+    passed, bytes is assumed.
+    """
+    s = s.replace(' ', '')
+    if s[-1].isdigit():
+        return Size(b=float(s))
+    n = float(s[:-1])
+    if s[-1].lower() == 't':
+        return Size(tb=n)
+    if s[-1].lower() == 'g':
+        return Size(gb=n)
+    if s[-1].lower() == 'm':
+        return Size(mb=n)
+    if s[-1].lower() == 'k':
+        return Size(kb=n)
+    return None
+
+
 def get_partitions_facts(sys_block_path):
     partition_metadata = {}
     for folder in os.listdir(sys_block_path):