From: Mohamad Gebai Date: Sun, 31 Mar 2019 17:06:43 +0000 (-0400) Subject: ceph-volume: add function to convert from a readable string to Size X-Git-Tag: v13.2.9~109^2~4 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=c726620764ec4100d3acf7df5f1a8a659673ad1a;p=ceph.git ceph-volume: add function to convert from a readable string to Size Signed-off-by: Mohamad Gebai (cherry picked from commit 50428062175623d561be01a8124e010a61221845) --- diff --git a/src/ceph-volume/ceph_volume/util/disk.py b/src/ceph-volume/ceph_volume/util/disk.py index 8ac215b8821b..0be90aecc015 100644 --- a/src/ceph-volume/ceph_volume/util/disk.py +++ b/src/ceph-volume/ceph_volume/util/disk.py @@ -687,6 +687,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):