|
12 | 12 | import tarfile
|
13 | 13 | import tempfile
|
14 | 14 | import typing as t
|
| 15 | +from collections import ChainMap, defaultdict |
15 | 16 | from dataclasses import dataclass, field
|
16 | 17 | from io import BytesIO
|
17 | 18 | from pathlib import Path
|
@@ -61,6 +62,7 @@ class Repo:
|
61 | 62 | branch: str = "main"
|
62 | 63 | branch_in_use: t.Optional[str] = None
|
63 | 64 | local_basepath: t.Optional[Path] = None
|
| 65 | + subpackages: t.Optional[t.List] = None |
64 | 66 | status: RepoStatus = field(default_factory=lambda: RepoStatus())
|
65 | 67 | type: t.Optional[str] = None
|
66 | 68 |
|
@@ -190,6 +192,15 @@ def download_from_gh_latest(dest_dir: Path, owner: str, name: str):
|
190 | 192 | return latest_release_tar_url
|
191 | 193 |
|
192 | 194 |
|
| 195 | +@dataclass |
| 196 | +class SubPackage: |
| 197 | + """A package that lives under another Repo.""" |
| 198 | + |
| 199 | + name: str |
| 200 | + title: str |
| 201 | + type: t.Optional[str] = None |
| 202 | + |
| 203 | + |
193 | 204 | @dataclass
|
194 | 205 | class Repos:
|
195 | 206 | """A collection of Repos"""
|
@@ -242,14 +253,34 @@ def from_yaml(cls, path: str):
|
242 | 253 | raise ValueError("File does not exist:", file)
|
243 | 254 | log.info(f"repofile={str(file.absolute())}")
|
244 | 255 |
|
| 256 | + # Create Repo objects from yaml data |
| 257 | + repos: t.Dict[str, t.List] = {} |
| 258 | + nested_packages: t.Dict[str, t.List[SubPackage]] = {} |
245 | 259 | with open(file, "r") as f:
|
246 | 260 | data = yaml.load(f, Loader=yaml.SafeLoader)
|
247 |
| - repos = data["repos"] |
248 |
| - core_repo = Repo(**repos["core"][0], type="core") |
249 |
| - content_repos = [Repo(**repo, type="content") for repo in repos["content"]] |
250 |
| - other_repos = [Repo(**repo, type="other") for repo in repos["other"]] |
| 261 | + for repo_type in ("core", "content", "other"): |
| 262 | + repos[repo_type] = [] |
| 263 | + for repo in data["repos"][repo_type]: |
| 264 | + # Collect nested packages |
| 265 | + if parent_package := repo.pop("subpackage_of", None): |
| 266 | + nested_packages.setdefault(parent_package, []).append( |
| 267 | + SubPackage(**repo, type=repo_type) |
| 268 | + ) |
| 269 | + continue |
| 270 | + # Create regular packages |
| 271 | + repos[repo_type].append(Repo(**repo, type=repo_type)) |
| 272 | + |
| 273 | + # Update Repo objects that contain subpackages |
| 274 | + for parent_repo_name, subpackages_list in nested_packages.items(): |
| 275 | + flat_repos = repos["core"] + repos["content"] + repos["other"] |
| 276 | + for repo in flat_repos: |
| 277 | + if repo.name == parent_repo_name: |
| 278 | + repo.subpackages = subpackages_list |
| 279 | + |
251 | 280 | return Repos(
|
252 |
| - core_repo=core_repo, content_repos=content_repos, other_repos=other_repos |
| 281 | + core_repo=repos["core"][0], |
| 282 | + content_repos=repos["content"], |
| 283 | + other_repos=repos["other"], |
253 | 284 | )
|
254 | 285 |
|
255 | 286 | @classmethod
|
|
0 commit comments