Skip to content

Commit ac80a21

Browse files
committed
Update ExpressionInfo #56
* Add repr to ExpressionInfo class * Remove valid_symbols and valid_exception_symbols from ExpressionInfo * Update vendored licensedb index * Avoid indexing deprecated licenses * Update tests Signed-off-by: Jono Yang <[email protected]>
1 parent f6c8fbd commit ac80a21

File tree

4 files changed

+1894
-92
lines changed

4 files changed

+1894
-92
lines changed

src/license_expression/__init__.py

Lines changed: 45 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -133,40 +133,40 @@ class ExpressionInfo:
133133
Licensing.validate().
134134
135135
The ExpressionInfo class has the following fields:
136-
- original_license_expression: str.
136+
- original_expression: str.
137137
- This is the license expression that was originally passed into Licensing.validate()
138-
- normalized_license_expression: str.
138+
- normalized_expression: str.
139139
- If a valid license expression has been passed into `validate()`,
140140
then the license expression string will be set in this field.
141141
- errors: list
142142
- If there were errors validating a license expression,
143143
the error messages will be appended here.
144-
- valid_symbols: list
145-
- If a valid license expression has been passed into `validate()`,
146-
then the license symbols from the license expression will be
147-
appended here.
148-
- valid_exception_symbols: list
149-
- If a license symbol in the license expression is a license exception,
150-
then that license symbol will be appended here.
151-
- invalid_symbols: list
152-
- If an invalid license expression has been passed into `validate()`,
153-
then the invalid license symbols from the license expression will be
154-
appended here.
144+
- invalid_keys: list
145+
- If the license expression that has been passed into `validate()` has
146+
license keys that are invalid (either that they are unknown or not used
147+
in the right context), then those keys will be appended here.
155148
"""
156149
def __init__(
157-
self,
158-
original_license_expression,
159-
normalized_license_expression=None,
160-
errors=None,
161-
valid_symbols=None,
162-
valid_exception_symbols=None,
163-
invalid_symbols=None):
164-
self.original_license_expression = original_license_expression
165-
self.normalized_license_expression = normalized_license_expression or ''
150+
self,
151+
original_expression,
152+
normalized_expression=None,
153+
errors=None,
154+
invalid_keys=None,
155+
):
156+
self.original_expression = original_expression
157+
self.normalized_expression = normalized_expression
166158
self.errors = errors or []
167-
self.valid_symbols = valid_symbols or []
168-
self.valid_exception_symbols = valid_exception_symbols or []
169-
self.invalid_symbols = invalid_symbols or []
159+
self.invalid_keys = invalid_keys or []
160+
161+
def __repr__(self):
162+
return (
163+
'ExpressionInfo(\n'
164+
f' original_expression={self.original_expression!r},\n'
165+
f' normalized_expression={self.normalized_expression!r},\n'
166+
f' errors={self.errors!r},\n'
167+
f' invalid_keys={self.invalid_keys!r}\n'
168+
')'
169+
)
170170

171171

172172
class Licensing(boolean.BooleanAlgebra):
@@ -678,35 +678,22 @@ def validate(self, expression, strict=True, **kwargs):
678678
Return a ExpressionInfo object that contains information about
679679
the validation of an `expression` license expression string.
680680
681-
If `expression` is valid, then
682-
`ExpressionInfo.normalized_license_expression` is set, along with a list
683-
of valid license symbols in `valid_symbols` and `exception_symbols`.
681+
If the syntax and license keys of `expression` is valid, then
682+
`ExpressionInfo.normalized_license_expression` is set.
684683
685684
If an error was encountered when validating `expression`,
686685
`ExpressionInfo.errors` will be populated with strings containing the
687-
error message that has occured. If an error has occured due to invalid
688-
license symbols, the offending symbols will be present in
689-
`ExpressionInfo.invalid_symbols`
686+
error message that has occured. If an error has occured due to unknown
687+
license keys, the offending keys will be present in
688+
`ExpressionInfo.invalid_keys`
690689
691690
If `strict` is True, validation error messages will be included if in a "WITH"
692691
expression such as "XXX with ZZZ" if the XXX symbol has `is_exception`
693692
set to True or the YYY symbol has `is_exception` set to False. This
694693
checks that symbols are used strictly as intended.
695694
"""
696-
def set_ExpressionInfo_fields(parsed_expression, expression_info):
697-
symbols = list(parsed_expression.symbols)
698-
expression_info.normalized_license_expression = str(parsed_expression)
699-
expression_info.valid_symbols = [s.render() for s in symbols]
700-
expression_info.valid_exception_symbols = [
701-
s.render()
702-
for s in symbols
703-
if isinstance(s, LicenseWithExceptionSymbol)
704-
or s.is_exception
705-
]
706-
return expression_info
707-
708695
expression_info = ExpressionInfo(
709-
original_license_expression=str(expression)
696+
original_expression=str(expression)
710697
)
711698

712699
# Check `expression` type
@@ -722,26 +709,23 @@ def set_ExpressionInfo_fields(parsed_expression, expression_info):
722709
parsed_expression = self.parse(expression, strict=strict)
723710
except ExpressionParseError as e:
724711
expression_info.errors.append(str(e))
725-
expression_info.invalid_symbols.append(e.token_string)
712+
expression_info.invalid_keys.append(e.token_string)
726713

727714
# Check `expression` keys (validate)
728715
try:
729716
self.validate_license_keys(expression)
730717
except ExpressionError as e:
731-
error_message = str(e)
732-
expression_info.errors.append(error_message)
718+
expression_info.errors.append(str(e))
733719
unknown_keys = self.unknown_license_keys(expression)
734-
expression_info.invalid_symbols.extend(unknown_keys)
735-
return set_ExpressionInfo_fields(
736-
parsed_expression=parsed_expression,
737-
expression_info=expression_info
738-
)
720+
expression_info.invalid_keys.extend(unknown_keys)
721+
return expression_info
739722

740-
# If we have not hit an exception, load `expression_info` and return it
741-
return set_ExpressionInfo_fields(
742-
parsed_expression=parsed_expression,
743-
expression_info=expression_info
744-
)
723+
# If we have not hit an exception, set `normalized_expression` in
724+
# `expression_info` only if we did not encounter any errors
725+
# along the way
726+
if not expression_info.errors and not expression_info.invalid_keys:
727+
expression_info.normalized_expression = str(parsed_expression)
728+
return expression_info
745729

746730

747731
def get_license_index(license_index_location=vendored_scancode_licensedb_index_location):
@@ -773,7 +757,7 @@ def build_licensing(license_index):
773757
{
774758
'key': l.get('license_key', ''),
775759
'is_exception': l.get('is_exception', ''),
776-
} for l in license_index
760+
} for l in license_index if not l.get('is_deprecated', False)
777761
]
778762
return load_licensing_from_license_index(lics)
779763

@@ -788,7 +772,9 @@ def build_spdx_licensing(license_index):
788772
'key': l.get('spdx_license_key', ''),
789773
'aliases': l.get('other_spdx_license_keys', ''),
790774
'is_exception': l.get('is_exception', ''),
791-
} for l in license_index if l.get('spdx_license_key')
775+
} for l in license_index
776+
if l.get('spdx_license_key')
777+
and not l.get('is_deprecated', False)
792778
]
793779
return load_licensing_from_license_index(lics)
794780

0 commit comments

Comments
 (0)