1414#
1515# SPDX-License-Identifier: Apache-2.0
1616
17- __all__ = ['is_supported_id' , 'fixup_id' , 'is_compound_expression' ]
17+ __all__ = [
18+ 'is_supported_id' , 'fixup_id' ,
19+ 'is_compound_expression'
20+ ]
1821
1922from json import load as json_load
2023from os .path import dirname , join as path_join
21- from typing import Dict , Optional , Set
24+ from typing import TYPE_CHECKING , Dict , Optional , Set
25+
26+ from license_expression import get_spdx_licensing # type: ignore
27+
28+ if TYPE_CHECKING :
29+ from license_expression import Licensing
2230
2331# region init
2432# python's internal module loader will assure that this init-part runs only once.
3038
3139__IDS_LOWER_MAP : Dict [str , str ] = dict ((id_ .lower (), id_ ) for id_ in __IDS )
3240
41+ __SPDX_EXPRESSION_LICENSING : 'Licensing' = get_spdx_licensing ()
3342
3443# endregion
3544
45+
3646def is_supported_id (value : str ) -> bool :
3747 """Validate a SPDX-ID according to current spec."""
3848 return value in __IDS
@@ -50,11 +60,12 @@ def is_compound_expression(value: str) -> bool:
5060 """Validate compound expression.
5161
5262 .. note::
53- Uses a best-effort detection of SPDX compound expression according to `SPDX license expression spec`_.
63+ Utilizes `license-expression library`_ to
64+ validate SPDX compound expression according to `SPDX license expression spec`_.
5465
5566 .. _SPDX license expression spec: https://spdx.github.io/spdx-spec/v2.3/SPDX-license-expressions/
67+ .. _license-expression library: https://github.com/nexB/license-expression
5668 """
57- # shortest known valid expression: (A or B) - 8 characters long
58- return len (value ) >= 8 \
59- and value .startswith ('(' ) \
60- and value .endswith (')' )
69+ return 0 == len (
70+ __SPDX_EXPRESSION_LICENSING .validate (value ).errors
71+ )
0 commit comments