Skip to content

Commit 0a5326e

Browse files
committed
Populate mapping of config by purl type post init
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
1 parent b10870a commit 0a5326e

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

aboutcode/federated/__init__.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,8 @@ def config_repo(self) -> "GitRepo":
497497
def from_dict(
498498
cls,
499499
data: dict,
500-
local_root_dir: Path=None,
501-
remote_root_url: str=None,
500+
local_root_dir: Path = None,
501+
remote_root_url: str = None,
502502
) -> "DataFederation":
503503
"""
504504
Return a DataFederation from a configuration mapping.
@@ -532,7 +532,7 @@ def from_dict(
532532
)
533533

534534
@classmethod
535-
def load(cls, name: str, local_root_dir: Path, remote_root_url: str=None) -> "DataFederation":
535+
def load(cls, name: str, local_root_dir: Path, remote_root_url: str = None) -> "DataFederation":
536536
"""
537537
Return an existing DataFederation loaded from ``local_root_dir`` using
538538
the existing configuration file at its conventional location.
@@ -551,7 +551,7 @@ def from_url(
551551
cls,
552552
name: str,
553553
remote_root_url: str,
554-
local_root_dir: Path=None,
554+
local_root_dir: Path = None,
555555
) -> "DataFederation":
556556
"""
557557
Return a DataFederation loaded from a remote configuration file.
@@ -578,8 +578,8 @@ def from_yaml_config(
578578
cls,
579579
name: str,
580580
text: str,
581-
local_root_dir: Path=None,
582-
remote_root_url: str=None,
581+
local_root_dir: Path = None,
582+
remote_root_url: str = None,
583583
) -> "DataFederation":
584584
"""
585585
Return a DataFederation loaded from a YAML configuration text.
@@ -685,6 +685,7 @@ def get_local_datafile(self, data_kind: str, purl: Union[str, PackageURL]) -> "L
685685
@dataclass
686686
class LocalDataFile:
687687
"""A local data file storeed optionally in a GitRepo"""
688+
688689
path: Path
689690
git_repo: "GitRepo" = None
690691

@@ -743,8 +744,17 @@ class DataCluster:
743744
repr=False,
744745
)
745746

747+
# mapping of {purl_type: PurlTypeConfig} for the repos stored in this data
748+
# cluster. This is auto populated and not serialized in the config file.
749+
_configs_by_purl_type: dict[str, "PurlTypeConfig"] = datafield(
750+
default_factory=dict,
751+
init=False,
752+
repr=False,
753+
)
754+
746755
def __post_init__(self):
747756
self.populate_repos()
757+
self.populate_configs()
748758

749759
def populate_repos(self):
750760
"""
@@ -755,6 +765,10 @@ def populate_repos(self):
755765

756766
for ptc in self.purl_type_configs:
757767
drbpt[ptc.purl_type] = [repo for repo in ptc.get_repos(data_kind=kind)]
768+
769+
def populate_configs(self):
770+
for ptc in self.purl_type_configs:
771+
self._configs_by_purl_type[ptc.purl_type] = ptc
758772

759773
@classmethod
760774
def from_dict(cls, data: dict) -> "DataCluster":
@@ -904,7 +918,7 @@ def get_repos(self, data_kind: str) -> Iterable["DataRepository"]:
904918
hashids = self.hashids
905919

906920
for i in range(0, self.number_of_dirs, dirs_per_repo):
907-
hashids_of_repo = hashids[i: i + dirs_per_repo]
921+
hashids_of_repo = hashids[i : i + dirs_per_repo]
908922
yield DataRepository.from_hashids(
909923
data_kind=data_kind,
910924
purl_type=purl_type,
@@ -1223,7 +1237,7 @@ class DataRepository:
12231237
default_factory=list,
12241238
repr=False,
12251239
)
1226-
1240+
12271241
@property
12281242
def name(self):
12291243
return f"{self.data_kind}-{self.purl_type}-{self.start_hashid}"
@@ -1369,7 +1383,7 @@ def build_raw_download_url(
13691383
root_url: str,
13701384
repo: str,
13711385
path: str,
1372-
branch: str="main",
1386+
branch: str = "main",
13731387
builder=None,
13741388
):
13751389
"""
@@ -1391,7 +1405,7 @@ def build_raw_download_url_github(
13911405
root_url: str,
13921406
repo: str,
13931407
path: str,
1394-
branch: str="main",
1408+
branch: str = "main",
13951409
):
13961410
"""
13971411
Return a direct access raw URL to a file in a github repo.
@@ -1405,7 +1419,7 @@ def build_raw_download_url_gitlab(
14051419
root_url: str,
14061420
repo: str,
14071421
path: str,
1408-
branch: str="main",
1422+
branch: str = "main",
14091423
):
14101424
"""
14111425
Return a direct access raw URL to a file in a gitlab repo.
@@ -1418,15 +1432,15 @@ def build_raw_download_url_codeberg(
14181432
root_url: str,
14191433
repo: str,
14201434
path: str,
1421-
branch: str="main",
1435+
branch: str = "main",
14221436
):
14231437
"""
14241438
Return a direct access raw URL to a file in a codeberg repo.
14251439
"""
14261440
return "/".join([root_url, repo, "raw/branch", branch, path])
14271441

