]>
git.proxmox.com Git - ceph.git/blob - ceph/src/ceph-volume/ceph_volume/util/__init__.py
3 from ceph_volume
import terminal
6 input = raw_input # pylint: disable=redefined-builtin
10 logger
= logging
.getLogger(__name__
)
13 def as_string(string
):
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.
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')
26 Ensure that whatever type of string is incoming, it is returned as bytes,
27 encoding to utf-8 otherwise
29 if isinstance(string
, bytes
):
31 return string
.encode('utf-8', errors
='ignore')
34 def str_to_int(string
, round_down
=True):
36 Parses a string number into an integer, optionally converting to a float
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
42 error_msg
= "Unable to convert to integer: '%s'" % str(string
)
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)):
51 logger
.exception(error_msg
)
52 raise RuntimeError(error_msg
)
53 except (TypeError, ValueError):
54 logger
.exception(error_msg
)
55 raise RuntimeError(error_msg
)
58 integer
= floor(integer
)
60 integer
= round(integer
)
66 Convert a string representation of truth to True or False
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.
72 true_vals
= ['yes', 'y', '']
73 false_vals
= ['no', 'n']
76 except AttributeError:
77 val
= str(val
).lower()
80 elif val
in false_vals
:
83 raise ValueError("Invalid input value: %s" % val
)
86 def prompt_bool(question
, input_
=None):
88 Interface to prompt a boolean (or boolean-like) response from a user.
89 Usually a confirmation.
91 input_prompt
= input_
or input
92 prompt_format
= '--> {question} '.format(question
=question
)
93 response
= input_prompt(prompt_format
)
95 return str_to_bool(response
)
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
)