Skip to content

Commit

Permalink
Merge branch 'main' into wip/scenario-tanner
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum authored Oct 5, 2023
2 parents b1f5020 + 822c5ab commit bd35b39
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,5 @@ cython_debug/
.vscode

.DS_Store

project_data
18 changes: 18 additions & 0 deletions dsp_permissions_scripts/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
)
from dsp_permissions_scripts.utils.doap_set import apply_updated_doaps_on_server
from dsp_permissions_scripts.utils.oap import apply_updated_oaps_on_server
from dsp_permissions_scripts.utils.oap_serialize import (
deserialize_resource_oaps,
serialize_resource_oaps,
)
from dsp_permissions_scripts.utils.project import get_all_resource_oaps_of_project


Expand Down Expand Up @@ -41,7 +45,21 @@ def update_oaps(
host=host,
token=token,
)
serialize_resource_oaps(
resource_oaps=resource_oaps,
shortcode=shortcode,
mode="original",
)
resource_oaps_updated = modify_oaps(oaps=resource_oaps)
serialize_resource_oaps(
resource_oaps=resource_oaps,
shortcode=shortcode,
mode="modified",
)
resource_oaps_updated = deserialize_resource_oaps(
shortcode=shortcode,
mode="modified",
)
apply_updated_oaps_on_server(
resource_oaps=resource_oaps_updated,
host=host,
Expand Down
39 changes: 39 additions & 0 deletions dsp_permissions_scripts/utils/oap_serialize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import re
from pathlib import Path
from typing import Literal

from dsp_permissions_scripts.models.oap import Oap


def _get_project_data_path(
shortcode: str,
mode: Literal["original", "modified"]
) -> Path:
return Path(f"project_data/{shortcode}/OAPs_{mode}")


def serialize_resource_oaps(
resource_oaps: list[Oap],
shortcode: str,
mode: Literal["original", "modified"],
) -> None:
"""Serialize the resource OAPs to JSON files."""
folder = _get_project_data_path(shortcode, mode)
folder.mkdir(parents=True, exist_ok=True)
for res_oap in resource_oaps:
filename = re.sub(r"http://rdfh\.ch/[^/]+/", "resource_", res_oap.object_iri)
with open(folder / f"{filename}.json", mode="w", encoding="utf-8") as f:
f.write(res_oap.model_dump_json(indent=2))


def deserialize_resource_oaps(
shortcode: str,
mode: Literal["original", "modified"],
) -> list[Oap]:
"""Deserialize the resource OAPs from JSON files."""
folder = _get_project_data_path(shortcode, mode)
resource_oaps = []
for file in folder.iterdir():
with open(file, mode="r", encoding="utf-8") as f:
resource_oaps.append(Oap.model_validate_json(f.read()))
return resource_oaps

0 comments on commit bd35b39

Please sign in to comment.