Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[issue-10] use license_expression library for LicenseExpression #447

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Expand Up @@ -24,7 +24,7 @@ classifiers = [
]
urls = {Homepage = "https://github.com/spdx/tools-python"}
requires-python = ">=3.7"
dependencies = ["ply", "rdflib", "click", "pyyaml", "xmltodict", "typeguard", "uritools"]
dependencies = ["ply", "rdflib", "click", "pyyaml", "xmltodict", "typeguard", "uritools", "license_expression"]
dynamic = ["version"]

[project.optional-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/spdx/jsonschema/optional_utils.py
Expand Up @@ -18,4 +18,4 @@ def apply_if_present(function: Callable[[T], S], optional_value: Optional[T]) ->
"""
Apply the passed function to the optional value if it is not None. Else returns None.
"""
return function(optional_value) if optional_value else None
return function(optional_value) if optional_value is not None else None
2 changes: 1 addition & 1 deletion src/spdx/model/file.py
Expand Up @@ -14,7 +14,7 @@

from spdx.model.checksum import Checksum
from common.typing.dataclass_with_properties import dataclass_with_properties
from spdx.model.license_expression import LicenseExpression
from license_expression import LicenseExpression
from spdx.model.spdx_no_assertion import SpdxNoAssertion
from spdx.model.spdx_none import SpdxNone
from common.typing.type_checks import check_types_and_set_values
Expand Down
26 changes: 0 additions & 26 deletions src/spdx/model/license_expression.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/spdx/model/package.py
Expand Up @@ -16,7 +16,7 @@
from spdx.model.actor import Actor
from spdx.model.checksum import Checksum
from common.typing.dataclass_with_properties import dataclass_with_properties
from spdx.model.license_expression import LicenseExpression
from license_expression import LicenseExpression
from spdx.model.spdx_no_assertion import SpdxNoAssertion
from spdx.model.spdx_none import SpdxNone
from common.typing.type_checks import check_types_and_set_values
Expand Down
2 changes: 1 addition & 1 deletion src/spdx/model/snippet.py
Expand Up @@ -12,7 +12,7 @@
from typing import Tuple, Optional, List, Union

from common.typing.dataclass_with_properties import dataclass_with_properties
from spdx.model.license_expression import LicenseExpression
from license_expression import LicenseExpression
from spdx.model.spdx_no_assertion import SpdxNoAssertion
from spdx.model.spdx_none import SpdxNone
from common.typing.type_checks import check_types_and_set_values
Expand Down
2 changes: 1 addition & 1 deletion src/spdx/parser/jsonlikedict/file_parser.py
Expand Up @@ -12,7 +12,7 @@

from spdx.model.checksum import Checksum
from spdx.model.file import File, FileType
from spdx.model.license_expression import LicenseExpression
from license_expression import LicenseExpression
from spdx.model.spdx_no_assertion import SpdxNoAssertion
from spdx.model.spdx_none import SpdxNone
from spdx.parser.jsonlikedict.checksum_parser import ChecksumParser
Expand Down
37 changes: 24 additions & 13 deletions src/spdx/parser/jsonlikedict/license_expression_parser.py
Expand Up @@ -10,31 +10,42 @@
# limitations under the License.
from typing import Union, List

from spdx.model.license_expression import LicenseExpression
from license_expression import LicenseExpression, Licensing, ExpressionError

from spdx.model.spdx_no_assertion import SpdxNoAssertion
from spdx.model.spdx_none import SpdxNone
from spdx.parser.error import SPDXParsingError
from spdx.parser.jsonlikedict.dict_parsing_functions import construct_or_raise_parsing_error, append_parsed_field_or_log_error, \
from spdx.parser.jsonlikedict.dict_parsing_functions import append_parsed_field_or_log_error, \
raise_parsing_error_if_logger_has_messages
from spdx.parser.logger import Logger


class LicenseExpressionParser:
@staticmethod
def parse_license_expression(license_expression_str_or_list: str) -> LicenseExpression:
license_expression = construct_or_raise_parsing_error(LicenseExpression,
dict(expression_string=license_expression_str_or_list))
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 = Licensing().parse(license_expression_str)
except ExpressionError as err:
raise SPDXParsingError([f"Error parsing LicenseExpression: {err.args[0]}: {license_expression_str}"])

return license_expression

def parse_license_expressions(self, license_expression_str_or_list: Union[str, List[str]]) -> Union[LicenseExpression, List[LicenseExpression]]:
if isinstance(license_expression_str_or_list, str):
def parse_license_expressions(self, license_expression_str_or_list: Union[str, List[str]]) -> Union[
LicenseExpression, SpdxNone, SpdxNoAssertion, List[Union[LicenseExpression, SpdxNone, SpdxNoAssertion]]]:
if not isinstance(license_expression_str_or_list, List):
return self.parse_license_expression(license_expression_str_or_list)

license_expressions = []
logger = Logger()
for license_expression_str in license_expression_str_or_list:
try:
license_expressions = append_parsed_field_or_log_error(logger, license_expressions,
license_expression_str,
self.parse_license_expression)
except SPDXParsingError as err:
logger.append(err.get_messages())
license_expressions = append_parsed_field_or_log_error(logger, license_expressions,
license_expression_str,
self.parse_license_expression)
raise_parsing_error_if_logger_has_messages(logger)
return license_expressions
2 changes: 1 addition & 1 deletion src/spdx/parser/jsonlikedict/package_parser.py
Expand Up @@ -12,7 +12,7 @@
from typing import Dict, List, Optional, Union

from spdx.model.actor import Actor
from spdx.model.license_expression import LicenseExpression
from license_expression import LicenseExpression
from spdx.model.package import Package, ExternalPackageRef, PackageVerificationCode, PackagePurpose, \
ExternalPackageRefCategory
from spdx.model.spdx_no_assertion import SpdxNoAssertion
Expand Down
2 changes: 1 addition & 1 deletion src/spdx/parser/jsonlikedict/snippet_parser.py
Expand Up @@ -11,7 +11,7 @@
from enum import auto, Enum
from typing import Dict, Tuple, List, Optional, Union

from spdx.model.license_expression import LicenseExpression
from license_expression import LicenseExpression
from spdx.model.snippet import Snippet
from spdx.model.spdx_no_assertion import SpdxNoAssertion
from spdx.model.spdx_none import SpdxNone
Expand Down
2 changes: 1 addition & 1 deletion src/spdx/validation/license_expression_validator.py
Expand Up @@ -11,7 +11,7 @@

from typing import List, Optional, Union

from spdx.model.license_expression import LicenseExpression
from license_expression import LicenseExpression
from spdx.model.spdx_no_assertion import SpdxNoAssertion
from spdx.model.spdx_none import SpdxNone
from spdx.validation.validation_message import ValidationMessage
Expand Down
7 changes: 3 additions & 4 deletions src/spdx/writer/tagvalue/file_writer.py
Expand Up @@ -11,9 +11,8 @@
from typing import TextIO

from spdx.model.file import File
from spdx.writer.tagvalue.tagvalue_writer_helper_functions import write_value, write_text_value, \
write_license_expression
from spdx.writer.tagvalue.checksum_writer import write_checksum_to_tag_value
from spdx.writer.tagvalue.tagvalue_writer_helper_functions import write_value, write_text_value, write_license_info_list


def write_file(file: File, text_output: TextIO):
Expand All @@ -28,8 +27,8 @@ def write_file(file: File, text_output: TextIO):
for file_checksum in file.checksums:
write_value("FileChecksum", write_checksum_to_tag_value(file_checksum), text_output)

write_license_expression("LicenseConcluded", file.license_concluded, text_output)
write_license_expression("LicenseInfoInFile", file.license_info_in_file, text_output)
write_value("LicenseConcluded", file.license_concluded, text_output)
write_license_info_list("LicenseInfoInFile", file.license_info_in_file, text_output)
write_text_value("LicenseComments", file.license_comment, text_output)
write_text_value("FileCopyrightText", file.copyright_text, text_output)

Expand Down
10 changes: 5 additions & 5 deletions src/spdx/writer/tagvalue/package_writer.py
Expand Up @@ -12,9 +12,9 @@

from spdx.datetime_conversions import datetime_to_iso_string
from spdx.model.package import Package, PackageVerificationCode
from spdx.writer.tagvalue.tagvalue_writer_helper_functions import write_value, write_text_value, \
write_license_expression, transform_enum_name_to_tv, write_actor
from spdx.writer.tagvalue.checksum_writer import write_checksum_to_tag_value
from spdx.writer.tagvalue.tagvalue_writer_helper_functions import write_value, write_text_value, \
transform_enum_name_to_tv, write_actor, write_license_info_list


def write_package(package: Package, text_output: TextIO):
Expand All @@ -39,9 +39,9 @@ def write_package(package: Package, text_output: TextIO):
write_value("PackageHomePage", package.homepage, text_output)
write_text_value("PackageSourceInfo", package.source_info, text_output)

write_license_expression("PackageLicenseConcluded", package.license_concluded, text_output)
write_license_expression("PackageLicenseInfoFromFiles", package.license_info_from_files, text_output)
write_license_expression("PackageLicenseDeclared", package.license_declared, text_output)
write_value("PackageLicenseConcluded", package.license_concluded, text_output)
write_license_info_list("PackageLicenseInfoFromFiles", package.license_info_from_files, text_output)
write_value("PackageLicenseDeclared", package.license_declared, text_output)
write_text_value("PackageLicenseComments", package.license_comment, text_output)
write_text_value("PackageCopyrightText", package.copyright_text, text_output)

Expand Down
28 changes: 14 additions & 14 deletions src/spdx/writer/tagvalue/snippet_writer.py
Expand Up @@ -12,24 +12,24 @@

from spdx.model.snippet import Snippet
from spdx.writer.tagvalue.tagvalue_writer_helper_functions import write_value, write_text_value, write_range, \
write_license_expression
write_license_info_list


def write_snippet(snippet: Snippet, output_text: TextIO):
output_text.write("## Snippet Information\n")
def write_snippet(snippet: Snippet, text_output: TextIO):
text_output.write("## Snippet Information\n")

write_value("SnippetSPDXID", snippet.spdx_id, output_text)
write_value("SnippetFromFileSPDXID", snippet.file_spdx_id, output_text)
write_range("SnippetByteRange", snippet.byte_range, output_text)
write_range("SnippetLineRange", snippet.line_range, output_text)
write_value("SnippetSPDXID", snippet.spdx_id, text_output)
write_value("SnippetFromFileSPDXID", snippet.file_spdx_id, text_output)
write_range("SnippetByteRange", snippet.byte_range, text_output)
write_range("SnippetLineRange", snippet.line_range, text_output)

write_license_expression("SnippetLicenseConcluded", snippet.license_concluded, output_text)
write_license_expression("LicenseInfoInSnippet", snippet.license_info_in_snippet, output_text)
write_text_value("SnippetLicenseComments", snippet.license_comment, output_text)
write_text_value("SnippetCopyrightText", snippet.copyright_text, output_text)
write_value("SnippetLicenseConcluded", snippet.license_concluded, text_output)
write_license_info_list("LicenseInfoInSnippet", snippet.license_info_in_snippet, text_output)
write_text_value("SnippetLicenseComments", snippet.license_comment, text_output)
write_text_value("SnippetCopyrightText", snippet.copyright_text, text_output)

write_text_value("SnippetComment", snippet.comment, output_text)
write_value("SnippetName", snippet.name, output_text)
write_text_value("SnippetComment", snippet.comment, text_output)
write_value("SnippetName", snippet.name, text_output)

for attribution_text in snippet.attribution_texts:
write_text_value("SnippetAttributionText", attribution_text, output_text)
write_text_value("SnippetAttributionText", attribution_text, text_output)
26 changes: 12 additions & 14 deletions src/spdx/writer/tagvalue/tagvalue_writer_helper_functions.py
Expand Up @@ -12,7 +12,7 @@

from spdx.model.actor import Actor
from spdx.model.file import File
from spdx.model.license_expression import LicenseExpression
from license_expression import LicenseExpression
from spdx.model.package import Package
from spdx.model.relationship import Relationship
from spdx.model.snippet import Snippet
Expand All @@ -24,8 +24,8 @@ def write_separator(out: TextIO):
out.write("\n")


def write_value(tag: str, value: Optional[Union[bool, str, SpdxNone, SpdxNoAssertion]], out: TextIO):
if value:
def write_value(tag: str, value: Optional[Union[bool, str, SpdxNone, SpdxNoAssertion, LicenseExpression]], out: TextIO):
if value is not None:
out.write(f"{tag}: {value}\n")


Expand Down Expand Up @@ -58,24 +58,22 @@ def write_list_of_elements(list_of_elements: List[Any], write_method: Callable[[
write_separator(text_output)


def write_license_info_list(tag: str, license_infos: Union[SpdxNone, SpdxNoAssertion, List[LicenseExpression]], text_output: TextIO):
if isinstance(license_infos, (SpdxNone, SpdxNoAssertion)):
write_value(tag, license_infos, text_output)
return

for license_info in license_infos:
write_value(tag, license_info, text_output)


def write_actor(tag: str, element_to_write: Optional[Union[Actor, SpdxNoAssertion]], text_output: TextIO):
if isinstance(element_to_write, Actor):
write_value(tag, element_to_write.to_serialized_string(), text_output)
else:
write_value(tag, element_to_write, text_output)


def write_license_expression(tag: str, element_to_write: Optional[Union[
List[LicenseExpression], LicenseExpression, SpdxNoAssertion, SpdxNone]], text_output: TextIO):
if isinstance(element_to_write, (SpdxNone, SpdxNoAssertion, str)):
write_value(tag, element_to_write, text_output)
elif isinstance(element_to_write, LicenseExpression):
write_value(tag, element_to_write.expression_string, text_output)
elif isinstance(element_to_write, list):
for element in element_to_write:
write_value(tag, element.expression_string, text_output)


def scan_relationships(relationships: List[Relationship], packages: List[Package], files: List[File]) \
-> Tuple[List, Dict]:
contained_files_by_package_id = dict()
Expand Down