custom_components/bahmcloud_store/metadata.py aktualisiert
This commit is contained in:
@@ -22,36 +22,28 @@ class RepoMetadata:
|
|||||||
maintainer: str | None = None
|
maintainer: str | None = None
|
||||||
|
|
||||||
|
|
||||||
def _normalize_repo_name(name: str | None) -> str | None:
|
|
||||||
if not name:
|
|
||||||
return None
|
|
||||||
n = name.strip()
|
|
||||||
if n.endswith(".git"):
|
|
||||||
n = n[:-4]
|
|
||||||
return n or None
|
|
||||||
|
|
||||||
|
|
||||||
def _split_owner_repo(repo_url: str) -> tuple[str | None, str | None]:
|
def _split_owner_repo(repo_url: str) -> tuple[str | None, str | None]:
|
||||||
u = urlparse(repo_url.rstrip("/"))
|
u = urlparse(repo_url.rstrip("/"))
|
||||||
parts = [p for p in u.path.strip("/").split("/") if p]
|
parts = [p for p in u.path.split("/") if p]
|
||||||
if len(parts) < 2:
|
if len(parts) < 2:
|
||||||
return None, None
|
return None, None
|
||||||
owner = parts[0].strip() or None
|
owner = parts[0]
|
||||||
repo = _normalize_repo_name(parts[1])
|
repo = parts[1]
|
||||||
|
repo = repo[:-4] if repo.endswith(".git") else repo
|
||||||
return owner, repo
|
return owner, repo
|
||||||
|
|
||||||
|
|
||||||
def _is_github(repo_url: str) -> bool:
|
def _is_github(url: str) -> bool:
|
||||||
return "github.com" in urlparse(repo_url).netloc.lower()
|
return "github.com" in (urlparse(url).netloc or "").lower()
|
||||||
|
|
||||||
|
|
||||||
def _is_gitlab(repo_url: str) -> bool:
|
def _is_gitlab(url: str) -> bool:
|
||||||
return "gitlab" in urlparse(repo_url).netloc.lower()
|
return "gitlab.com" in (urlparse(url).netloc or "").lower()
|
||||||
|
|
||||||
|
|
||||||
def _is_gitea(repo_url: str) -> bool:
|
def _is_gitea(url: str) -> bool:
|
||||||
host = urlparse(repo_url).netloc.lower()
|
host = (urlparse(url).netloc or "").lower()
|
||||||
return host and ("github.com" not in host) and ("gitlab" not in host)
|
return bool(host) and not _is_github(url) and not _is_gitlab(url)
|
||||||
|
|
||||||
|
|
||||||
async def _fetch_text(hass: HomeAssistant, url: str) -> str | None:
|
async def _fetch_text(hass: HomeAssistant, url: str) -> str | None:
|
||||||
@@ -67,10 +59,9 @@ async def _fetch_text(hass: HomeAssistant, url: str) -> str | None:
|
|||||||
|
|
||||||
def _parse_meta_yaml(raw: str, source: str) -> RepoMetadata:
|
def _parse_meta_yaml(raw: str, source: str) -> RepoMetadata:
|
||||||
try:
|
try:
|
||||||
data = ha_yaml.parse_yaml(raw)
|
data = ha_yaml.load(raw) or {}
|
||||||
if not isinstance(data, dict):
|
if not isinstance(data, dict):
|
||||||
return RepoMetadata(source=source)
|
return RepoMetadata(source=source)
|
||||||
|
|
||||||
return RepoMetadata(
|
return RepoMetadata(
|
||||||
source=source,
|
source=source,
|
||||||
name=data.get("name"),
|
name=data.get("name"),
|
||||||
@@ -88,20 +79,14 @@ def _parse_meta_hacs_json(raw: str) -> RepoMetadata:
|
|||||||
data = json.loads(raw)
|
data = json.loads(raw)
|
||||||
if not isinstance(data, dict):
|
if not isinstance(data, dict):
|
||||||
return RepoMetadata(source="hacs.json")
|
return RepoMetadata(source="hacs.json")
|
||||||
|
# common keys
|
||||||
name = data.get("name")
|
|
||||||
description = data.get("description")
|
|
||||||
author = data.get("author")
|
|
||||||
maintainer = data.get("maintainer")
|
|
||||||
category = data.get("category") or data.get("type")
|
|
||||||
|
|
||||||
return RepoMetadata(
|
return RepoMetadata(
|
||||||
source="hacs.json",
|
source="hacs.json",
|
||||||
name=name if isinstance(name, str) else None,
|
name=data.get("name"),
|
||||||
description=description if isinstance(description, str) else None,
|
description=data.get("description"),
|
||||||
category=category if isinstance(category, str) else None,
|
category=data.get("category"),
|
||||||
author=author if isinstance(author, str) else None,
|
author=data.get("author"),
|
||||||
maintainer=maintainer if isinstance(maintainer, str) else None,
|
maintainer=data.get("maintainer"),
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
return RepoMetadata(source="hacs.json")
|
return RepoMetadata(source="hacs.json")
|
||||||
@@ -113,13 +98,7 @@ async def fetch_repo_metadata(hass: HomeAssistant, repo_url: str, default_branch
|
|||||||
return RepoMetadata()
|
return RepoMetadata()
|
||||||
|
|
||||||
branch = default_branch or "main"
|
branch = default_branch or "main"
|
||||||
|
|
||||||
# Priority:
|
|
||||||
# 1) bcs.yaml
|
|
||||||
# 2) hacs.yaml
|
|
||||||
# 3) hacs.json
|
|
||||||
filenames = ["bcs.yaml", "hacs.yaml", "hacs.json"]
|
filenames = ["bcs.yaml", "hacs.yaml", "hacs.json"]
|
||||||
|
|
||||||
candidates: list[tuple[str, str]] = []
|
candidates: list[tuple[str, str]] = []
|
||||||
|
|
||||||
if _is_github(repo_url):
|
if _is_github(repo_url):
|
||||||
@@ -130,23 +109,16 @@ async def fetch_repo_metadata(hass: HomeAssistant, repo_url: str, default_branch
|
|||||||
elif _is_gitlab(repo_url):
|
elif _is_gitlab(repo_url):
|
||||||
u = urlparse(repo_url.rstrip("/"))
|
u = urlparse(repo_url.rstrip("/"))
|
||||||
root = f"{u.scheme}://{u.netloc}/{owner}/{repo}"
|
root = f"{u.scheme}://{u.netloc}/{owner}/{repo}"
|
||||||
# GitLab raw format
|
|
||||||
# https://gitlab.com/<owner>/<repo>/-/raw/<branch>/<file>
|
|
||||||
for fn in filenames:
|
for fn in filenames:
|
||||||
candidates.append((fn, f"{root}/-/raw/{branch}/{fn}"))
|
candidates.append((fn, f"{root}/-/raw/{branch}/{fn}"))
|
||||||
|
|
||||||
elif _is_gitea(repo_url):
|
elif _is_gitea(repo_url):
|
||||||
u = urlparse(repo_url.rstrip("/"))
|
u = urlparse(repo_url.rstrip("/"))
|
||||||
root = f"{u.scheme}://{u.netloc}/{owner}/{repo}"
|
root = f"{u.scheme}://{u.netloc}/{owner}/{repo}"
|
||||||
|
bases = [f"{root}/raw/branch/{branch}", f"{root}/raw/{branch}"]
|
||||||
bases = [
|
|
||||||
f"{root}/raw/branch/{branch}",
|
|
||||||
f"{root}/raw/{branch}",
|
|
||||||
]
|
|
||||||
for fn in filenames:
|
for fn in filenames:
|
||||||
for b in bases:
|
for b in bases:
|
||||||
candidates.append((fn, f"{b}/{fn}"))
|
candidates.append((fn, f"{b}/{fn}"))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return RepoMetadata()
|
return RepoMetadata()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user