8000 Issue 456 add rdf parser by meretp · Pull Request #469 · spdx/tools-python · GitHub
[go: up one dir, main page]

Skip to content

Issue 456 add rdf parser #469

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

Merged
merged 43 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[issue-456] add annotation parser
Signed-off-by: Meret Behrens <meret.behrens@tngtech.com>
  • Loading branch information
meretp committed Feb 22, 2023
commit 1eb803bf57e982a2d27c203bf2143a68f4e0cadf
43 changes: 43 additions & 0 deletions src/spdx/parser/rdf/annotation_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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.jsonlikedict.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
from spdx.rdfschema.namespace import SPDX_NAMESPACE


def parse_annotation(annotation_node: URIRef, graph: Graph, parent_ref: URIRef, doc_namespace) -> Annotation:
logger = Logger()
spdx_id = parse_spdx_id(parent_ref, doc_namespace)
annotator = parse_literal(logger, graph, annotation_node, SPDX_NAMESPACE.annotator,
method_to_apply=ActorParser.parse_actor)
annotation_type = graph.value(annotation_node, SPDX_NAMESPACE.annotationType)
if annotation_type:
annotation_type = annotation_type.removeprefix(SPDX_NAMESPACE).replace("annotationType_", "").upper()
try:
annotation_type = AnnotationType[annotation_type]
except KeyError:
logger.append(f"Invalid AnnotationType: {annotation_type}")
annotation_date = parse_literal(logger, graph, annotation_node, SPDX_NAMESPACE.annotationDate,
method_to_apply=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
8 changes: 7 additions & 1 deletion src/spdx/parser/rdf/rdf_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from spdx.parser.error import SPDXParsingError
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.annotation_parser import parse_annotation
from spdx.parser.rdf.creation_info_parser import parse_creation_info
from spdx.parser.rdf.file_parser import parse_file
from spdx.parser.rdf.snippet_parser import parse_snippet
Expand Down Expand Up @@ -51,7 +52,12 @@ def translate_graph_to_document(graph: Graph) -> Document:
except SPDXParsingError as err:
logger.extend(err.get_messages())

annotations = []
for (parent_node, _, annotation_node) in graph.triples((None, SPDX_NAMESPACE.annotation, None)):
annotations.append(parse_annotation(annotation_node, graph, parent_node, creation_info.document_namespace))

raise_parsing_error_if_logger_has_messages(logger)
document = construct_or_raise_parsing_error(Document,
dict(creation_info=creation_info, snippets=snippets, files=files))
dict(creation_info=creation_info, snippets=snippets, files=files,
annotations=annotations))
return document
34 changes: 34 additions & 0 deletions tests/spdx/parser/rdf/test_annotation_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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.
import os
from datetime import datetime

from rdflib import Graph, URIRef

from spdx.model.actor import Actor, ActorType
from spdx.model.annotation import AnnotationType
from spdx.parser.rdf.annotation_parser import parse_annotation
from spdx.rdfschema.namespace import SPDX_NAMESPACE


def test_parse_annotation():
graph = Graph().parse(os.path.join(os.path.dirname(__file__), "data/file_to_test_rdf_parser.rdf.xml"))
doc_namespace = "https://some.namespace"
file_node = URIRef(f"{doc_namespace}#SPDXRef-File")
annotation_node = graph.value(subject=file_node, predicate=SPDX_NAMESPACE.annotation)

annotation = parse_annotation(annotation_node, graph, file_node, doc_namespace)

assert annotation.spdx_id == "SPDXRef-File"
assert annotation.annotation_type == AnnotationType.REVIEW
assert annotation.annotator == Actor(ActorType.PERSON, "annotatorName", "some@mail.com")
assert annotation.annotation_date == datetime(2022, 12, 1, 0, 0)
assert annotation.annotation_comment == "annotationComment"
1 change: 1 addition & 0 deletions tests/spdx/parser/rdf/test_rdf_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ def test_rdf_parser_with_2_3_example():
assert type(doc) == Document
assert len(doc.snippets) == 1
assert len(doc.files) == 5
assert len(doc.annotations) == 5
0