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 456 add rdf parser #469

Merged
merged 43 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
18e7063
delete rdflib as optional dependency as it is required
meretp Feb 1, 2023
cf5ee6a
delete installation from rdflib as the package is a required dependency
meretp Feb 1, 2023
298255e
move helper methods to also use it for the rdf parser
meretp Feb 9, 2023
9729b96
move actor_parser.py as it is format-agnostic and will be used for js…
meretp Feb 1, 2023
96498c0
[issue-456] add creation_info parser
meretp Jan 31, 2023
024fa89
[issue-456] add graph parsing functions
meretp Feb 1, 2023
b1a47f9
[issue-456] add snippet parser
meretp Feb 1, 2023
8466d2e
[issue-456] add checksum parser
meretp Feb 2, 2023
6d1e93e
[issue-456] add external document ref parser
meretp Feb 2, 2023
7087e36
[issue-456] add file parser
meretp Feb 2, 2023
1eb803b
[issue-456] add annotation parser
meretp Feb 2, 2023
6c8fdee
[issue-456] add package parser
meretp Feb 2, 2023
625e2a9
[issue-456] add package verification code parser
meretp Feb 8, 2023
198f2a6
[issue-456] add helper method in casing_tools and move the module as …
meretp Feb 8, 2023
b3e274b
[issue-456] add parser for ExternalPackageRefs
meretp Feb 8, 2023
cea02ae
[issue-456] use enum helper method for primary package purpose
meretp Feb 8, 2023
d0238be
[issue-456] use enum helper method for annotation type
meretp Feb 8, 2023
fb105dc
[issue-456] use helper method for checksum algorithm
meretp Feb 8, 2023
58783f9
[issue-456] add relationship parser
meretp Feb 8, 2023
da33b14
[issue-456] use NamespaceManager to translate external document names…
meretp Feb 9, 2023
69d16aa
[issue-456] add extracted licensing info parser
meretp Feb 9, 2023
6652e4f
[issue-456] allow no assertion in package download location
meretp Feb 8, 2023
ce17ca6
[issue-456, refactor] extract method to get a unique value, sort methods
meretp Feb 9, 2023
9737fde
[issue-456, refactor] replace str_or_no_assertion_or_none with parse …
meretp Feb 9, 2023
9df9980
[issue-456] add license expression parser
meretp Feb 9, 2023
7b61c8f
[issue-456, refactor]
meretp Feb 9, 2023
9ecc373
[issue-456] parse list of license expressions
meretp Feb 9, 2023
1b8cf82
[issue-456] also parse URIRefs with the documents namespace as prefix…
meretp Feb 9, 2023
432a39f
[issue-456] add rdf parser to cli tool
meretp Feb 9, 2023
7a27e88
[issue-456] delete RDFExample from specVersion 2.1 as the tool curren…
meretp Feb 9, 2023
03ee53c
[issue-456, refactor] use generic helper methods instead of individua…
meretp Feb 9, 2023
446757c
[issue-456] move actor_parser.py as it is format-agnostic and used fo…
meretp Feb 9, 2023
86bc68c
[issue-456, refactor] adapt type hints, delete unused imports, rename…
meretp Feb 9, 2023
47e7db0
[issue-456] add helper method to remove prefix to support Python 3.7 …
meretp Feb 9, 2023
9681339
[issue-456] allow multiple members in ConjunctiveLicenseSet and Disju…
meretp Feb 10, 2023
8f9259b
[issue-456] strip parsed values by default to prevent new lines and l…
meretp Feb 10, 2023
9eb1206
[issue-456] fix parsing of externalPackageRef
meretp Feb 10, 2023
4ec5b3c
fix helper method
meretp Feb 15, 2023
c4d04d4
[review] use helper method for enums also for FileType
meretp Feb 20, 2023
c787280
[review, refactor] strip copyright texts, delete trailing comma
meretp Feb 20, 2023
6b70dda
[review] rewrite parse_ranges
meretp Feb 20, 2023
1d728b7
[review, refactor] let Constructor handle None values and use the mor…
meretp Feb 20, 2023
1cbe629
[review] refactor helper methods
meretp Feb 20, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/install_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ jobs:
python -m build -nwx .
python -m pip install --upgrade ./dist/*.whl
python -m pip install pytest
python -m pip install rdflib
shell: bash
- name: Run tests
run: pytest
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ dependencies = ["click", "pyyaml", "xmltodict", "rdflib", "typeguard", "uritools
dynamic = ["version"]

[project.optional-dependencies]
test = ["pytest", "rdflib"]
rdf = ["rdflib"]
test = ["pytest"]

[project.scripts]
pyspdxtools = "spdx.clitools.pyspdxtools:main"
Expand Down
5 changes: 5 additions & 0 deletions src/spdx/writer/casing_tools.py → src/spdx/casing_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@
def snake_case_to_camel_case(snake_case_string: str) -> str:
each_word_capitalized = sub(r"[_\-]+", " ", snake_case_string).title().replace(" ", "")
return each_word_capitalized[0].lower() + each_word_capitalized[1:]


def camel_case_to_snake_case(camel_case_string: str) -> str:
snake_case_string = sub("(?!^)([A-Z]+)", r"_\1", camel_case_string).lower()
return snake_case_string
2 changes: 1 addition & 1 deletion src/spdx/jsonschema/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from spdx.jsonschema.json_property import JsonProperty
from spdx.model.document import Document
from spdx.writer.casing_tools import snake_case_to_camel_case
from spdx.casing_tools import snake_case_to_camel_case

MISSING_IMPLEMENTATION_MESSAGE = "Must be implemented"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from spdx.model.actor import Actor, ActorType
from spdx.parser.error import SPDXParsingError
from spdx.parser.jsonlikedict.dict_parsing_functions import construct_or_raise_parsing_error
from spdx.parser.parsing_functions import construct_or_raise_parsing_error


class ActorParser:
Expand Down
6 changes: 3 additions & 3 deletions src/spdx/parser/jsonlikedict/annotation_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
from spdx.model.actor import Actor
from spdx.model.annotation import Annotation, AnnotationType
from spdx.parser.error import SPDXParsingError
from spdx.parser.jsonlikedict.actor_parser import ActorParser
from spdx.parser.jsonlikedict.dict_parsing_functions import construct_or_raise_parsing_error, \
parse_field_or_log_error, append_parsed_field_or_log_error, raise_parsing_error_if_logger_has_messages
from spdx.parser.actor_parser import ActorParser
from spdx.parser.jsonlikedict.dict_parsing_functions import parse_field_or_log_error, append_parsed_field_or_log_error
from spdx.parser.parsing_functions import construct_or_raise_parsing_error, raise_parsing_error_if_logger_has_messages
from spdx.datetime_conversions import datetime_from_str
from spdx.parser.logger import Logger

Expand Down
4 changes: 2 additions & 2 deletions src/spdx/parser/jsonlikedict/checksum_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from typing import Dict, Optional

from spdx.model.checksum import Checksum, ChecksumAlgorithm
from spdx.parser.jsonlikedict.dict_parsing_functions import raise_parsing_error_if_logger_has_messages, json_str_to_enum_name, \
construct_or_raise_parsing_error
from spdx.parser.jsonlikedict.dict_parsing_functions import json_str_to_enum_name
from spdx.parser.parsing_functions import construct_or_raise_parsing_error, raise_parsing_error_if_logger_has_messages
from spdx.parser.logger import Logger


Expand Down
5 changes: 3 additions & 2 deletions src/spdx/parser/jsonlikedict/creation_info_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
from spdx.model.external_document_ref import ExternalDocumentRef
from spdx.model.version import Version
from spdx.parser.error import SPDXParsingError
from spdx.parser.jsonlikedict.actor_parser import ActorParser
from spdx.parser.actor_parser import ActorParser
from spdx.parser.jsonlikedict.checksum_parser import ChecksumParser
from spdx.parser.jsonlikedict.dict_parsing_functions import append_parsed_field_or_log_error, \
raise_parsing_error_if_logger_has_messages, construct_or_raise_parsing_error, parse_field_or_log_error, \
parse_field_or_log_error, \
parse_field_or_no_assertion
from spdx.parser.parsing_functions import construct_or_raise_parsing_error, raise_parsing_error_if_logger_has_messages
from spdx.datetime_conversions import datetime_from_str
from spdx.parser.logger import Logger

Expand Down
18 changes: 1 addition & 17 deletions src/spdx/parser/jsonlikedict/dict_parsing_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

from spdx.model.spdx_no_assertion import SpdxNoAssertion
from spdx.model.spdx_none import SpdxNone
from common.typing.constructor_type_errors import ConstructorTypeErrors
from spdx.parser.error import SPDXParsingError
from spdx.parser.logger import Logger
from spdx.parser.parsing_functions import raise_parsing_error_if_logger_has_messages


def json_str_to_enum_name(json_str: str) -> str:
Expand All @@ -23,14 +23,6 @@ def json_str_to_enum_name(json_str: str) -> str:
return json_str.replace("-", "_").upper()


def construct_or_raise_parsing_error(object_to_construct: Any, args_for_construction: Dict) -> Any:
try:
constructed_object = object_to_construct(**args_for_construction)
except ConstructorTypeErrors as err:
raise SPDXParsingError([f"Error while constructing {object_to_construct.__name__}: {err.get_messages()}"])
return constructed_object


def parse_field_or_log_error(logger: Logger, field: Any, parsing_method: Callable = lambda x: x, default: Any = None,
field_is_list: bool = False) -> Any:
if not field:
Expand Down Expand Up @@ -59,14 +51,6 @@ def append_parsed_field_or_log_error(logger: Logger, list_to_append_to: List[Any
return list_to_append_to


def raise_parsing_error_if_logger_has_messages(logger: Logger, parsed_object_name: str = None):
if logger.has_messages():
if parsed_object_name:
raise SPDXParsingError([f"Error while parsing {parsed_object_name}: {logger.get_messages()}"])
else:
raise SPDXParsingError(logger.get_messages())


def parse_field_or_no_assertion_or_none(field: Optional[str], method_for_field: Callable = lambda x: x) -> Any:
if field == SpdxNoAssertion().__str__():
return SpdxNoAssertion()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

from spdx.model.extracted_licensing_info import ExtractedLicensingInfo
from spdx.model.spdx_no_assertion import SpdxNoAssertion
from spdx.parser.jsonlikedict.dict_parsing_functions import construct_or_raise_parsing_error, parse_field_or_no_assertion
from spdx.parser.jsonlikedict.dict_parsing_functions import parse_field_or_no_assertion
from spdx.parser.parsing_functions import construct_or_raise_parsing_error
from spdx.parser.logger import Logger


Expand Down
4 changes: 2 additions & 2 deletions src/spdx/parser/jsonlikedict/file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
from spdx.model.spdx_no_assertion import SpdxNoAssertion
from spdx.model.spdx_none import SpdxNone
from spdx.parser.jsonlikedict.checksum_parser import ChecksumParser
from spdx.parser.jsonlikedict.dict_parsing_functions import raise_parsing_error_if_logger_has_messages, \
construct_or_raise_parsing_error, parse_field_or_log_error, \
from spdx.parser.jsonlikedict.dict_parsing_functions import parse_field_or_log_error, \
parse_field_or_no_assertion_or_none
from spdx.parser.parsing_functions import construct_or_raise_parsing_error, raise_parsing_error_if_logger_has_messages
from spdx.parser.jsonlikedict.license_expression_parser import LicenseExpressionParser
from spdx.parser.logger import Logger

Expand Down
4 changes: 2 additions & 2 deletions src/spdx/parser/jsonlikedict/json_like_dict_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
from spdx.parser.error import SPDXParsingError
from spdx.parser.jsonlikedict.annotation_parser import AnnotationParser
from spdx.parser.jsonlikedict.creation_info_parser import CreationInfoParser
from spdx.parser.jsonlikedict.dict_parsing_functions import raise_parsing_error_if_logger_has_messages, \
construct_or_raise_parsing_error, parse_list_of_elements
from spdx.parser.jsonlikedict.dict_parsing_functions import parse_list_of_elements
from spdx.parser.parsing_functions import construct_or_raise_parsing_error, raise_parsing_error_if_logger_has_messages
from spdx.parser.jsonlikedict.extracted_licensing_info_parser import ExtractedLicensingInfoParser
from spdx.parser.jsonlikedict.file_parser import FileParser
from spdx.parser.logger import Logger
Expand Down
4 changes: 2 additions & 2 deletions src/spdx/parser/jsonlikedict/license_expression_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
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 append_parsed_field_or_log_error, \
raise_parsing_error_if_logger_has_messages
from spdx.parser.jsonlikedict.dict_parsing_functions import append_parsed_field_or_log_error
from spdx.parser.parsing_functions import raise_parsing_error_if_logger_has_messages
from spdx.parser.logger import Logger


Expand Down
6 changes: 3 additions & 3 deletions src/spdx/parser/jsonlikedict/package_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
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.actor_parser import ActorParser
from spdx.parser.actor_parser import ActorParser
from spdx.parser.jsonlikedict.checksum_parser import ChecksumParser
from spdx.parser.jsonlikedict.dict_parsing_functions import append_parsed_field_or_log_error, \
raise_parsing_error_if_logger_has_messages, json_str_to_enum_name, construct_or_raise_parsing_error, \
parse_field_or_log_error, parse_field_or_no_assertion_or_none, parse_field_or_no_assertion
json_str_to_enum_name, parse_field_or_log_error, parse_field_or_no_assertion_or_none, parse_field_or_no_assertion
from spdx.parser.parsing_functions import construct_or_raise_parsing_error, raise_parsing_error_if_logger_has_messages
from spdx.datetime_conversions import datetime_from_str
from spdx.parser.jsonlikedict.license_expression_parser import LicenseExpressionParser
from spdx.parser.logger import Logger
Expand Down
5 changes: 2 additions & 3 deletions src/spdx/parser/jsonlikedict/relationship_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
from spdx.model.relationship import Relationship, RelationshipType
from common.typing.constructor_type_errors import ConstructorTypeErrors
from spdx.parser.error import SPDXParsingError
from spdx.parser.jsonlikedict.dict_parsing_functions import raise_parsing_error_if_logger_has_messages, \
json_str_to_enum_name, \
construct_or_raise_parsing_error, \
from spdx.parser.jsonlikedict.dict_parsing_functions import json_str_to_enum_name, \
parse_field_or_log_error, parse_field_or_no_assertion_or_none, delete_duplicates_from_list
from spdx.parser.parsing_functions import construct_or_raise_parsing_error, raise_parsing_error_if_logger_has_messages
from spdx.parser.logger import Logger


Expand Down
3 changes: 2 additions & 1 deletion src/spdx/parser/jsonlikedict/snippet_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
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, parse_field_or_log_error, \
from spdx.parser.jsonlikedict.dict_parsing_functions import parse_field_or_log_error, \
parse_field_or_no_assertion_or_none
from spdx.parser.parsing_functions import construct_or_raise_parsing_error

from spdx.parser.jsonlikedict.license_expression_parser import LicenseExpressionParser
from spdx.parser.logger import Logger
Expand Down
3 changes: 2 additions & 1 deletion src/spdx/parser/parse_anything.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
# limitations under the License.
from spdx.formats import file_name_to_format, FileFormat
from spdx.parser.json import json_parser
from spdx.parser.rdf import rdf_parser
from spdx.parser.xml import xml_parser
from spdx.parser.yaml import yaml_parser


def parse_file(file_name: str):
input_format = file_name_to_format(file_name)
if input_format == FileFormat.RDF_XML:
raise NotImplementedError("Currently, the rdf parser is not implemented")
return rdf_parser.parse_from_file(file_name)
elif input_format == FileFormat.TAG_VALUE:
raise NotImplementedError("Currently, the tag-value parser is not implemented")
elif input_format == FileFormat.JSON:
Expand Down
31 changes: 31 additions & 0 deletions src/spdx/parser/parsing_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (c) 2023 spdx contributors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any, Dict

from common.typing.constructor_type_errors import ConstructorTypeErrors
from spdx.parser.error import SPDXParsingError
from spdx.parser.logger import Logger


def construct_or_raise_parsing_error(object_to_construct: Any, args_for_construction: Dict) -> Any:
try:
constructed_object = object_to_construct(**args_for_construction)
except ConstructorTypeErrors as err:
raise SPDXParsingError([f"Error while constructing {object_to_construct.__name__}: {err.get_messages()}"])
return constructed_object


def raise_parsing_error_if_logger_has_messages(logger: Logger, parsed_object_name: str = None):
if logger.has_messages():
if parsed_object_name:
raise SPDXParsingError([f"Error while parsing {parsed_object_name}: {logger.get_messages()}"])
else:
raise SPDXParsingError(logger.get_messages())
Empty file added src/spdx/parser/rdf/__init__.py
Empty file.
39 changes: 39 additions & 0 deletions src/spdx/parser/rdf/annotation_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (c) 2023 spdx contributors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from rdflib import URIRef, Graph, RDFS

from spdx.datetime_conversions import datetime_from_str
from spdx.model.annotation import Annotation, AnnotationType
from spdx.parser.actor_parser import ActorParser
from spdx.parser.logger import Logger
from spdx.parser.parsing_functions import raise_parsing_error_if_logger_has_messages, construct_or_raise_parsing_error
from spdx.parser.rdf.graph_parsing_functions import parse_literal, parse_spdx_id, parse_enum_value
from spdx.rdfschema.namespace import SPDX_NAMESPACE


def parse_annotation(annotation_node: URIRef, graph: Graph, parent_node: URIRef, doc_namespace: str) -> Annotation:
logger = Logger()
spdx_id = parse_spdx_id(parent_node, doc_namespace, graph)
annotator = parse_literal(logger, graph, annotation_node, SPDX_NAMESPACE.annotator,
parsing_method=ActorParser.parse_actor)
annotation_type = parse_literal(logger, graph, annotation_node, SPDX_NAMESPACE.annotationType,
parsing_method=lambda x: parse_enum_value(x, AnnotationType,
SPDX_NAMESPACE.annotationType_))
annotation_date = parse_literal(logger, graph, annotation_node, SPDX_NAMESPACE.annotationDate,
parsing_method=datetime_from_str)
annotation_comment = parse_literal(logger, graph, annotation_node, RDFS.comment)

raise_parsing_error_if_logger_has_messages(logger, "Annotation")
annotation = construct_or_raise_parsing_error(Annotation, dict(spdx_id=spdx_id, annotation_type=annotation_type,
annotator=annotator, annotation_date=annotation_date,
annotation_comment=annotation_comment))

return annotation
40 changes: 40 additions & 0 deletions src/spdx/parser/rdf/checksum_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) 2023 spdx contributors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from rdflib import Graph, URIRef
from spdx.parser.error import SPDXParsingError

from spdx.model.checksum import Checksum, ChecksumAlgorithm
from spdx.parser.logger import Logger
from spdx.parser.parsing_functions import construct_or_raise_parsing_error, raise_parsing_error_if_logger_has_messages
from spdx.parser.rdf.graph_parsing_functions import parse_literal, remove_prefix
from spdx.rdfschema.namespace import SPDX_NAMESPACE


def parse_checksum(parent_node: URIRef, graph: Graph) -> Checksum:
logger = Logger()
algorithm = parse_literal(logger, graph, parent_node, SPDX_NAMESPACE.algorithm,
parsing_method=convert_rdf_to_algorithm)
value = parse_literal(logger, graph, parent_node, SPDX_NAMESPACE.checksumValue)

raise_parsing_error_if_logger_has_messages(logger, "Checksum")
checksum = construct_or_raise_parsing_error(Checksum, dict(algorithm=algorithm, value=value))
return checksum


def convert_rdf_to_algorithm(algorithm: str) -> ChecksumAlgorithm:
algorithm = remove_prefix(algorithm, SPDX_NAMESPACE.checksumAlgorithm_).upper()
if "BLAKE2B" in algorithm:
algorithm = algorithm.replace("BLAKE2B", "BLAKE2B_")
try:
checksum = ChecksumAlgorithm[algorithm]
except KeyError:
raise SPDXParsingError([f"Invalid value for ChecksumAlgorithm: {algorithm}"])
return checksum
Loading