Skip to content

Use import.resources instead of __file__ #851

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 8 additions & 4 deletions src/spdx_tools/spdx3/writer/json_ld/json_ld_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@
#
# SPDX-License-Identifier: Apache-2.0
import json
import os
from importlib import resources

from spdx_tools.spdx3.payload import Payload
from spdx_tools.spdx3.writer.json_ld.json_ld_converter import convert_payload_to_json_ld_list_of_elements
from spdx_tools.spdx3.writer.json_ld.json_ld_converter import (
convert_payload_to_json_ld_list_of_elements,
)


def write_payload(payload: Payload, file_name: str):
element_list = convert_payload_to_json_ld_list_of_elements(payload)

# this will be obsolete as soon as the context is publicly available under some URI
with open(os.path.join(os.path.dirname(__file__), "context.json"), "r") as infile:
# Note: 3.0.1 context is now available at
# https://spdx.org/rdf/3.0.1/spdx-context.jsonld
with resources.files("spdx_tools.spdx3.writer.json_ld").joinpath("context.json").open("r") as infile:
context = json.load(infile)

complete_dict = {"@context": context, "@graph": element_list}

with open(file_name + ".jsonld", "w") as out:
with open(file_name + ".jsonld", "w", encoding="utf-8") as out:
json.dump(complete_dict, out, indent=2)
9 changes: 6 additions & 3 deletions src/spdx_tools/spdx3/writer/json_ld/owl_to_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: Apache-2.0
import json
import os.path
from importlib import resources

# current workflow: markdown files + spec_parser -> model.ttl -> convert to json_ld: SPDX_OWL.json ->
# use the function below to generate context.json
Expand Down Expand Up @@ -108,8 +108,11 @@ def convert_spdx_owl_to_jsonld_context(spdx_owl: str = "SPDX_OWL.json"):
else:
print(f"unknown node_type: {node_type}")

with open(os.path.join(os.path.dirname(__file__), "context.json"), "w") as infile:
json.dump(context_dict, infile)
with resources.as_file(
resources.files("spdx_tools.spdx3.writer.json_ld").joinpath("context.json")
) as context_path:
with open(context_path, "w", encoding="utf-8") as outfile:
json.dump(context_dict, outfile)


if __name__ == "__main__":
Expand Down
21 changes: 11 additions & 10 deletions tests/spdx/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-FileCopyrightText: 2023 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0
import os
from importlib import resources

import pytest
from click.testing import CliRunner
Expand All @@ -12,16 +12,16 @@
@pytest.mark.parametrize(
"options",
[
("--infile", os.path.join(os.path.dirname(__file__), "data/SPDXJSONExample-v2.3.spdx.json")),
("-i", os.path.join(os.path.dirname(__file__), "data/SPDXJSONExample-v2.3.spdx.json"), "--novalidation"),
("--infile", str(resources.files("tests.spdx.data").joinpath("SPDXJSONExample-v2.3.spdx.json"))),
("-i", str(resources.files("tests.spdx.data").joinpath("SPDXJSONExample-v2.3.spdx.json")), "--novalidation"),
(
"-i",
os.path.join(os.path.dirname(__file__), "data/SPDXJSONExample-v2.3.spdx.json"),
str(resources.files("tests.spdx.data").joinpath("SPDXJSONExample-v2.3.spdx.json")),
"--novalidation",
"--version",
"SPDX-2.3",
),
("-i", os.path.join(os.path.dirname(__file__), "data/SPDXJSONExample-v2.3.spdx.json"), "-o", "-"),
("-i", str(resources.files("tests.spdx.data").joinpath("SPDXJSONExample-v2.3.spdx.json")), "-o", "-"),
],
)
def test_cli_with_system_exit_code_0(options):
Expand All @@ -37,9 +37,10 @@ def test_cli_with_system_exit_code_0(options):
[
(
"-i",
os.path.join(
os.path.dirname(__file__),
"data/invalid/spdx-trivy-vmware_log-intelligence-fluentd-sha256_086af034f561f343f633be9d9f9e95f65ae6c61b8ddb2c6755ef5bb25b40f53a.json", # noqa: E501
str(
resources.files("tests.spdx.data.invalid").joinpath(
"spdx-trivy-vmware_log-intelligence-fluentd-sha256_086af034f561f343f633be9d9f9e95f65ae6c61b8ddb2c6755ef5bb25b40f53a.json"
)
),
),
("-i", "non_existent_file.spdx"),
Expand All @@ -57,8 +58,8 @@ def test_cli_with_system_exit_code_1(options):
"options",
[
(),
("-i", os.path.join(os.path.dirname(__file__), "data/SPDXJSONExample-v2.3.spdx.json"), "--version"),
("-i", os.path.join(os.path.dirname(__file__), "data/SPDXJSONExample-v2.3.spdx.json"), "-o"),
("-i", str(resources.files("tests.spdx.data").joinpath("SPDXJSONExample-v2.3.spdx.json")), "--version"),
("-i", str(resources.files("tests.spdx.data").joinpath("SPDXJSONExample-v2.3.spdx.json")), "-o"),
],
)
def test_cli_with_system_exit_code_2(options):
Expand Down
5 changes: 3 additions & 2 deletions tests/spdx3/writer/json_ld/test_json_ld_writer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-FileCopyrightText: 2023 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0
import os
from importlib import resources

from spdx_tools.spdx3.bump_from_spdx2.spdx_document import bump_spdx_document
from spdx_tools.spdx3.payload import Payload
Expand All @@ -15,4 +15,5 @@ def test_json_writer():
payload: Payload = bump_spdx_document(spdx2_document)

# this currently generates an actual file to look at, this should be changed to a temp file later
write_payload(payload, os.path.join(os.path.dirname(__file__), "../../../SPDX3_jsonld_test"))
with resources.as_file(resources.files("tests.spdx3.writer.json_ld").joinpath("SPDX3_jsonld_test")) as output_file:
write_payload(payload, str(output_file))