Skip to content

Commit 4128774

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 <jyang@nexb.com>
1 parent f6c8fbd commit 4128774

4 files changed

Lines changed: 1889 additions & 88 deletions

File tree

src/license_expression/__init__.py

Lines changed: 42 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -133,40 +133,39 @@ 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__(
157150
self,
158-
original_license_expression,
159-
normalized_license_expression=None,
151+
original_expression,
152+
normalized_expression=None,
160153
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 ''
154+
invalid_keys=None):
155+
self.original_expression = original_expression
156+
self.normalized_expression = normalized_expression
166157
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 []
158+
self.invalid_keys = invalid_keys or []
159+
160+
def __repr__(self):
161+
return (
162+
'ExpressionInfo(\n'
163+
f' original_expression={self.original_expression!r},\n'
164+
f' normalized_expression={self.normalized_expression!r},\n'
165+
f' errors={self.errors!r},\n'
166+
f' invalid_keys={self.invalid_keys!r}\n'
167+
')'
168+
)
170169

171170

172171
class Licensing(boolean.BooleanAlgebra):
@@ -678,35 +677,22 @@ def validate(self, expression, strict=True, **kwargs):
678677
Return a ExpressionInfo object that contains information about
679678
the validation of an `expression` license expression string.
680679
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`.
680+
If the syntax and license keys of `expression` is valid, then
681+
`ExpressionInfo.normalized_license_expression` is set.
684682
685683
If an error was encountered when validating `expression`,
686684
`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`
685+
error message that has occured. If an error has occured due to unknown
686+
license keys, the offending keys will be present in
687+
`ExpressionInfo.invalid_keys`
690688
691689
If `strict` is True, validation error messages will be included if in a "WITH"
692690
expression such as "XXX with ZZZ" if the XXX symbol has `is_exception`
693691
set to True or the YYY symbol has `is_exception` set to False. This
694692
checks that symbols are used strictly as intended.
695693
"""
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-
708694
expression_info = ExpressionInfo(
709-
original_license_expression=str(expression)
695+
original_expression=str(expression)
710696
)
711697

712698
# Check `expression` type
@@ -722,26 +708,23 @@ def set_ExpressionInfo_fields(parsed_expression, expression_info):
722708
parsed_expression = self.parse(expression, strict=strict)
723709
except ExpressionParseError as e:
724710
expression_info.errors.append(str(e))
725-
expression_info.invalid_symbols.append(e.token_string)
711+
expression_info.invalid_keys.append(e.token_string)
726712

727713
# Check `expression` keys (validate)
728714
try:
729715
self.validate_license_keys(expression)
730716
except ExpressionError as e:
731-
error_message = str(e)
732-
expression_info.errors.append(error_message)
717+
expression_info.errors.append(str(e))
733718
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-
)
719+
expression_info.invalid_keys.extend(unknown_keys)
720+
return expression_info
739721

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-
)
722+
# If we have not hit an exception, set `normalized_expression` in
723+
# `expression_info` only if we did not encounter any errors
724+
# along the way
725+
if not expression_info.errors and not expression_info.invalid_keys:
726+
expression_info.normalized_expression = str(parsed_expression)
727+
return expression_info
745728

746729

747730
def get_license_index(license_index_location=vendored_scancode_licensedb_index_location):
@@ -773,7 +756,7 @@ def build_licensing(license_index):
773756
{
774757
'key': l.get('license_key', ''),
775758
'is_exception': l.get('is_exception', ''),
776-
} for l in license_index
759+
} for l in license_index if not l.get('is_deprecated', False)
777760
]
778761
return load_licensing_from_license_index(lics)
779762

@@ -788,7 +771,9 @@ def build_spdx_licensing(license_index):
788771
'key': l.get('spdx_license_key', ''),
789772
'aliases': l.get('other_spdx_license_keys', ''),
790773
'is_exception': l.get('is_exception', ''),
791-
} for l in license_index if l.get('spdx_license_key')
774+
} for l in license_index
775+
if l.get('spdx_license_key')
776+
and not l.get('is_deprecated', False)
792777
]
793778
return load_licensing_from_license_index(lics)
794779

0 commit comments

Comments
 (0)