2019-04-13 20:17:01 +00:00
|
|
|
"""Models for manifest validator."""
|
2019-12-09 15:24:03 +00:00
|
|
|
import importlib
|
2019-04-13 20:17:01 +00:00
|
|
|
import json
|
|
|
|
import pathlib
|
2019-12-09 15:24:03 +00:00
|
|
|
from typing import Any, Dict, List
|
2019-04-13 20:17:01 +00:00
|
|
|
|
|
|
|
import attr
|
|
|
|
|
|
|
|
|
|
|
|
@attr.s
|
|
|
|
class Error:
|
|
|
|
"""Error validating an integration."""
|
|
|
|
|
|
|
|
plugin = attr.ib(type=str)
|
|
|
|
error = attr.ib(type=str)
|
|
|
|
fixable = attr.ib(type=bool, default=False)
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
"""Represent error as string."""
|
2020-04-05 10:49:57 +00:00
|
|
|
return f"[{self.plugin.upper()}] {self.error}"
|
2019-04-13 20:17:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
@attr.s
|
|
|
|
class Config:
|
|
|
|
"""Config for the run."""
|
|
|
|
|
|
|
|
root = attr.ib(type=pathlib.Path)
|
|
|
|
action = attr.ib(type=str)
|
|
|
|
errors = attr.ib(type=List[Error], factory=list)
|
|
|
|
cache = attr.ib(type=Dict[str, Any], factory=dict)
|
|
|
|
|
|
|
|
def add_error(self, *args, **kwargs):
|
|
|
|
"""Add an error."""
|
|
|
|
self.errors.append(Error(*args, **kwargs))
|
|
|
|
|
|
|
|
|
|
|
|
@attr.s
|
|
|
|
class Integration:
|
|
|
|
"""Represent an integration in our validator."""
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def load_dir(cls, path: pathlib.Path):
|
|
|
|
"""Load all integrations in a directory."""
|
|
|
|
assert path.is_dir()
|
|
|
|
integrations = {}
|
|
|
|
for fil in path.iterdir():
|
2019-07-31 19:25:30 +00:00
|
|
|
if fil.is_file() or fil.name == "__pycache__":
|
2019-04-13 20:17:01 +00:00
|
|
|
continue
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
init = fil / "__init__.py"
|
2019-04-29 08:53:27 +00:00
|
|
|
if not init.exists():
|
2019-07-31 19:25:30 +00:00
|
|
|
print(
|
2020-01-03 13:47:06 +00:00
|
|
|
f"Warning: {init} missing, skipping directory. "
|
2019-07-31 19:25:30 +00:00
|
|
|
"If this is your development environment, "
|
2020-01-03 13:47:06 +00:00
|
|
|
"you can safely delete this folder."
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-04-29 08:53:27 +00:00
|
|
|
continue
|
|
|
|
|
2019-04-13 20:17:01 +00:00
|
|
|
integration = cls(fil)
|
|
|
|
integration.load_manifest()
|
|
|
|
integrations[integration.domain] = integration
|
|
|
|
|
|
|
|
return integrations
|
|
|
|
|
|
|
|
path = attr.ib(type=pathlib.Path)
|
|
|
|
manifest = attr.ib(type=dict, default=None)
|
|
|
|
errors = attr.ib(type=List[Error], factory=list)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def domain(self) -> str:
|
|
|
|
"""Integration domain."""
|
|
|
|
return self.path.name
|
|
|
|
|
2020-04-03 19:58:19 +00:00
|
|
|
@property
|
|
|
|
def requirements(self) -> List[str]:
|
|
|
|
"""List of requirements."""
|
|
|
|
return self.manifest.get("requirements", [])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def dependencies(self) -> List[str]:
|
|
|
|
"""List of dependencies."""
|
|
|
|
return self.manifest.get("dependencies", [])
|
|
|
|
|
2019-04-13 20:17:01 +00:00
|
|
|
def add_error(self, *args, **kwargs):
|
|
|
|
"""Add an error."""
|
|
|
|
self.errors.append(Error(*args, **kwargs))
|
|
|
|
|
|
|
|
def load_manifest(self) -> None:
|
|
|
|
"""Load manifest."""
|
2019-07-31 19:25:30 +00:00
|
|
|
manifest_path = self.path / "manifest.json"
|
2019-04-18 20:40:46 +00:00
|
|
|
if not manifest_path.is_file():
|
2019-08-23 16:53:33 +00:00
|
|
|
self.add_error("model", f"Manifest file {manifest_path} not found")
|
2019-04-13 20:17:01 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2019-04-18 20:40:46 +00:00
|
|
|
manifest = json.loads(manifest_path.read_text())
|
2019-04-13 20:17:01 +00:00
|
|
|
except ValueError as err:
|
2019-08-23 16:53:33 +00:00
|
|
|
self.add_error("model", f"Manifest contains invalid JSON: {err}")
|
2019-04-13 20:17:01 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
self.manifest = manifest
|
2019-05-13 08:16:55 +00:00
|
|
|
|
|
|
|
def import_pkg(self, platform=None):
|
|
|
|
"""Import the Python file."""
|
2019-08-23 16:53:33 +00:00
|
|
|
pkg = f"homeassistant.components.{self.domain}"
|
2019-05-13 08:16:55 +00:00
|
|
|
if platform is not None:
|
2019-08-23 16:53:33 +00:00
|
|
|
pkg += f".{platform}"
|
2019-05-13 08:16:55 +00:00
|
|
|
return importlib.import_module(pkg)
|