custom_components/bahmcloud_store/core.py aktualisiert
This commit is contained in:
@@ -212,13 +212,32 @@ class BCSCore:
|
|||||||
await self.refresh()
|
await self.refresh()
|
||||||
self.signal_updated()
|
self.signal_updated()
|
||||||
|
|
||||||
def _normalize_repo_name(self, name: str | None) -> str | None:
|
async def list_custom_repos(self) -> list[CustomRepo]:
|
||||||
if not name:
|
return await self.storage.list_custom_repos()
|
||||||
return None
|
|
||||||
n = name.strip()
|
def list_repos_public(self) -> list[dict[str, Any]]:
|
||||||
if n.endswith(".git"):
|
out: list[dict[str, Any]] = []
|
||||||
n = n[:-4]
|
for r in self.repos.values():
|
||||||
return n or None
|
out.append(
|
||||||
|
{
|
||||||
|
"id": r.id,
|
||||||
|
"name": r.name,
|
||||||
|
"url": r.url,
|
||||||
|
"source": r.source,
|
||||||
|
"owner": r.owner,
|
||||||
|
"provider": r.provider,
|
||||||
|
"repo_name": r.provider_repo_name,
|
||||||
|
"description": r.provider_description or r.meta_description,
|
||||||
|
"default_branch": r.default_branch,
|
||||||
|
"latest_version": r.latest_version,
|
||||||
|
"latest_version_source": r.latest_version_source,
|
||||||
|
"category": r.meta_category,
|
||||||
|
"meta_author": r.meta_author,
|
||||||
|
"meta_maintainer": r.meta_maintainer,
|
||||||
|
"meta_source": r.meta_source,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return out
|
||||||
|
|
||||||
def _split_owner_repo(self, repo_url: str) -> tuple[str | None, str | None]:
|
def _split_owner_repo(self, repo_url: str) -> tuple[str | None, str | None]:
|
||||||
u = urlparse(repo_url.rstrip("/"))
|
u = urlparse(repo_url.rstrip("/"))
|
||||||
@@ -226,8 +245,11 @@ class BCSCore:
|
|||||||
if len(parts) < 2:
|
if len(parts) < 2:
|
||||||
return None, None
|
return None, None
|
||||||
owner = parts[0].strip() or None
|
owner = parts[0].strip() or None
|
||||||
repo = self._normalize_repo_name(parts[1])
|
name = parts[1].strip()
|
||||||
return owner, repo
|
if name.endswith(".git"):
|
||||||
|
name = name[:-4]
|
||||||
|
name = name.strip() or None
|
||||||
|
return owner, name
|
||||||
|
|
||||||
def _is_github(self, repo_url: str) -> bool:
|
def _is_github(self, repo_url: str) -> bool:
|
||||||
return "github.com" in urlparse(repo_url).netloc.lower()
|
return "github.com" in urlparse(repo_url).netloc.lower()
|
||||||
@@ -236,12 +258,6 @@ class BCSCore:
|
|||||||
host = urlparse(repo_url).netloc.lower()
|
host = urlparse(repo_url).netloc.lower()
|
||||||
return host and "github.com" not in host and "gitlab.com" not in host
|
return host and "github.com" not in host and "gitlab.com" not in host
|
||||||
|
|
||||||
def _is_gitlab(self, repo_url: str, provider: str | None = None) -> bool:
|
|
||||||
host = urlparse(repo_url).netloc.lower()
|
|
||||||
if provider and provider.strip().lower() == "gitlab":
|
|
||||||
return True
|
|
||||||
return "gitlab" in host
|
|
||||||
|
|
||||||
async def _fetch_text(self, url: str) -> str | None:
|
async def _fetch_text(self, url: str) -> str | None:
|
||||||
session = async_get_clientsession(self.hass)
|
session = async_get_clientsession(self.hass)
|
||||||
try:
|
try:
|
||||||
@@ -279,6 +295,13 @@ class BCSCore:
|
|||||||
# Filename fallbacks
|
# Filename fallbacks
|
||||||
filenames = ["README.md", "readme.md", "README.MD", "README.rst", "README"]
|
filenames = ["README.md", "readme.md", "README.MD", "README.rst", "README"]
|
||||||
|
|
||||||
|
provider = (repo.provider or "").strip().lower()
|
||||||
|
if not provider:
|
||||||
|
provider = detect_provider(repo_url) or ""
|
||||||
|
|
||||||
|
u = urlparse(repo_url.rstrip("/"))
|
||||||
|
host = (u.netloc or "").lower()
|
||||||
|
|
||||||
candidates: list[str] = []
|
candidates: list[str] = []
|
||||||
|
|
||||||
if self._is_github(repo_url):
|
if self._is_github(repo_url):
|
||||||
@@ -289,21 +312,21 @@ class BCSCore:
|
|||||||
base = f"https://raw.githubusercontent.com/{owner}/{name}/{branch}"
|
base = f"https://raw.githubusercontent.com/{owner}/{name}/{branch}"
|
||||||
candidates.extend([f"{base}/{fn}" for fn in filenames])
|
candidates.extend([f"{base}/{fn}" for fn in filenames])
|
||||||
|
|
||||||
elif self._is_gitlab(repo_url, repo.provider):
|
elif provider == "gitlab" or "gitlab" in host:
|
||||||
# GitLab can have nested groups: /group/subgroup/repo
|
# GitLab can have nested groups: /group/subgroup/repo
|
||||||
u = urlparse(repo_url.rstrip("/"))
|
|
||||||
parts = [p for p in u.path.strip("/").split("/") if p]
|
parts = [p for p in u.path.strip("/").split("/") if p]
|
||||||
if len(parts) < 2:
|
if len(parts) < 2:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
repo_name = self._normalize_repo_name(parts[-1])
|
repo_name = parts[-1].strip()
|
||||||
|
if repo_name.endswith(".git"):
|
||||||
|
repo_name = repo_name[:-4]
|
||||||
group_path = "/".join(parts[:-1]).strip("/")
|
group_path = "/".join(parts[:-1]).strip("/")
|
||||||
if not group_path or not repo_name:
|
if not group_path or not repo_name:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
root = f"{u.scheme}://{u.netloc}/{group_path}/{repo_name}"
|
root = f"{u.scheme}://{u.netloc}/{group_path}/{repo_name}"
|
||||||
for branch in branch_candidates:
|
for branch in branch_candidates:
|
||||||
# Most common GitLab raw form
|
|
||||||
bases = [
|
bases = [
|
||||||
f"{root}/-/raw/{branch}",
|
f"{root}/-/raw/{branch}",
|
||||||
# Some instances may expose /raw/<branch> as well
|
# Some instances may expose /raw/<branch> as well
|
||||||
@@ -317,11 +340,9 @@ class BCSCore:
|
|||||||
if not owner or not name:
|
if not owner or not name:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
u = urlparse(repo_url.rstrip("/"))
|
|
||||||
root = f"{u.scheme}://{u.netloc}/{owner}/{name}"
|
root = f"{u.scheme}://{u.netloc}/{owner}/{name}"
|
||||||
|
|
||||||
for branch in branch_candidates:
|
for branch in branch_candidates:
|
||||||
# gitea raw endpoints (both common forms)
|
|
||||||
bases = [
|
bases = [
|
||||||
f"{root}/raw/branch/{branch}",
|
f"{root}/raw/branch/{branch}",
|
||||||
f"{root}/raw/{branch}",
|
f"{root}/raw/{branch}",
|
||||||
|
|||||||
Reference in New Issue
Block a user