-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into wip/scenario-tanner
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,3 +162,5 @@ cython_debug/ | |
.vscode | ||
|
||
.DS_Store | ||
|
||
project_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |