Skip to content

Commit

Permalink
Add plugin option [hassfest] (home-assistant#65024)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Jan 27, 2022
1 parent 2325cac commit f6c6796
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Automatically generated by hassfest.
#
# To update, run python3 -m script.hassfest
# To update, run python3 -m script.hassfest -p mypy_config

[mypy]
python_version = 3.9
Expand Down
32 changes: 31 additions & 1 deletion script/hassfest/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
mypy_config,
]

ALL_PLUGIN_NAMES = [
plugin.__name__.rsplit(".", maxsplit=1)[-1]
for plugin in (*INTEGRATION_PLUGINS, *HASS_PLUGINS)
]


def valid_integration_path(integration_path):
"""Test if it's a valid integration."""
Expand All @@ -53,6 +58,17 @@ def valid_integration_path(integration_path):
return path


def validate_plugins(plugin_names: str) -> list[str]:
"""Split and validate plugin names."""
all_plugin_names = set(ALL_PLUGIN_NAMES)
plugins = plugin_names.split(",")
for plugin in plugins:
if plugin not in all_plugin_names:
raise argparse.ArgumentTypeError(f"{plugin} is not a valid plugin name")

return plugins


def get_config() -> Config:
"""Return config."""
parser = argparse.ArgumentParser(description="Hassfest")
Expand All @@ -70,6 +86,13 @@ def get_config() -> Config:
action="store_true",
help="Validate requirements",
)
parser.add_argument(
"-p",
"--plugins",
type=validate_plugins,
default=ALL_PLUGIN_NAMES,
help="Comma-separate list of plugins to run. Valid plugin names: %(default)s",
)
parsed = parser.parse_args()

if parsed.action is None:
Expand All @@ -91,6 +114,7 @@ def get_config() -> Config:
specific_integrations=parsed.integration_path,
action=parsed.action,
requirements=parsed.requirements,
plugins=set(parsed.plugins),
)


Expand All @@ -117,9 +141,12 @@ def main():
plugins += HASS_PLUGINS

for plugin in plugins:
plugin_name = plugin.__name__.rsplit(".", maxsplit=1)[-1]
if plugin_name not in config.plugins:
continue
try:
start = monotonic()
print(f"Validating {plugin.__name__.split('.')[-1]}...", end="", flush=True)
print(f"Validating {plugin_name}...", end="", flush=True)
if (
plugin is requirements
and config.requirements
Expand Down Expand Up @@ -161,6 +188,9 @@ def main():

if config.action == "generate":
for plugin in plugins:
plugin_name = plugin.__name__.rsplit(".", maxsplit=1)[-1]
if plugin_name not in config.plugins:
continue
if hasattr(plugin, "generate"):
plugin.generate(integrations, config)
return 0
Expand Down
1 change: 1 addition & 0 deletions script/hassfest/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Config:
requirements: bool = attr.ib()
errors: list[Error] = attr.ib(factory=list)
cache: dict[str, Any] = attr.ib(factory=dict)
plugins: set[str] = attr.ib(factory=set)

def add_error(self, *args: Any, **kwargs: Any) -> None:
"""Add an error."""
Expand Down
2 changes: 1 addition & 1 deletion script/hassfest/mypy_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
HEADER: Final = """
# Automatically generated by hassfest.
#
# To update, run python3 -m script.hassfest
# To update, run python3 -m script.hassfest -p mypy_config
""".lstrip()

Expand Down

0 comments on commit f6c6796

Please sign in to comment.