From 93ceea9ff8433eb359f8fff076f5911477c73098 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Sat, 25 Dec 2021 12:48:30 +0100 Subject: [PATCH] Cleanup validation (#2365) --- custom_components/hacs/enums.py | 7 ++++++ .../hacs/helpers/methods/registration.py | 2 +- custom_components/hacs/validate/__init__.py | 19 +++++++++++----- custom_components/hacs/validate/base.py | 22 ++++++++++--------- .../hacs/validate/common/__init__.py | 0 .../hacs/validate/common/hacs_manifest.py | 5 +++-- .../hacs/validate/integration/__init__.py | 0 .../integration/integration_manifest.py | 12 +++++----- tests/conftest.py | 3 ++- .../test_async_run_repository_checks.py | 13 ++++++----- 10 files changed, 51 insertions(+), 32 deletions(-) create mode 100644 custom_components/hacs/validate/common/__init__.py create mode 100644 custom_components/hacs/validate/integration/__init__.py diff --git a/custom_components/hacs/enums.py b/custom_components/hacs/enums.py index 61e0f14c03f..b4f54c8010b 100644 --- a/custom_components/hacs/enums.py +++ b/custom_components/hacs/enums.py @@ -17,6 +17,13 @@ def __str__(self): return str(self.value) +class RepositoryFile(str, Enum): + """Repository file names.""" + + HACS_JSON = "hacs.json" + MAINIFEST_JSON = "manifest.json" + + class ConfigurationType(str, Enum): YAML = "yaml" CONFIG_ENTRY = "config_entry" diff --git a/custom_components/hacs/helpers/methods/registration.py b/custom_components/hacs/helpers/methods/registration.py index 71eed45c1fa..d2d1fe9650f 100644 --- a/custom_components/hacs/helpers/methods/registration.py +++ b/custom_components/hacs/helpers/methods/registration.py @@ -38,4 +38,4 @@ async def async_registration(self, ref=None) -> None: class RepositoryMethodPostRegistration(ABC): async def async_post_registration(self): - await async_run_repository_checks(self) + await async_run_repository_checks(self.hacs, self) diff --git a/custom_components/hacs/validate/__init__.py b/custom_components/hacs/validate/__init__.py index 42ec85ff574..98ad848ab78 100644 --- a/custom_components/hacs/validate/__init__.py +++ b/custom_components/hacs/validate/__init__.py @@ -1,9 +1,18 @@ +from __future__ import annotations import asyncio import glob import importlib from os.path import dirname, join, sep +from typing import TYPE_CHECKING -from custom_components.hacs.share import SHARE, get_hacs +from homeassistant.core import HomeAssistant + + +from ..share import SHARE + +if TYPE_CHECKING: + from ..base import HacsBase + from ..helpers.classes.repository import HacsRepository def _initialize_rules(): @@ -15,15 +24,13 @@ def _initialize_rules(): importlib.import_module(rule) -async def async_initialize_rules(): - hass = get_hacs().hass +async def async_initialize_rules(hass: HomeAssistant) -> None: await hass.async_add_executor_job(_initialize_rules) -async def async_run_repository_checks(repository): - hacs = get_hacs() +async def async_run_repository_checks(hacs: HacsBase, repository: HacsRepository): if not SHARE["rules"]: - await async_initialize_rules() + await async_initialize_rules(hacs.hass) if not hacs.system.running: return checks = [] diff --git a/custom_components/hacs/validate/base.py b/custom_components/hacs/validate/base.py index 481201c8610..3db9c1e04a1 100644 --- a/custom_components/hacs/validate/base.py +++ b/custom_components/hacs/validate/base.py @@ -1,4 +1,10 @@ -from custom_components.hacs.share import SHARE, get_hacs +from __future__ import annotations +from typing import TYPE_CHECKING +from ..share import SHARE, get_hacs + + +if TYPE_CHECKING: + from ..helpers.classes.repository import HacsRepository class ValidationException(Exception): @@ -6,9 +12,11 @@ class ValidationException(Exception): class ValidationBase: - def __init__(self, repository) -> None: - self.repository = repository + action_only = False + + def __init__(self, repository: HacsRepository) -> None: self.hacs = get_hacs() + self.repository = repository self.failed = False self.logger = repository.logger @@ -20,10 +28,6 @@ def __init_subclass__(cls, category="common", **kwargs) -> None: if cls not in SHARE["rules"][category]: SHARE["rules"][category].append(cls) - @property - def action_only(self): - return False - async def _async_run_check(self): """DO NOT OVERRIDE THIS IN SUBCLASSES!""" if self.hacs.system.action: @@ -43,6 +47,4 @@ async def async_check(self): class ActionValidationBase(ValidationBase): - @property - def action_only(self): - return True + action_only = True diff --git a/custom_components/hacs/validate/common/__init__.py b/custom_components/hacs/validate/common/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/custom_components/hacs/validate/common/hacs_manifest.py b/custom_components/hacs/validate/common/hacs_manifest.py index 6701c1215ea..05df09dda77 100644 --- a/custom_components/hacs/validate/common/hacs_manifest.py +++ b/custom_components/hacs/validate/common/hacs_manifest.py @@ -1,3 +1,4 @@ +from custom_components.hacs.enums import RepositoryFile from custom_components.hacs.validate.base import ( ActionValidationBase, ValidationException, @@ -6,5 +7,5 @@ class HacsManifest(ActionValidationBase): def check(self): - if "hacs.json" not in [x.filename for x in self.repository.tree]: - raise ValidationException("The repository has no 'hacs.json' file") + if RepositoryFile.HACS_JSON not in [x.filename for x in self.repository.tree]: + raise ValidationException(f"The repository has no '{RepositoryFile.HACS_JSON}' file") diff --git a/custom_components/hacs/validate/integration/__init__.py b/custom_components/hacs/validate/integration/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/custom_components/hacs/validate/integration/integration_manifest.py b/custom_components/hacs/validate/integration/integration_manifest.py index 41b1c971b38..5252908c867 100644 --- a/custom_components/hacs/validate/integration/integration_manifest.py +++ b/custom_components/hacs/validate/integration/integration_manifest.py @@ -1,10 +1,10 @@ -from custom_components.hacs.validate.base import ( - ActionValidationBase, - ValidationException, -) +from ..base import ActionValidationBase, ValidationException +from ...enums import RepositoryFile class IntegrationManifest(ActionValidationBase, category="integration"): def check(self): - if "manifest.json" not in [x.filename for x in self.repository.tree]: - raise ValidationException("The repository has no 'hacs.json' file") + if RepositoryFile.MAINIFEST_JSON not in [x.filename for x in self.repository.tree]: + raise ValidationException( + f"The repository has no '{RepositoryFile.MAINIFEST_JSON}' file" + ) diff --git a/tests/conftest.py b/tests/conftest.py index cd5efc3b527..5ac8d067d75 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,6 +7,7 @@ from awesomeversion import AwesomeVersion from homeassistant.const import __version__ as HAVERSION +from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceNotFound from homeassistant.helpers.aiohttp_client import async_create_clientsession from homeassistant.loader import Integration @@ -94,7 +95,7 @@ def exc_handle(loop, context): @pytest.fixture -def hacs(hass): +def hacs(hass: HomeAssistant): """Fixture to provide a HACS object.""" hacs_obj = Hacs() hacs_obj.hass = hass diff --git a/tests/validate/test_async_run_repository_checks.py b/tests/validate/test_async_run_repository_checks.py index 0c37d45a41b..4424bcfbd44 100644 --- a/tests/validate/test_async_run_repository_checks.py +++ b/tests/validate/test_async_run_repository_checks.py @@ -1,4 +1,5 @@ import pytest +from custom_components.hacs.base import HacsBase from custom_components.hacs.share import SHARE from custom_components.hacs.validate import ( @@ -8,22 +9,22 @@ @pytest.mark.asyncio -async def test_async_initialize_rules(hacs): +async def test_async_initialize_rules(hacs: HacsBase): - await async_initialize_rules() + await async_initialize_rules(hacs.hass) @pytest.mark.asyncio -async def test_async_run_repository_checks(hacs, repository_integration): - await async_run_repository_checks(repository_integration) +async def test_async_run_repository_checks(hacs: HacsBase, repository_integration): + await async_run_repository_checks(hacs, repository_integration) hacs.system.action = True hacs.system.running = True repository_integration.tree = [] with pytest.raises(SystemExit): - await async_run_repository_checks(repository_integration) + await async_run_repository_checks(hacs, repository_integration) hacs.system.action = False SHARE["rules"] = {} - await async_run_repository_checks(repository_integration) + await async_run_repository_checks(hacs, repository_integration) hacs.system.running = False