base.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """Base class for validation."""
  2. from __future__ import annotations
  3. from typing import TYPE_CHECKING
  4. from ..enums import HacsCategory
  5. from ..exceptions import HacsException
  6. if TYPE_CHECKING:
  7. from ..repositories.base import HacsRepository
  8. class ValidationException(HacsException):
  9. """Raise when there is a validation issue."""
  10. class ActionValidationBase:
  11. """Base class for action validation."""
  12. categories: list[HacsCategory] = []
  13. allow_fork: bool = True
  14. more_info: str = "https://hacs.xyz/docs/publish/action"
  15. def __init__(self, repository: HacsRepository) -> None:
  16. self.hacs = repository.hacs
  17. self.repository = repository
  18. self.failed = False
  19. @property
  20. def slug(self) -> str:
  21. """Return the check slug."""
  22. return self.__class__.__module__.rsplit(".", maxsplit=1)[-1]
  23. async def async_validate(self) -> None:
  24. """Validate the repository."""
  25. async def execute_validation(self, *_, **__) -> None:
  26. """Execute the task defined in subclass."""
  27. self.failed = False
  28. try:
  29. await self.async_validate()
  30. except ValidationException as exception:
  31. self.failed = True
  32. self.hacs.log.error(
  33. "<Validation %s> failed: %s (More info: %s )",
  34. self.slug,
  35. exception,
  36. self.more_info,
  37. )
  38. else:
  39. self.hacs.log.info("<Validation %s> completed", self.slug)