Coverage for harbor_cli/harbor/artifact.py: 26%

25 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-09 12:09 +0100

1from __future__ import annotations 

2 

3from typing import NamedTuple 

4 

5from ..exceptions import ArtifactNameFormatError 

6 

7 

8class ArtifactName(NamedTuple): 

9 domain: str | None 

10 project: str 

11 repository: str 

12 reference: str 

13 

14 

15def parse_artifact_name(s: str) -> ArtifactName: 

16 """Splits an artifact string into domain name (optional), project, 

17 repo, and reference (tag or digest). 

18 

19 Raises ValueError if the string is not in the correct format. 

20 

21 Parameters 

22 ---------- 

23 s : str 

24 Artifact string in the form of [domain/]<project>/<repo>{@sha256:<digest>,:<tag>} 

25 

26 Returns 

27 ------- 

28 ArtifactName 

29 Named tuple of domain name (optional), project, repo, and reference (tag or digest). 

30 """ 

31 parts = s.split("/") 

32 if len(parts) == 3: 

33 domain, project, rest = parts 

34 elif len(parts) == 2: 

35 project, rest = parts 

36 domain = None 

37 else: 

38 raise ArtifactNameFormatError(s) 

39 

40 # TODO: make this more robust 

41 if "@" in rest: 

42 repo, tag_or_digest = rest.split("@") 

43 elif ":" in rest: 

44 parts = rest.split(":") 

45 if len(parts) != 2: 

46 raise ArtifactNameFormatError(s) 

47 repo, tag_or_digest = parts 

48 else: 

49 raise ArtifactNameFormatError(s) 

50 

51 return ArtifactName(domain, project, repo, tag_or_digest)