Coverage for src/gitlabracadabra/packages/raw.py: 80%

1#

2# Copyright (C) 2019-2025 Mathieu Parent <math.parent@gmail.com>

3#

4# This program is free software: you can redistribute it and/or modify

5# it under the terms of the GNU Lesser General Public License as published by

6# the Free Software Foundation, either version 3 of the License, or

7# (at your option) any later version.

8#

9# This program is distributed in the hope that it will be useful,

10# but WITHOUT ANY WARRANTY; without even the implied warranty of

11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

12# GNU Lesser General Public License for more details.

13#

14# You should have received a copy of the GNU Lesser General Public License

15# along with this program. If not, see <http://www.gnu.org/licenses/>.

16

17 from __future__ import annotations

18

19 from typing import TYPE_CHECKING

20

21 from gitlabracadabra.packages.package_file import PackageFile

22 from gitlabracadabra.packages.source import Source

23

24 if TYPE_CHECKING: 24 ↛ 25line 24 didn't jump to line 25 because the condition on line 24 was never true

25 from typing import TypedDict

26

27 class PackageFileArgs(TypedDict, total=False):

28 url: str

29 package_name: str

30 package_version: str

31 file_name: str

32

33

34 class RawSource(Source):

35 """Raw urls repository."""

36

37 def __init__(

38 self,

39 *,

40 log_prefix: str = "",

41 default_url: str,

42 default_package_name: str | None = None,

43 default_package_version: str | None = None,

44 package_files: list[PackageFileArgs] | None = None,

45 ) -> None:

46 """Initialize a Raw Source object.

47

48 Args:

49 log_prefix: Log prefix.

50 default_url: Default package file URL.

51 default_package_name: Default package name.

52 default_package_version: Default package version.

53 package_files: Package files.

54 """

55 super().__init__()

56 self._log_prefix = log_prefix

57 self._default_url = default_url

58 self._default_package_name = default_package_name or "unknown"

59 self._default_package_version = default_package_version or "0"

60 self._package_files: list[PackageFileArgs] = package_files or [{}]

61

62 def __str__(self) -> str:

63 """Return string representation.

64

65 Returns:

66 A string.

67 """

68 return f"Raw repository (default_url={self._default_url})"

69

70 @property

71 def package_files(self) -> list[PackageFile]:

72 """Return list of package files.

73

74 Returns:

75 List of package files.

76 """

77 package_files: list[PackageFile] = []

78 for package_file_args in self._package_files:

79 package_file = self._package_file(package_file_args)

80 if package_file: 80 ↛ 78line 80 didn't jump to line 78 because the condition on line 80 was always true

81 package_files.append(package_file)

82 return package_files

83

84 def _package_file(self, package_file_args: PackageFileArgs) -> PackageFile | None:

85 url = package_file_args.get("url") or self._default_url

86 if not url: 86 ↛ 87line 86 didn't jump to line 87 because the condition on line 86 was never true

87 return None

88 package_name = package_file_args.get("package_name") or self._default_package_name

89 package_version = package_file_args.get("package_version") or self._default_package_version

90 file_name = package_file_args.get("file_name")

91 default_url = self._default_url.format(

92 default_package_name=self._default_package_name,

93 default_package_version=self._default_package_version,

94 package_name=package_name,

95 package_version=package_version,

96 file_name=file_name or "{file_name}",

97 )

98 url = url.format(

99 default_url=default_url,

100 default_package_name=self._default_package_name,

101 default_package_version=self._default_package_version,

102 package_name=package_name,

103 package_version=package_version,

104 file_name=file_name or "{file_name}",

105 )

106 if not file_name:

107 file_name = url.split("/").pop()

108 return PackageFile(

109 url,

110 "raw",

111 package_name,

112 package_version,

113 file_name,

114 )