]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-volume util move the str_to_int utility out of lvm
authorAlfredo Deza <adeza@redhat.com>
Fri, 13 Jul 2018 12:54:33 +0000 (08:54 -0400)
committerAlfredo Deza <adeza@redhat.com>
Tue, 17 Jul 2018 18:42:59 +0000 (14:42 -0400)
Signed-off-by: Alfredo Deza <adeza@redhat.com>
src/ceph-volume/ceph_volume/util/__init__.py

index 3b8c30906129db18092d951dbcdd8f16f92cb0f1..1da2a3da6a34fe811408c483b7cb34908f67e00c 100644 (file)
@@ -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)