Coverage for pyTooling / Licensing / __init__.py: 92%

74 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-07 17:18 +0000

1# ==================================================================================================================== # 

2# _____ _ _ _ _ _ # 

3# _ __ _ |_ _|__ ___ | (_)_ __ __ _ | | (_) ___ ___ _ __ ___(_)_ __ __ _ # 

4# | '_ \| | | || |/ _ \ / _ \| | | '_ \ / _` | | | | |/ __/ _ \ '_ \/ __| | '_ \ / _` | # 

5# | |_) | |_| || | (_) | (_) | | | | | | (_| |_| |___| | (_| __/ | | \__ \ | | | | (_| | # 

6# | .__/ \__, ||_|\___/ \___/|_|_|_| |_|\__, (_)_____|_|\___\___|_| |_|___/_|_| |_|\__, | # 

7# |_| |___/ |___/ |___/ # 

8# ==================================================================================================================== # 

9# Authors: # 

10# Patrick Lehmann # 

11# # 

12# License: # 

13# ==================================================================================================================== # 

14# Copyright 2017-2026 Patrick Lehmann - Bötzingen, Germany # 

15# # 

16# Licensed under the Apache License, Version 2.0 (the "License"); # 

17# you may not use this file except in compliance with the License. # 

18# You may obtain a copy of the License at # 

19# # 

20# http://www.apache.org/licenses/LICENSE-2.0 # 

21# # 

22# Unless required by applicable law or agreed to in writing, software # 

23# distributed under the License is distributed on an "AS IS" BASIS, # 

24# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 

25# See the License for the specific language governing permissions and # 

26# limitations under the License. # 

27# # 

28# SPDX-License-Identifier: Apache-2.0 # 

29# ==================================================================================================================== # 

30# 

31""" 

32The Licensing module implements mapping tables for various license names and identifiers. 

33 

34.. seealso:: 

35 

36 List of SPDX identifiers: 

37 

38 * https://spdx.org/licenses/ 

39 * https://github.com/spdx/license-list-XML 

40 

41 List of `Python classifiers <https://pypi.org/classifiers/>`__ 

42 

43.. hint:: 

44 

45 See :ref:`high-level help <LICENSING>` for explanations and usage examples. 

46""" 

47from dataclasses import dataclass 

48from typing import Any, Dict 

49 

50from pyTooling.Decorators import export, readonly 

51from pyTooling.MetaClasses import ExtendedType 

52 

53 

54__all__ = [ 

55 "PYTHON_LICENSE_NAMES", 

56 

57 "Apache_2_0_License", 

58 "BSD_3_Clause_License", 

59 "GPL_2_0_or_later", 

60 "MIT_License", 

61 

62 "SPDX_INDEX" 

63] 

64 

65 

66@export 

67@dataclass 

68class PythonLicenseName: 

69 """A *data class* to represent the license's short name and the package classifier for a license.""" 

70 

71 ShortName: str #: License's short name 

72 Classifier: str #: Package classifier for a license. 

73 

74 def __str__(self) -> str: 

75 """ 

76 The string representation of this name tuple returns the short name of the license. 

77 

78 :returns: Short name of the license. 

79 """ 

80 return self.ShortName 

81 

82 

83#: Mapping of SPDX identifiers to Python license names 

84PYTHON_LICENSE_NAMES: Dict[str, PythonLicenseName] = { 

85 "Apache-2.0": PythonLicenseName("Apache 2.0", "Apache Software License"), 

86 "BSD-3-Clause": PythonLicenseName("BSD", "BSD License"), 

87 "MIT": PythonLicenseName("MIT", "MIT License"), 

88 "GPL-2.0-or-later": PythonLicenseName("GPL-2.0-or-later", "GNU General Public License v2 or later (GPLv2+)"), 

89} 

90 

91 

92@export 

93class License(metaclass=ExtendedType, slots=True): 

94 """Representation of a license.""" 

95 

96 _spdxIdentifier: str #: Unique SPDX identifier. 

97 _name: str #: Name of the license. 

98 _osiApproved: bool #: OSI approval status 

99 _fsfApproved: bool #: FSF approval status 

100 

101 def __init__(self, spdxIdentifier: str, name: str, osiApproved: bool = False, fsfApproved: bool = False) -> None: 

102 self._spdxIdentifier = spdxIdentifier 

103 self._name = name 

104 self._osiApproved = osiApproved 

105 self._fsfApproved = fsfApproved 

106 

107 @readonly 

108 def Name(self) -> str: 

109 """ 

110 Returns the license' name. 

111 

112 :returns: License name. 

113 """ 

114 return self._name 

115 

116 @readonly 

117 def SPDXIdentifier(self) -> str: 

118 """ 

119 Returns the license' unique `SPDX identifier <https://spdx.org/licenses/>`__. 

120 

121 :returns: The the unique SPDX identifier. 

122 """ 

123 return self._spdxIdentifier 

124 

125 @readonly 

