]>
git.proxmox.com Git - ceph.git/blob - ceph/src/ceph-volume/ceph_volume/util/__init__.py
3 from ceph_volume
import terminal
6 logger
= logging
.getLogger(__name__
)
11 Ensure that whatever type of string is incoming, it is returned as an
12 actual string, versus 'bytes' which Python 3 likes to use.
14 if isinstance(string
, bytes
):
15 # we really ignore here if we can't properly decode with utf-8
16 return string
.decode('utf-8', 'ignore')
22 Ensure that whatever type of string is incoming, it is returned as bytes,
23 encoding to utf-8 otherwise
25 if isinstance(string
, bytes
):
27 return string
.encode('utf-8', errors
='ignore')
30 def str_to_int(string
, round_down
=True):
32 Parses a string number into an integer, optionally converting to a float
35 error_msg
= "Unable to convert to integer: '%s'" % str(string
)
37 integer
= float(string
)
38 except (TypeError, ValueError):
39 logger
.exception(error_msg
)
40 raise RuntimeError(error_msg
)
43 integer
= floor(integer
)
45 integer
= round(integer
)
51 Convert a string representation of truth to True or False
53 True values are 'y', 'yes', or ''; case-insensitive
54 False values are 'n', or 'no'; case-insensitive
55 Raises ValueError if 'val' is anything else.
57 true_vals
= ['yes', 'y', '']
58 false_vals
= ['no', 'n']
61 except AttributeError:
62 val
= str(val
).lower()
65 elif val
in false_vals
:
68 raise ValueError("Invalid input value: %s" % val
)
71 def prompt_bool(question
, _raw_input
=None):
73 Interface to prompt a boolean (or boolean-like) response from a user.
74 Usually a confirmation.
76 input_prompt
= _raw_input
or raw_input
77 prompt_format
= '--> {question} '.format(question
=question
)
78 response
= input_prompt(prompt_format
)
80 return str_to_bool(response
)
82 terminal
.error('Valid true responses are: y, yes, <Enter>')
83 terminal
.error('Valid false responses are: n, no')
84 terminal
.error('That response was invalid, please try again')
85 return prompt_bool(question
, _raw_input
=input_prompt
)