Skip to content

Commit

Permalink
black reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
Volodymyr Savchenko authored and Volodymyr Savchenko committed May 23, 2024
1 parent 21666fb commit 53efeee
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 27 deletions.
16 changes: 10 additions & 6 deletions tests/test_absolutize.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,28 @@ def get_test_data_file(filename):
return os.path.join(tests_dir, "test_data", filename)


class TestAbsolutizePaths: #(unittest.TestCase):
class TestAbsolutizePaths: # (unittest.TestCase):
# NOTE: ids can not be found, like 634, or forbidden, like 678
@pytest.mark.parametrize("workflow_id", [41, 552])
def test_make_paths_absolute(self, workflow_id):

with patch_rdflib_urlopen(get_test_data_file):
with open(get_test_data_file(f"{workflow_id}_ro-crate-metadata.json"), "r") as f:
with open(
get_test_data_file(f"{workflow_id}_ro-crate-metadata.json"), "r"
) as f:
json_data = json.load(f)

assert not is_all_absolute(rdflib.Graph().parse(data=json.dumps(json_data), format="json-ld"))
assert not is_all_absolute(
rdflib.Graph().parse(data=json.dumps(json_data), format="json-ld")
)

json_data_abs_paths = make_paths_absolute(json_data, BASE_URL, 41)

G = rdflib.Graph().parse(data=json.dumps(json_data_abs_paths), format="json-ld")
G = rdflib.Graph().parse(
data=json.dumps(json_data_abs_paths), format="json-ld"
)

assert is_all_absolute(G)


def test_merged(self):
G = merge_all_files("data/*21*.json")
assert is_all_absolute(G)
2 changes: 1 addition & 1 deletion tests/test_source_crates.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ def test_process_workflow_ids(

with open(expected_file_path, "rb") as f:
content = f.read()
assert content == b'{"name": "test"}'
assert content == b'{"name": "test"}'
6 changes: 4 additions & 2 deletions workflowhub_graph/absolutize.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ def is_all_absolute(G: rdflib.Graph) -> bool:

# we accept file:// with a netloc, even if netloc is not a FQDN,
# see https://github.com/workflowhub-eu/workflowhub-graph/issues/1#issuecomment-2127351752
if netloc == '':
print(f"found non-absolute path <{item}> {netloc}, {urlparse(item)}")
if netloc == "":
print(
f"found non-absolute path <{item}> {netloc}, {urlparse(item)}"
)
return False
else:
print("this path is absolute", item, urlparse(item))
Expand Down
19 changes: 8 additions & 11 deletions workflowhub_graph/cachedurlopen.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@


from unittest.mock import patch, MagicMock
from contextlib import contextmanager
import io


cached_urls = {
"https://w3id.org/ro/crate/1.0/context": "ro-crate-context-1.0.json",
"https://w3id.org/ro/crate/1.1/context": "ro-crate-context-1.1.json",
}
"https://w3id.org/ro/crate/1.0/context": "ro-crate-context-1.0.json",
"https://w3id.org/ro/crate/1.1/context": "ro-crate-context-1.1.json",
}


@contextmanager
def patch_rdflib_urlopen(file_locator):
def patch_rdflib_urlopen(file_locator):
def cached_urlopen(request):
url = request.get_full_url()

if url not in cached_urls:
# TODO: store and use cache
raise ValueError(f"URL {url} not in cache, have: {cached_urls.keys()}")
Expand All @@ -26,14 +24,13 @@ class Response(io.StringIO):

def info(self):
return self.headers

def geturl(self):
return url

content = open(file_locator(cached_urls[url]), "rt").read()

return Response(content)

with patch("rdflib.parser.urlopen", cached_urlopen):
yield

1 change: 0 additions & 1 deletion workflowhub_graph/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@
ZIP_ENDPOINT = "/workflows/{w_id}/ro_crate?version={w_version}"

TARGET_FILE_NAME = "ro-crate-metadata.json"

15 changes: 9 additions & 6 deletions workflowhub_graph/merge.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import argparse
import glob
import json
Expand Down Expand Up @@ -29,17 +28,21 @@ def merge_all_files(pattern="data/*.json") -> rdflib.Graph:
json_data = make_paths_absolute(json_data, BASE_URL, w_id)

# TODO: make this actual caching, and pre-populate in the test
with patch_rdflib_urlopen(lambda x: "tests/test_data/ro-crate-context-1.0.json"):
with patch_rdflib_urlopen(
lambda x: "tests/test_data/ro-crate-context-1.0.json"
):
G.parse(data=json_data, format="json-ld")

# TODO: set a total version
return G
return G


if __name__ == "__main__":
argparser = argparse.ArgumentParser()
argparser.add_argument("output_filename", help="The output filename.", default="merged.ttl")
argparser.add_argument(
"output_filename", help="The output filename.", default="merged.ttl"
)
args = argparser.parse_args()

G = merge_all_files()
G.serialize(args.output_filename, format="ttl")
G.serialize(args.output_filename, format="ttl")

0 comments on commit 53efeee

Please sign in to comment.