126 def OSIApproved(self) -> bool: 

127 """ 

128 Returns true, if the license is approved by OSI (`Open Source Initiative <https://opensource.org/>`__). 

129 

130 :returns: ``True``, if the license is approved by the Open Source Initiative. 

131 """ 

132 return self._osiApproved 

133 

134 @readonly 

135 def FSFApproved(self) -> bool: 

136 """ 

137 Returns true, if the license is approved by FSF (`Free Software Foundation <https://www.fsf.org/>`__). 

138 

139 :returns: ``True``, if the license is approved by the Free Software Foundation. 

140 """ 

141 return self._fsfApproved 

142 

143 @readonly 

144 def PythonLicenseName(self) -> str: 

145 """ 

146 Returns the Python license name for this license if it's defined. 

147 

148 :returns: The Python license name. 

149 :raises ValueError: If there is no license name defined for the license. |br| (See and check :data:`~pyTooling.Licensing.PYTHON_LICENSE_NAMES`) 

150 """ 

151 try: 

152 item: PythonLicenseName = PYTHON_LICENSE_NAMES[self._spdxIdentifier] 

153 except KeyError as ex: 

154 raise ValueError("License has no Python specify information.") from ex 

155 

156 return item.ShortName 

157 

158 @readonly 

159 def PythonClassifier(self) -> str: 

160 """ 

161 Returns the Python package classifier for this license if it's defined. 

162 

163 :returns: The Python package classifier. 

164 :raises ValueError: If there is no classifier defined for the license. |br| (See and check :data:`~pyTooling.Licensing.PYTHON_LICENSE_NAMES`) 

165 

166 .. seealso:: 

167 

168 List of `Python classifiers <https://pypi.org/classifiers/>`__ 

169 """ 

170 try: 

171 item: PythonLicenseName = PYTHON_LICENSE_NAMES[self._spdxIdentifier] 

172 except KeyError as ex: 

173 raise ValueError(f"License has no Python specify information.") from ex 

174 

175 osi = "OSI Approved :: " if self._osiApproved else "" 

176 return f"License :: {osi}{item.Classifier}" 

177 

178 def __eq__(self, other: Any) -> bool: 

179 """ 

180 Returns true, if both licenses are identical (comparison based on SPDX identifiers). 

181 

182 :returns: ``True``, if both licenses are identical. 

183 :raises TypeError: If second operand is not of type :class:`License` or :class:`str`. 

184 """ 

185 if isinstance(other, License): 

186 return self._spdxIdentifier == other._spdxIdentifier 

187 else: 

188 ex = TypeError(f"Second operand of type '{other.__class__.__name__}' is not supported by equal operator.") 

189 ex.add_note(f"Supported types for second operand: License, str") 

190 raise ex 

191 

192 def __ne__(self, other: Any) -> bool: 

193 """ 

194 Returns true, if both licenses are not identical (comparison based on SPDX identifiers). 

195 

196 :returns: ``True``, if both licenses are not identical. 

197 :raises TypeError: If second operand is not of type :class:`License` or :class:`str`. 

198 """ 

199 if isinstance(other, License): 

200 return self._spdxIdentifier != other._spdxIdentifier 

201 else: 

202 ex = TypeError(f"Second operand of type '{other.__class__.__name__}' is not supported by unequal operator.") 

203 ex.add_note(f"Supported types for second operand: License, str") 

204 raise ex 

205 

206 def __le__(self, other: Any) -> bool: 

207 """Returns true, if both licenses are compatible.""" 

208 raise NotImplementedError("License compatibility check is not yet implemented.") 

209 

210 def __ge__(self, other: Any) -> bool: 

211 """Returns true, if both licenses are compatible.""" 

212 raise NotImplementedError("License compatibility check is not yet implemented.") 

213 

214 def __repr__(self) -> str: 

215 """ 

216 Returns the internal unique representation (a.k.a SPDX identifier). 

217 

218 :returns: SPDX identifier of the license. 

219 """ 

220 return self._spdxIdentifier 

221 

222 def __str__(self) -> str: 

223 """ 

224 Returns the license' name. 

225 

226 :returns: Name of the license. 

227 """ 

228 return self._name 

229 

230 

231Apache_2_0_License = License("Apache-2.0", "Apache License 2.0", True, True) 

232BSD_3_Clause_License = License("BSD-3-Clause", "BSD 3-Clause Revised License", True, True) 

233GPL_2_0_or_later = License("GPL-2.0-or-later", "GNU General Public License v2.0 or later", True, True) 

234MIT_License = License("MIT", "MIT License", True, True) 

235 

236 

237#: Mapping of predefined licenses 

238SPDX_INDEX: Dict[str, License] = { 

239 "Apache-2.0": Apache_2_0_License, 

240 "BSD-3-Clause": BSD_3_Clause_License, 

241 "GPL-2.0-or-later": GPL_2_0_or_later, 

242 "MIT": MIT_License 

243}