Skip to content

Commit

Permalink
improve naming+docstrings, consider DoapTargetType
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Oct 11, 2023
1 parent dec51e2 commit 12eabcf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
6 changes: 3 additions & 3 deletions dsp_permissions_scripts/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from dsp_permissions_scripts.models.scope import PUBLIC
from dsp_permissions_scripts.utils.authentication import login
from dsp_permissions_scripts.utils.doap_get import get_doaps_of_project
from dsp_permissions_scripts.utils.doap_serialize import serialize_project_doaps
from dsp_permissions_scripts.utils.doap_serialize import serialize_doaps_of_project
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 serialize_resource_oaps
Expand Down Expand Up @@ -69,13 +69,13 @@ def update_doaps(
shortcode=shortcode,
token=token,
)
serialize_project_doaps(
serialize_doaps_of_project(
project_doaps=project_doaps,
shortcode=shortcode,
mode="original",
)
project_doaps_updated = modify_doaps(doaps=project_doaps)
serialize_project_doaps(
serialize_doaps_of_project(
project_doaps=project_doaps,
shortcode=shortcode,
mode="modified",
Expand Down
14 changes: 9 additions & 5 deletions dsp_permissions_scripts/utils/doap_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path
from typing import Literal

from dsp_permissions_scripts.models.doap import Doap
from dsp_permissions_scripts.models.doap import Doap, DoapTargetType


def _get_file_path(
Expand All @@ -12,16 +12,20 @@ def _get_file_path(
return Path(f"project_data/{shortcode}/DOAPs_{mode}.json")


def serialize_project_doaps(
def serialize_doaps_of_project(
project_doaps: list[Doap],
shortcode: str,
mode: Literal["original", "modified"],
target: DoapTargetType = DoapTargetType.ALL,
) -> None:
"""Serialize the resource DOAPs to a JSON file."""
"""Serialize the DOAPs of a project to a JSON file."""
filepath = _get_file_path(shortcode, mode)
filepath.parent.mkdir(parents=True, exist_ok=True)
explanation_string = f"Project {shortcode} has {len(project_doaps)} DOAPs"
if target != DoapTargetType.ALL:
explanation_string += f" which are related to a {target}"
doaps_as_dicts = [doap.model_dump(exclude_none=True) for doap in project_doaps]
doaps_as_dict = {f"Project {shortcode} has {len(project_doaps)} DOAPs": doaps_as_dicts}
doaps_as_dict = {explanation_string: doaps_as_dicts}
with open(filepath, mode="w", encoding="utf-8") as f:
f.write(json.dumps(doaps_as_dict, ensure_ascii=False, indent=2))

Expand All @@ -30,7 +34,7 @@ def deserialize_project_doaps(
shortcode: str,
mode: Literal["original", "modified"],
) -> list[Doap]:
"""Deserialize the resource DOAPs from a JSON file."""
"""Deserialize the DOAPs of a project from a JSON file."""
filepath = _get_file_path(shortcode, mode)
with open(filepath, mode="r", encoding="utf-8") as f:
doaps_as_dict = json.load(f)
Expand Down

0 comments on commit 12eabcf

Please sign in to comment.