]> git.proxmox.com Git - ceph.git/blob - ceph/src/ceph-volume/ceph_volume/util/__init__.py
bump version to 15.2.4-pve1
[ceph.git] / ceph / src / ceph-volume / ceph_volume / util / __init__.py
1 import logging
2 from math import floor
3 from ceph_volume import terminal
4
5 try:
6 input = raw_input # pylint: disable=redefined-builtin
7 except NameError:
8 pass
9
10 logger = logging.getLogger(__name__)
11
12
13 def as_string(string):
14 """
15 Ensure that whatever type of string is incoming, it is returned as an
16 actual string, versus 'bytes' which Python 3 likes to use.
17 """
18 if isinstance(string, bytes):
19 # we really ignore here if we can't properly decode with utf-8
20 return string.decode('utf-8', 'ignore')
21 return string
22
23
24 def as_bytes(string):
25 """
26 Ensure that whatever type of string is incoming, it is returned as bytes,
27 encoding to utf-8 otherwise
28 """
29 if isinstance(string, bytes):
30 return string
31 return string.encode('utf-8', errors='ignore')
32
33
34 def str_to_int(string, round_down=True):
35 """
36 Parses a string number into an integer, optionally converting to a float
37 and rounding down.
38
39 Some LVM values may come with a comma instead of a dot to define decimals.
40 This function normalizes a comma into a dot
41 """
42 error_msg = "Unable to convert to integer: '%s'" % str(string)
43 try:
44 integer = float(string.replace(',', '.'))
45 except AttributeError:
46 # this might be a integer already, so try to use it, otherwise raise
47 # the original exception
48 if isinstance(string, (int, float)):
49 integer = string
50 else:
51 logger.exception(error_msg)
52 raise RuntimeError(error_msg)
53 except (TypeError, ValueError):
54 logger.exception(error_msg)
55 raise RuntimeError(error_msg)
56
57 if round_down:
58 integer = floor(integer)
59 else:
60 integer = round(integer)
61 return int(integer)
62
63
64 def str_to_bool(val):
65 """
66 Convert a string representation of truth to True or False
67
68 True values are 'y', 'yes', or ''; case-insensitive
69 False values are 'n', or 'no'; case-insensitive
70 Raises ValueError if 'val' is anything else.
71 """
72 true_vals = ['yes', 'y', '']
73 false_vals = ['no', 'n']
74 try:
75 val = val.lower()
76 except AttributeError:
77 val = str(val).lower()
78 if val in true_vals:
79 return True
80 elif val in false_vals:
81 return False
82 else:
83 raise ValueError("Invalid input value: %s" % val)
84
85
86 def prompt_bool(question, input_=None):
87 """
88 Interface to prompt a boolean (or boolean-like) response from a user.
89 Usually a confirmation.
90 """
91 input_prompt = input_ or input
92 prompt_format = '--> {question} '.format(question=question)
93 response = input_prompt(prompt_format)
94 try:
95 return str_to_bool(response)
96 except ValueError:
97 terminal.error('Valid true responses are: y, yes, <Enter>')
98 terminal.error('Valid false responses are: n, no')
99 terminal.error('That response was invalid, please try again')
100 return prompt_bool(question, input_=input_prompt)