Skip to content

Commit

Permalink
[issue-456] add helper method to remove prefix to support Python 3.7 …
Browse files Browse the repository at this point in the history
…and Python 3.8

Signed-off-by: Meret Behrens <meret.behrens@tngtech.com>
  • Loading branch information
meretp committed Feb 9, 2023
1 parent dba5086 commit ef61acf
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/spdx/parser/rdf/checksum_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
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
from spdx.parser.rdf.graph_parsing_functions import parse_literal, remove_prefix
from spdx.rdfschema.namespace import SPDX_NAMESPACE


Expand All @@ -29,7 +29,7 @@ def parse_checksum(parent_node: URIRef, graph: Graph) -> Checksum:


def convert_rdf_to_algorithm(algorithm: str) -> ChecksumAlgorithm:
algorithm = algorithm.removeprefix(SPDX_NAMESPACE.checksumAlgorithm_).upper()
algorithm = remove_prefix(algorithm, SPDX_NAMESPACE.checksumAlgorithm_).upper()
if "BLAKE2B" in algorithm:
algorithm = algorithm.replace("BLAKE2B", "BLAKE2B_")

Expand Down
4 changes: 2 additions & 2 deletions src/spdx/parser/rdf/creation_info_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
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.checksum_parser import parse_checksum
from spdx.parser.rdf.graph_parsing_functions import parse_literal, parse_spdx_id
from spdx.parser.rdf.graph_parsing_functions import parse_literal, parse_spdx_id, remove_prefix
from spdx.rdfschema.namespace import SPDX_NAMESPACE, LICENSE_NAMESPACE

from spdx.datetime_conversions import datetime_from_str
Expand All @@ -34,7 +34,7 @@ def parse_creation_info(graph: Graph) -> Tuple[CreationInfo, URIRef]:
namespace, spdx_id, doc_node = parse_namespace_and_spdx_id(graph)
spec_version = parse_literal(logger, graph, doc_node, SPDX_NAMESPACE.specVersion)
data_license = parse_literal(logger, graph, doc_node, SPDX_NAMESPACE.dataLicense,
parsing_method=lambda x: x.removeprefix(LICENSE_NAMESPACE))
parsing_method=lambda x: remove_prefix(x, LICENSE_NAMESPACE))
comment = parse_literal(logger, graph, doc_node, RDFS.comment)
name = parse_literal(logger, graph, doc_node, SPDX_NAMESPACE.name)

Expand Down
4 changes: 2 additions & 2 deletions src/spdx/parser/rdf/file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from spdx.parser.parsing_functions import construct_or_raise_parsing_error, raise_parsing_error_if_logger_has_messages
from spdx.parser.rdf.checksum_parser import parse_checksum
from spdx.parser.rdf.graph_parsing_functions import parse_literal, parse_spdx_id, parse_literal_or_no_assertion_or_none, \
get_correct_typed_value
get_correct_typed_value, remove_prefix
from spdx.parser.rdf.license_expression_parser import parse_license_expression
from spdx.rdfschema.namespace import SPDX_NAMESPACE

Expand Down Expand Up @@ -67,5 +67,5 @@ def parse_file(file_node: URIRef, graph: Graph, doc_namespace: str) -> File:


def convert_uri_ref_to_file_type(file_type_ref: URIRef) -> FileType:
file_type = file_type_ref.removeprefix(SPDX_NAMESPACE).replace("fileType_", "").upper()
file_type = remove_prefix(file_type_ref, SPDX_NAMESPACE.fileType_).upper()
return FileType[file_type]
11 changes: 10 additions & 1 deletion src/spdx/parser/rdf/graph_parsing_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def apply_parsing_method_or_log_error(logger: Logger, value: Any, parsing_method
logger.extend(err.args[0])
return default


def parse_literal_or_no_assertion_or_none(logger: Logger, graph: Graph, subject: Node, predicate: Node,
parsing_method: Callable = str, default: Any = None):
value = get_unique_value(logger, graph, subject, predicate, default)
Expand Down Expand Up @@ -84,7 +85,7 @@ def get_unique_value(logger: Logger, graph: Graph, subject: Node, predicate: Nod

def parse_enum_value(enum_str: str, enum_class: Type[Enum], prefix: str) -> Enum:
try:
enum_without_rdf_prefix = enum_str.removeprefix(prefix)
enum_without_rdf_prefix = remove_prefix(enum_str, prefix)
value = camel_case_to_snake_case(enum_without_rdf_prefix).upper()
return enum_class[value]
except KeyError:
Expand All @@ -100,3 +101,11 @@ def parse_spdx_id(resource: URIRef, doc_namespace: str, graph: Graph) -> Optiona
namespace_manager = NamespaceManager(graph)
return namespace_manager.normalizeUri(resource)
return resource.toPython() or None


# Python 3.9 introduced the method removeprefix() for strings, but as we are also supporting Python 3.7 and 3.8 we need
# to write our own helper method to delete prefixes.
def remove_prefix(string: str, prefix: str) -> str:
if string.startswith(prefix):
return string[len(prefix):]
return string
8 changes: 5 additions & 3 deletions src/spdx/parser/rdf/license_expression_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
from license_expression import LicenseExpression, get_spdx_licensing
from rdflib.term import Identifier, URIRef, BNode, Node
from spdx.parser.error import SPDXParsingError
from spdx.parser.rdf.graph_parsing_functions import remove_prefix

from spdx.rdfschema.namespace import SPDX_NAMESPACE, LICENSE_NAMESPACE


def parse_license_expression(license_expression_node: Union[URIRef, BNode, Node], graph: Graph, doc_namespace: str) -> LicenseExpression:
def parse_license_expression(license_expression_node: Union[URIRef, BNode, Node], graph: Graph,
doc_namespace: str) -> LicenseExpression:
spdx_licensing = get_spdx_licensing()
expression = ""
if license_expression_node.startswith(LICENSE_NAMESPACE):
expression = license_expression_node.removeprefix(LICENSE_NAMESPACE)
expression = remove_prefix(license_expression_node, LICENSE_NAMESPACE)
return spdx_licensing.parse(expression)
if license_expression_node.startswith(doc_namespace):
expression = license_expression_node.fragment
Expand Down Expand Up @@ -60,7 +62,7 @@ def parse_license_expression(license_expression_node: Union[URIRef, BNode, Node]

def parse_license_exception(exception_node: Identifier, graph: Graph) -> str:
if exception_node.startswith(LICENSE_NAMESPACE):
exception = exception_node.removeprefix(LICENSE_NAMESPACE)
exception = remove_prefix(exception_node, LICENSE_NAMESPACE)
else:
exception = graph.value(exception_node, SPDX_NAMESPACE.licenseExceptionId).toPython()
return exception
4 changes: 2 additions & 2 deletions src/spdx/parser/rdf/package_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from spdx.parser.parsing_functions import raise_parsing_error_if_logger_has_messages, construct_or_raise_parsing_error
from spdx.parser.rdf.checksum_parser import parse_checksum
from spdx.parser.rdf.graph_parsing_functions import parse_spdx_id, parse_literal, parse_enum_value, \
parse_literal_or_no_assertion_or_none, get_correct_typed_value, parse_literal_or_no_assertion
parse_literal_or_no_assertion_or_none, get_correct_typed_value, parse_literal_or_no_assertion, remove_prefix
from spdx.parser.rdf.license_expression_parser import parse_license_expression
from spdx.rdfschema.namespace import SPDX_NAMESPACE, REFERENCE_NAMESPACE

Expand Down Expand Up @@ -128,7 +128,7 @@ def parse_external_package_ref(external_package_ref_node: URIRef, graph: Graph)
logger, graph, external_package_ref_node, SPDX_NAMESPACE.referenceCategory,
parsing_method=lambda x: parse_enum_value(x, ExternalPackageRefCategory, SPDX_NAMESPACE.referenceCategory_, ))
ref_type = parse_literal(logger, graph, external_package_ref_node, SPDX_NAMESPACE.referenceType,
parsing_method=lambda x: x.removeprefix(REFERENCE_NAMESPACE))
parsing_method=lambda x: remove_prefix(x, REFERENCE_NAMESPACE))
comment = parse_literal(logger, graph, external_package_ref_node, RDFS.comment)

raise_parsing_error_if_logger_has_messages(logger)
Expand Down
10 changes: 9 additions & 1 deletion tests/spdx/parser/rdf/test_graph_parsing_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import pytest
from rdflib import URIRef, Graph, Namespace

from spdx.parser.rdf.graph_parsing_functions import parse_spdx_id
from spdx.parser.rdf.graph_parsing_functions import parse_spdx_id, remove_prefix


@pytest.mark.parametrize("resource,doc_namespace,ext_namespace_mapping,expected",
Expand All @@ -27,3 +27,11 @@ def test_parse_spdx_id(resource, doc_namespace, ext_namespace_mapping, expected)
spdx_id = parse_spdx_id(resource, doc_namespace, graph)

assert spdx_id == expected


@pytest.mark.parametrize("string,prefix,expected", [("prefixString", "prefix", "String"),
("prefixString", "refix", "prefixString")])
def test_remove_prefix(string, prefix, expected):
shorten_string = remove_prefix(string, prefix)

assert expected == shorten_string

0 comments on commit ef61acf

Please sign in to comment.