14281442

1429-
def compute_purl_hash(purl: Union[PackageURL, str], max_value: int=1024) -> str:
1443+
def compute_purl_hash(purl: Union[PackageURL, str], max_value: int = 1024) -> str:
14301444
"""
14311445
Return a hash string from a ``purl`` string or object.
14321446
@@ -1480,7 +1494,7 @@ def compute_purl_hash(purl: Union[PackageURL, str], max_value: int=1024) -> str:
14801494
return _compute_hash(core_purl=core_purl, max_value=max_value)
14811495

14821496

1483-
def _compute_hash(core_purl: str, max_value: int=1024) -> str:
1497+
def _compute_hash(core_purl: str, max_value: int = 1024) -> str:
14841498
"""
14851499
Return a hash string from a ``core_purl`` string. The core purl string
14861500
must be computed ahead
@@ -1509,7 +1523,7 @@ def _compute_hash(core_purl: str, max_value: int=1024) -> str:
15091523
return f"{short_int:04}"
15101524

15111525

1512-
def is_valid_power_of_two(n: int, max_value: int=1024):
1526+
def is_valid_power_of_two(n: int, max_value: int = 1024):
15131527
"""
15141528
Return True if ``n`` is a power of two between 1 and ``max_value``.
15151529
Use bit manipulations.
@@ -1572,7 +1586,7 @@ def get_core_purl(purl: Union[PackageURL, str]):
15721586

15731587
def package_path_elements(
15741588
purl: Union[PackageURL, str],
1575-
max_value: int=1024,
1589+
max_value: int = 1024,
15761590
):
15771591
"""
15781592
Return a 4-tuple of POSIX path strings from the ``purl`` string or object.

aboutcode/federated/tests/test_federated.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#
88

99
from pathlib import Path
10+
from pickle import FALSE
1011

1112
import pytest
1213
import requests
@@ -27,11 +28,10 @@
2728
from aboutcode.federated import is_valid_power_of_two
2829
from aboutcode.federated import package_path_elements
2930
from aboutcode.federated import percent_quote_more
30-
from pickle import FALSE
3131

3232
TEST_DATA = Path(__file__).parent / "test_data"
3333

34-
REGEN =False
34+
REGEN = False
3535

3636

3737
def test_DataFederation_from_dict_and_to_dict(tmp_path):
@@ -80,7 +80,6 @@ def test_DataFederation_load(tmp_path):
8080

8181

8282
def test_DataFederation_from_url(monkeypatch):
83-
8483
class Response:
8584
ok = True
8685
text = "name: fed\n" "remote_root_url: https://github.com/org\n"
@@ -286,6 +285,6 @@ def test_federation_with_all_cluster_preset():
286285
local_root_dir = TEST_DATA / "all-presets"
287286
if False:
288287
df.local_root_dir = local_root_dir
289-
df.dump()
288+
df.dump()
290289
df2 = DataFederation.load(name="foo", local_root_dir=local_root_dir)
291290
assert df.to_dict() == df2.to_dict()

0 commit comments

Comments
 (0)