Skip to content

Commit

Permalink
small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Apr 15, 2024
1 parent 108e0d9 commit bab3823
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
5 changes: 2 additions & 3 deletions dsp_permissions_scripts/oap/oap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
def _get_all_oaps_of_resclass(
resclass_iri: str, project_iri: str, dsp_client: DspClient, oap_config: OapRetrieveConfig
) -> list[Oap]:
logger.info(f"Getting all OAPs of class {resclass_iri}...")
headers = {"X-Knora-Accept-Project": project_iri}
all_oaps: list[Oap] = []
page = 0
Expand Down Expand Up @@ -68,7 +67,7 @@ def _get_next_page(

# result contains several resources: return them, then continue with next page
if "@graph" in result:
oaps: list[Oap] = []
oaps = []
for r in result["@graph"]:
oaps.append(_get_oap_of_one_resource(r, oap_config))
return True, oaps
Expand Down Expand Up @@ -129,7 +128,7 @@ def get_resource(resource_iri: str, dsp_client: DspClient) -> dict[str, Any]:
raise err from None


def get_oap_by_resource_iri(resource_iri: str, dsp_client: DspClient) -> ResourceOap:
def get_resource_oap_by_iri(resource_iri: str, dsp_client: DspClient) -> ResourceOap:
resource = get_resource(resource_iri, dsp_client)
scope = create_scope_from_string(resource["knora-api:hasPermissions"])
return ResourceOap(scope=scope, resource_iri=resource_iri)
Expand Down
10 changes: 7 additions & 3 deletions dsp_permissions_scripts/oap/oap_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@


class Oap(BaseModel):
"""
Model representing an object access permission of a resource and its values.
If only the resource is of interest, value_oaps will be an empty list.
If only the values (or a part of them) are of interest, resource_oap will be None.
"""
resource_oap: ResourceOap | None
value_oaps: list[ValueOap]


class ResourceOap(BaseModel):
"""Model representing a resource object access permission, containing a scope and the IRI of the resource"""

"""Model representing an object access permission of a resource"""
scope: PermissionScope
resource_iri: str


class ValueOap(BaseModel):
"""
Model representing a value object access permission.
Model representing an object access permission of a value.
Fields:
scope: permissions of this value
Expand Down
7 changes: 3 additions & 4 deletions dsp_permissions_scripts/oap/oap_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ def serialize_oaps(
if oap.resource_oap:
_serialize_oap(oap.resource_oap, folder)
counter += 1
if oap.value_oaps:
for value_oap in oap.value_oaps:
_serialize_oap(value_oap, folder)
counter += 1
for value_oap in oap.value_oaps:
_serialize_oap(value_oap, folder)
counter += 1
logger.info(f"Successfully wrote {len(oaps)} OAPs into {counter} files in folder {str(folder)}")


Expand Down
10 changes: 6 additions & 4 deletions dsp_permissions_scripts/oap/oap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ def _update_permissions_for_resource(
payload["knora-api:lastModificationDate"] = lmd
try:
dsp_client.put("/v2/resources", data=payload)
logger.info(f"Updated permissions of resource {resource_iri}")
except PermissionsAlreadyUpToDate:
logger.warning(f"Permissions of resource {resource_iri} are already up to date")
except ApiError as err:
err.message = f"ERROR while updating permissions of resource {resource_iri}"
raise err from None
logger.info(f"Updated permissions of resource {resource_iri}")


def _update_batch(batch: tuple[Oap, ...], dsp_client: DspClient) -> list[str]:
Expand Down Expand Up @@ -126,7 +128,7 @@ def _write_failed_res_iris_to_file(


def _launch_thread_pool(oaps: list[Oap], nthreads: int, dsp_client: DspClient) -> list[str]:
all_failed_iris: list[str] = []
all_failed_iris = []
with ThreadPoolExecutor(max_workers=nthreads) as pool:
jobs = [pool.submit(_update_batch, batch, dsp_client) for batch in itertools.batched(oaps, 100)]
for result in as_completed(jobs):
Expand All @@ -150,13 +152,13 @@ def apply_updated_oaps_on_server(
logger.warning("There are no OAPs to update")
return
logger.info(f"******* Updating OAPs of {len(oaps)} resources on {host}... *******")
failed_iris = _launch_thread_pool(oaps, nthreads, dsp_client)

failed_iris = _launch_thread_pool(oaps, nthreads, dsp_client)
if failed_iris:
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
filename = f"FAILED_RESOURCES_AND_VALUES_{timestamp}.txt"
_write_failed_res_iris_to_file(
failed_iris=failed_iris,
failed_iris=sorted(failed_iris),
shortcode=shortcode,
host=host,
filename=filename,
Expand Down
3 changes: 1 addition & 2 deletions dsp_permissions_scripts/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ def modify_oaps(oaps: list[Oap]) -> list[Oap]:
if oap.resource_oap:
if group.SYSTEM_ADMIN not in oap.resource_oap.scope.CR:
oap.resource_oap.scope = oap.resource_oap.scope.add("CR", group.SYSTEM_ADMIN)
modified_oaps.append(oap)
for value_oap in oap.value_oaps:
if group.SYSTEM_ADMIN not in value_oap.scope.CR:
value_oap.scope = value_oap.scope.add("CR", group.SYSTEM_ADMIN)
modified_oaps.append(oap)
modified_oaps.append(oap)
return modified_oaps


Expand Down

0 comments on commit bab3823

Please sign in to comment.