hass-core/script/scaffold/gather_info.py
Paulus Schoutsen 8502f7c7d4
Add integration scaffolding script (#26777)
* Add integration scaffolding script

* Make easier to develop

* Update py.test -> pytest
2019-09-20 17:02:18 -07:00

79 lines
2.1 KiB
Python

"""Gather info for scaffolding."""
from homeassistant.util import slugify
from .const import COMPONENT_DIR
from .model import Info
from .error import ExitApp
CHECK_EMPTY = ["Cannot be empty", lambda value: value]
FIELDS = {
"domain": {
"prompt": "What is the domain?",
"validators": [
CHECK_EMPTY,
[
"Domains cannot contain spaces or special characters.",
lambda value: value == slugify(value),
],
[
"There already is an integration with this domain.",
lambda value: not (COMPONENT_DIR / value).exists(),
],
],
},
"name": {
"prompt": "What is the name of your integration?",
"validators": [CHECK_EMPTY],
},
"codeowner": {
"prompt": "What is your GitHub handle?",
"validators": [
CHECK_EMPTY,
[
'GitHub handles need to start with an "@"',
lambda value: value.startswith("@"),
],
],
},
"requirement": {
"prompt": "What PyPI package and version do you depend on? Leave blank for none.",
"validators": [
["Versions should be pinned using '=='.", lambda value: "==" in value]
],
},
}
def gather_info() -> Info:
"""Gather info from user."""
answers = {}
for key, info in FIELDS.items():
hint = None
while key not in answers:
if hint is not None:
print()
print(f"Error: {hint}")
try:
print()
value = input(info["prompt"] + "\n> ")
except (KeyboardInterrupt, EOFError):
raise ExitApp("Interrupted!", 1)
value = value.strip()
hint = None
for validator_hint, validator in info["validators"]:
if not validator(value):
hint = validator_hint
break
if hint is None:
answers[key] = value
print()
return Info(**answers)