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

75 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-11-21 22:22 +0000

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

2# _____ _ _ _ _ _ # 

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

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

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

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

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

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

9# Authors: # 

10# Patrick Lehmann # 

11# # 

12# License: # 

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

14# Copyright 2017-2025 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:: See :ref:`high-level help <LICENSING>` for explanations and usage examples. 

44""" 

45from dataclasses import dataclass 

46from typing import Any, Dict, Optional as Nullable 

47 

48 

49try: 

50 from pyTooling.Decorators import export, readonly 

51 from pyTooling.MetaClasses import ExtendedType 

52except (ImportError, ModuleNotFoundError): # pragma: no cover 

53 print("[pyTooling.Licensing] Could not import from 'pyTooling.*'!") 

54 

55 try: 

56 from Decorators import export, readonly 

57 from MetaClasses import ExtendedType 

58 except (ImportError, ModuleNotFoundError) as ex: # pragma: no cover 

59 print("[pyTooling.Licensing] Could not import directly!") 

60 raise ex 

61 

62 

63__all__ = [ 

64 "PYTHON_LICENSE_NAMES", 

65 

66 "Apache_2_0_License", 

67 "BSD_3_Clause_License", 

68 "GPL_2_0_or_later", 

69 "MIT_License", 

70 

71 "SPDX_INDEX" 

72] 

73 

74 

75@export 

76@dataclass 

77class PythonLicenseName: 

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

79 

80 ShortName: str #: License's short name 

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

82 

83 def __str__(self) -> str: 

84 """ 

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

86 

87 :returns: Short name of the license. 

88 """ 

89 return self.ShortName 

90 

91 

92#: Mapping of SPDX identifiers to Python license names 

93PYTHON_LICENSE_NAMES: Dict[str, PythonLicenseName] = { 

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

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

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

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

98} 

99 

100 

101@export 

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

103 """Representation of a license.""" 

104 

105 _spdxIdentifier: str #: Unique SPDX identifier. 

106 _name: str #: Name of the license. 

107 _osiApproved: bool #: OSI approval status 

108 _fsfApproved: bool #: FSF approval status 

109 

110 def __init__(self, spdxIdentifier: str, name: str, osiApproved: bool = False, fsfApproved: bool = False): 

111 self._spdxIdentifier = spdxIdentifier 

112 self._name = name 

113 self._osiApproved = osiApproved 

114 self._fsfApproved = fsfApproved 

115 

116 @readonly 

117 def Name(self) -> str: 

118 """ 

119 Returns the license' name. 

120 

121 :returns: License name. 

122 """ 

123 return self._name 

124 

125 @readonly 

126 def SPDXIdentifier(self) -> str: 

127 """ 

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

129 

130 :returns: The the unique SPDX identifier. 

131 """ 

132 return self._spdxIdentifier 

133 

134 @readonly 

135 def OSIApproved(self) -> bool: 

136 """ 

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

138 

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

140 """ 

141 return self._osiApproved 

142 

143 @readonly 

144 def FSFApproved(self) -> bool: 

145 """ 

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

147 

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

149 """ 

150 return self._fsfApproved 

151 

152 @readonly 

153 def PythonLicenseName(self) -> str: 

154 """ 

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

156 

157 :returns: The Python license name. 

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

159 """ 

160 try: 

161 item: PythonLicenseName = PYTHON_LICENSE_NAMES[self._spdxIdentifier] 

162 except KeyError as ex: 

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

164 

165 return item.ShortName 

166 

167 @readonly 

168 def PythonClassifier(self) -> str: 

169 """ 

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

171 

172 :returns: The Python package classifier. 

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

174 

175 .. seealso:: 

176 

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

178 """ 

179 try: 

180 item: PythonLicenseName = PYTHON_LICENSE_NAMES[self._spdxIdentifier] 

181 except KeyError as ex: 

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

183 

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

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

186 

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

188 """ 

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

190 

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

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

193 """ 

194 if isinstance(other, License): 

195 return self._spdxIdentifier == other._spdxIdentifier 

196 else: 

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

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

199 raise ex 

200 

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

202 """ 

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

204 

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

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

207 """ 

208 if isinstance(other, License): 

209 return self._spdxIdentifier != other._spdxIdentifier 

210 else: 

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

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

213 raise ex 

214 

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

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

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

218 

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

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

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

222 

223 def __repr__(self) -> str: 

224 """ 

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

226 

227 :returns: SPDX identifier of the license. 

228 """ 

229 return self._spdxIdentifier 

230 

231 def __str__(self) -> str: 

232 """ 

233 Returns the license' name. 

234 

235 :returns: Name of the license. 

236 """ 

237 return self._name 

238 

239 

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

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

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

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

244 

245 

246#: Mapping of predefined licenses 

247SPDX_INDEX: Dict[str, License] = { 

248 "Apache-2.0": Apache_2_0_License, 

249 "BSD-3-Clause": BSD_3_Clause_License, 

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

251 "MIT": MIT_License 

252}