2021-10-26 10:19:02 +00:00
|
|
|
import re
|
2021-11-02 16:21:01 +00:00
|
|
|
from typing import Any, Type, Optional
|
2021-10-20 12:32:45 +00:00
|
|
|
|
|
|
|
import click
|
2021-10-27 11:33:26 +00:00
|
|
|
import ruamel.yaml
|
2021-10-28 11:54:02 +00:00
|
|
|
import ruamel.yaml.compat
|
2021-10-21 02:02:28 +00:00
|
|
|
|
2021-10-26 10:19:02 +00:00
|
|
|
from ._constants import HEADER_KIWI_CONF_NAME
|
|
|
|
|
2021-10-21 02:02:28 +00:00
|
|
|
|
2021-10-20 12:32:45 +00:00
|
|
|
def user_query(description: str, default: Any, cast_to: Type[Any] = str):
|
|
|
|
# prompt user as per argument
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
str_value = input(f"Enter {description} [{default}] ").strip()
|
|
|
|
if str_value:
|
|
|
|
return cast_to(str_value)
|
|
|
|
else:
|
|
|
|
return default
|
|
|
|
|
|
|
|
except EOFError:
|
|
|
|
click.echo("Input aborted.")
|
|
|
|
return default
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
click.echo(f"Invalid input: {e}")
|
|
|
|
|
|
|
|
|
2021-10-27 11:33:26 +00:00
|
|
|
class YAML(ruamel.yaml.YAML):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.indent(offset=2)
|
|
|
|
|
2021-10-28 11:54:02 +00:00
|
|
|
def dump(self, data, stream=None, **kwargs) -> Optional[str]:
|
|
|
|
into_str: bool = False
|
|
|
|
if stream is None:
|
|
|
|
into_str = True
|
|
|
|
stream = ruamel.yaml.compat.StringIO()
|
2021-10-27 11:33:26 +00:00
|
|
|
|
2021-10-28 11:54:02 +00:00
|
|
|
super().dump(data, stream=stream, **kwargs)
|
|
|
|
if into_str:
|
|
|
|
return stream.getvalue()
|
2021-10-26 10:19:02 +00:00
|
|
|
|
2021-10-28 11:54:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def _format_kiwi_yml(yml_string: str):
|
|
|
|
# insert newline before every main key
|
|
|
|
yml_string = re.sub(r'^(\S)', r'\n\1', yml_string, flags=re.MULTILINE)
|
2021-10-26 10:19:02 +00:00
|
|
|
|
2021-10-28 11:54:02 +00:00
|
|
|
# load header comment from file
|
|
|
|
with open(HEADER_KIWI_CONF_NAME, 'r') as stream:
|
|
|
|
yml_string = stream.read() + yml_string
|
|
|
|
|
|
|
|
return yml_string
|
|
|
|
|
|
|
|
def dump_kiwi_yml(self, data, **kwargs) -> Optional[str]:
|
|
|
|
return self.dump(data, transform=YAML._format_kiwi_yml, **kwargs)
|
2021-10-26 10:19:02 +00:00
|
|
|
|
|
|
|
|
2020-08-18 12:04:10 +00:00
|
|
|
def _surround(string, bang):
|
|
|
|
midlane = f"{bang * 3} {string} {bang * 3}"
|
2021-09-22 01:06:43 +00:00
|
|
|
sidelane = bang * len(midlane)
|
2020-08-18 12:04:10 +00:00
|
|
|
|
|
|
|
return f"{sidelane}\n{midlane}\n{sidelane}"
|
|
|
|
|
|
|
|
|
2020-08-18 13:05:19 +00:00
|
|
|
def _emphasize(lines):
|
|
|
|
if isinstance(lines, list):
|
|
|
|
return '\n'.join([_emphasize(line) for line in lines])
|
|
|
|
elif lines:
|
|
|
|
return f">>> {lines} <<<"
|
|
|
|
else:
|
|
|
|
return lines
|
|
|
|
|
2020-08-19 09:58:13 +00:00
|
|
|
|
2020-08-18 12:04:10 +00:00
|
|
|
def are_you_sure(prompt, default="no"):
|
|
|
|
if default.lower() == 'yes':
|
|
|
|
suffix = "[YES|no]"
|
|
|
|
else:
|
|
|
|
suffix = "[yes|NO]"
|
|
|
|
|
|
|
|
answer = input(
|
2020-08-25 16:01:39 +00:00
|
|
|
f"{_surround('MUST HAVE CAREFULING IN PROCESS', '!')}\n"
|
2020-08-18 12:04:10 +00:00
|
|
|
f"\n"
|
2020-08-18 13:05:19 +00:00
|
|
|
f"{_emphasize(prompt)}\n"
|
2020-08-18 12:04:10 +00:00
|
|
|
f"\n"
|
|
|
|
f"Are you sure you want to proceed? {suffix} "
|
|
|
|
).strip().lower()
|
|
|
|
|
|
|
|
if answer == '':
|
|
|
|
answer = default
|
|
|
|
|
|
|
|
while answer not in ['yes', 'no']:
|
|
|
|
answer = input("Please type 'yes' or 'no' explicitly: ").strip().lower()
|
|
|
|
|
|
|
|
return answer == 'yes'
|