From cb5b30ab43f37adc45fb6114452b37722c65501e Mon Sep 17 00:00:00 2001 From: Alfredo Deza Date: Fri, 13 Jul 2018 08:54:33 -0400 Subject: [PATCH] ceph-volume util move the str_to_int utility out of lvm Signed-off-by: Alfredo Deza --- src/ceph-volume/ceph_volume/util/__init__.py | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/ceph-volume/ceph_volume/util/__init__.py b/src/ceph-volume/ceph_volume/util/__init__.py index 3b8c30906129d..1da2a3da6a34f 100644 --- a/src/ceph-volume/ceph_volume/util/__init__.py +++ b/src/ceph-volume/ceph_volume/util/__init__.py @@ -1,3 +1,8 @@ +import logging +from math import floor + +logger = logging.getLogger(__name__) + def as_string(string): """ @@ -8,3 +13,22 @@ def as_string(string): # we really ignore here if we can't properly decode with utf-8 return string.decode('utf-8', 'ignore') return string + + +def str_to_int(string, round_down=True): + """ + Parses a string number into an integer, optionally converting to a float + and rounding down. + """ + error_msg = "Unable to convert to integer: '%s'" % str(string) + try: + integer = float(string) + except (TypeError, ValueError): + logger.exception(error_msg) + raise RuntimeError(error_msg) + + if round_down: + integer = floor(integer) + else: + integer = round(integer) + return int(integer) -- 2.39.5