Skip to content

Commit

Permalink
edit
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Oct 5, 2023
1 parent b88a5ae commit 5cf2fba
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
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
33 changes: 33 additions & 0 deletions dsp_permissions_scripts/utils/oap_serialize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import re
from pathlib import Path
from typing import Literal

from dsp_permissions_scripts.models.oap import Oap


def serialize_resource_oaps(
resource_oaps: list[Oap],
shortcode: str,
mode=Literal["original", "modified"],
) -> None:
"""Serialize the resource OAPs to JSON files into the folder "OAPs_project_{shortcode}_{mode}"."""
folder = Path(f"OAPs_project_{shortcode}_{mode}")
folder.mkdir(exist_ok=True)
for res_oap in resource_oaps:
filename = re.sub(r"", "_", res_oap.object_iri)
with open((folder / filename).with_suffix(".json"), mode="w", encoding="utf-8") as f:
f.write(res_oap.model_dump_json())


def deserialize_resource_oaps(
shortcode: str,
mode: Literal["original", "modified"],
) -> list[Oap]:
"""Deserialize the resource OAPs from JSON files in the folder "OAPs_project_{shortcode}_{mode}"."""
folder = Path(f"OAPs_project_{shortcode}_{mode}")
resource_oaps = []
for file in folder.iterdir():
if file.suffix == ".json" and "rdfh" in file.name:
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 5cf2fba

Please sign in to comment.