Skip to content

Commit

Permalink
consolidate: parse new LABELs
Browse files Browse the repository at this point in the history
- 'maintainer' replaces 'company'
- 'links' for existing + extra link options
- 'type' for broad extension type
- 'tags' for filterable categories
  • Loading branch information
ES-Alexander committed Apr 25, 2023
1 parent 03acec9 commit b6e637c
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions blueos_repository/consolidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import asyncio
import dataclasses
import json
from enum import Enum
from pathlib import Path
from typing import Any, AsyncIterable, Dict, List, Optional, Union

Expand All @@ -12,6 +13,13 @@
REPO_ROOT = "https://raw.githubusercontent.com/bluerobotics/BlueOS-Extensions-Repository/master/"


class StrEnum(str, Enum):
"""Temporary filler until Python 3.11 available."""

def __str__(self) -> str:
return self.value


class EnhancedJSONEncoder(json.JSONEncoder):
"""
Custom json encoder for dataclasses,
Expand All @@ -36,14 +44,23 @@ def from_json(json_dict: Dict[str, str]) -> "Author":


@dataclasses.dataclass
class Company:
class Maintainer:
name: str
about: Optional[str]
email: Optional[str]

@staticmethod
def from_json(json_dict: Dict[str, str]) -> "Company":
return Company(name=json_dict["name"], email=json_dict.get("email", None), about=json_dict.get("about", None))
def from_json(json_dict: Dict[str, str]) -> "Maintainer":
return Maintainer(
name=json_dict["name"], email=json_dict.get("email", None), about=json_dict.get("about", None)
)


class ExtensionType(StrEnum):
DEVICE_INTEGRATION = "device-integration"
EXAMPLE = "example"
THEME = "theme"
OTHER = "other"


# pylint: disable=too-many-instance-attributes
Expand All @@ -56,8 +73,16 @@ class Version:
authors: List[Author]
docs: Optional[str]
readme: Optional[str]
company: Optional[Company]
maintainer: Optional[Maintainer]
support: Optional[str]
type: ExtensionType
filter_tags: List[str]
extra_links: Dict[str, str]

@staticmethod
def validate_filter_tags(tags: List[str]) -> List[str]:
"""Returns a list of up to 10 lower-case alpha-numeric tags (dashes allowed)."""
return [tag.lower() for tag in tags if tag.replace("-", "").isalnum()][:10]


@dataclasses.dataclass
Expand Down Expand Up @@ -137,6 +162,7 @@ def is_valid_semver(string: str) -> bool:
except ValueError:
return False

# pylint: disable=too-many-locals
async def run(self) -> None:
async for repository in self.all_repositories():
for tag in await self.registry.fetch_remote_tags(repository.docker):
Expand All @@ -146,24 +172,33 @@ async def run(self) -> None:
continue
raw_labels = await self.registry.fetch_labels(f"{repository.docker}:{tag}")
permissions = raw_labels.get("permissions", None)
website = raw_labels.get("website", None)
links = raw_labels.get("links", {})
website = links.pop("website", raw_labels.get("website", None))
authors = raw_labels.get("authors", None)
docs = raw_labels.get("docs", None)
docs = links.pop("docs", links.pop("documentation", raw_labels.get("docs", None)))
readme = raw_labels.get("readme", None)
if readme is not None:
readme = readme.replace(r"{tag}", tag)
company_raw = raw_labels.get("company", None)
company = Company.from_json(json.loads(company_raw)) if company_raw is not None else None
support = raw_labels.get("support", None)
# support new "maintainer" and old "company" name for label
maintainer_raw = raw_labels.get("maintainer", raw_labels.get("company", None))
maintainer = (
Maintainer.from_json(json.loads(maintainer_raw)) if maintainer_raw is not None else None
)
support = links.pop("support", raw_labels.get("support", None))
type_ = raw_labels.get("type", ExtensionType.OTHER)
filter_tags = raw_labels.get("tags", [])

new_version = Version(
permissions=json.loads(permissions) if permissions else None,
website=website,
authors=json.loads(authors) if authors else [],
docs=json.loads(docs) if docs else None,
readme=await self.fetch_readme(readme) if readme is not None else None,
company=company,
maintainer=maintainer,
support=support,
extra_links=links,
type=type_,
filter_tags=Version.validate_filter_tags(filter_tags),
requirements=raw_labels.get("requirements", None),
tag=tag,
)
Expand Down

0 comments on commit b6e637c

Please sign in to comment.