forked from spdx/tools-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlicense_expression_parser.py
More file actions
29 lines (24 loc) · 1.13 KB
/
license_expression_parser.py
File metadata and controls
29 lines (24 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# SPDX-FileCopyrightText: 2022 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0
from beartype.typing import Union
from license_expression import ExpressionError, LicenseExpression
from spdx_tools.common.spdx_licensing import spdx_licensing
from spdx_tools.spdx.model import SpdxNoAssertion, SpdxNone
from spdx_tools.spdx.parser.error import SPDXParsingError
class LicenseExpressionParser:
@staticmethod
def parse_license_expression(license_expression_str: str) -> Union[LicenseExpression, SpdxNone, SpdxNoAssertion]:
if isinstance(license_expression_str, str):
if license_expression_str.upper() == "NOASSERTION":
return SpdxNoAssertion()
if license_expression_str.upper() == "NONE":
return SpdxNone()
try:
license_expression = spdx_licensing.parse(license_expression_str)
except ExpressionError as err:
err_msg = f'Error parsing LicenseExpression: "{license_expression_str}"'
if err.args:
err_msg += f": {err.args[0]}"
raise SPDXParsingError([err_msg])
return license_expression