Skip to content

Commit 8d198cd

Browse files
committed
Move ExpressionInfo class definition #56
* Add test for get_license_key_info Signed-off-by: Jono Yang <[email protected]>
1 parent 4ab809f commit 8d198cd

File tree

2 files changed

+71
-29
lines changed

2 files changed

+71
-29
lines changed

src/license_expression/__init__.py

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,39 @@ class ExpressionParseError(ParseError, ExpressionError):
120120
).finditer
121121

122122

123+
class ExpressionInfo:
124+
"""
125+
The ExpressionInfo class is returned by Licensing.validate() where it stores
126+
information about a given license expression passed into
127+
Licensing.validate().
128+
129+
The ExpressionInfo class has the following fields:
130+
- normalized_license_expression: str.
131+
- If a valid license expression has been passed into `validate()`,
132+
then the license expression string will be set in this field.
133+
- errors: list
134+
- If there were errors validating a license expression,
135+
the error messages will be appended here.
136+
- valid_symbols: list
137+
- If a valid license expression has been passed into `validate()`,
138+
then the license symbols from the license expression will be
139+
appended here.
140+
- invalid_symbols: list
141+
- If an invalid license expression has been passed into `validate()`,
142+
then the invalid license symbols from the license expression will be
143+
appended here.
144+
- exception_symbols: list
145+
- If a license symbol in the license expression is a license exception,
146+
then that license symbol will be appended here.
147+
"""
148+
def __init__(self):
149+
self.normalized_license_expression = ''
150+
self.errors = []
151+
self.valid_symbols = []
152+
self.invalid_symbols = []
153+
self.exception_symbols = []
154+
155+
123156
class Licensing(boolean.BooleanAlgebra):
124157
"""
125158
Licensing defines a mini language to parse, validate and compare license
@@ -626,13 +659,6 @@ def validate(self, expression, strict=True, **kwargs):
626659
Return a ExpressionInfo object that contains information about
627660
`expression` by parsing `expression` using Licensing.parse()
628661
629-
The ExpressionInfo class has the following fields:
630-
- normalized_license_expression: str
631-
- errors: list
632-
- valid_symbols: list
633-
- invalid_symbols: list
634-
- exception_symbols: list
635-
636662
If `expression` is valid, then
637663
`ExpressionInfo.normalized_license_expression` is set, along with a list
638664
of valid license symbols in `valid_symbols` and `exception_symbols`.
@@ -648,47 +674,40 @@ def validate(self, expression, strict=True, **kwargs):
648674
set to True or the YYY symbol has `is_exception` set to False. This
649675
checks that symbols are used strictly as constructed.
650676
"""
651-
class ExpressionInfo:
652-
normalized_license_expression = ''
653-
errors = []
654-
valid_symbols = []
655-
invalid_symbols = []
656-
exception_symbols = []
657-
658-
data = ExpressionInfo()
677+
expression_info = ExpressionInfo()
659678

660679
# Check `expression` type
661680
try:
662681
self.parse(expression)
663682
except ExpressionError as e:
664-
data.errors.append(str(e))
665-
return data
683+
expression_info.errors.append(str(e))
684+
return expression_info
666685

667686
# Check `expression` syntax
668687
try:
669688
self.parse(expression, strict=strict)
670689
except ExpressionParseError as e:
671-
data.errors.append(str(e))
672-
data.invalid_symbols.append(e.token_string)
673-
return data
690+
expression_info.errors.append(str(e))
691+
expression_info.invalid_symbols.append(e.token_string)
692+
return expression_info
674693

675694
# Check `expression` keys
676695
try:
677696
parsed_expression = self.parse(expression, strict=strict, validate=True)
678697
except ExpressionError as e:
679698
error_message = str(e)
680-
data.errors.append(error_message)
699+
expression_info.errors.append(error_message)
681700
if 'Unknown license key' in error_message:
682701
unknown_keys = self.unknown_license_keys(expression)
683-
data.invalid_symbols.extend(unknown_keys)
684-
return data
702+
expression_info.invalid_symbols.extend(unknown_keys)
703+
return expression_info
685704

686-
# If we have not hit an exception, load `data` and return it
705+
# If we have not hit an exception, load `expression_info` and return it
687706
symbols = list(parsed_expression.symbols)
688-
data.normalized_license_expression = parsed_expression.render()
689-
data.valid_symbols = [s.render() for s in symbols]
690-
data.exception_symbols = [s.render() for s in symbols if isinstance(s, LicenseWithExceptionSymbol) or s.is_exception]
691-
return data
707+
expression_info.normalized_license_expression = parsed_expression.render()
708+
expression_info.valid_symbols = [s.render() for s in symbols]
709+
expression_info.exception_symbols = [s.render() for s in symbols if isinstance(s, LicenseWithExceptionSymbol) or s.is_exception]
710+
return expression_info
692711

693712

694713
def get_license_key_info(license_key_index_location=None):

tests/test_license_expression.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
#
77
import json
88
import sys
9+
import pathlib
910
from collections import namedtuple
1011
from unittest import TestCase
12+
from os.path import abspath
1113
from os.path import join
1214
from os.path import dirname
1315

@@ -41,7 +43,7 @@
4143
from license_expression import TOKEN_WITH
4244
from license_expression import build_licensing
4345
from license_expression import build_spdx_licensing
44-
46+
from license_expression import get_license_key_info
4547

4648

4749
def _parse_error_as_dict(pe):
@@ -2322,3 +2324,24 @@ def test_build_spdx_licensing(self):
23222324

23232325
assert expected.known_symbols == result.known_symbols
23242326
assert expected.known_symbols_lowercase == result.known_symbols_lowercase
2327+
2328+
def test_get_license_key_info(self):
2329+
test_license_key_index_location = join(self.test_data_dir, 'test_license_key_index.json')
2330+
with open(test_license_key_index_location, 'r') as f:
2331+
expected = json.load(f)
2332+
result = get_license_key_info(test_license_key_index_location)
2333+
assert expected == result
2334+
2335+
def test_get_license_key_info_vendored(self):
2336+
curr_dir = dirname(abspath(__file__))
2337+
parent_dir = pathlib.Path(curr_dir).parent
2338+
vendored_license_key_index_location = parent_dir.joinpath(
2339+
'src',
2340+
'license_expression',
2341+
'data',
2342+
'license_key_index.json'
2343+
)
2344+
with open(vendored_license_key_index_location, 'r') as f:
2345+
expected = json.load(f)
2346+
result = get_license_key_info()
2347+
assert expected == result

0 commit comments

Comments
 (0)