Skip to content

Commit

Permalink
improving logging/printing in oap.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Oct 4, 2023
1 parent e6f6acb commit 0d231b9
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions dsp_permissions_scripts/utils/oap.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ def apply_updated_oaps_on_server(
token: str,
) -> None:
"""Applies object access permissions on a DSP server."""
for resource_oap in resource_oaps:
for index, resource_oap in enumerate(resource_oaps):
msg = f"Updating permissions of resource {index + 1}/{len(resource_oaps)}: {resource_oap.object_iri}..."
logger.info("=====")
logger.info(msg)
print(f"{get_timestamp()}: {msg}")
update_permissions_for_resources_and_values(
resource_iris=[resource_oap.object_iri],
scope=resource_oap.scope,
host=host,
token=token,
)
logger.info(f"Updated permissions of resource {resource_oap.object_iri} and its values.")


def update_permissions_for_resources_and_values(
Expand All @@ -50,8 +55,6 @@ def __update_permissions_for_resource_and_values(
"""
Updates the permissions for the given resource and its values.
"""
logger.info(f"Updating permissions for resource {resource_iri}...")
print(f"{get_timestamp()}: Updating permissions for resource {resource_iri}...")
resource = __get_resource(resource_iri, host, token)
lmd = __get_lmd(resource)
type_ = __get_type(resource)
Expand All @@ -60,8 +63,6 @@ def __update_permissions_for_resource_and_values(
update_permissions_for_resource(resource_iri, lmd, type_, context, scope, host, token)
for v in values:
__update_permissions_for_value(resource_iri, v, type_, context, scope, host, token)
logger.info(f"Successfully updated permissions for resource {resource_iri} and its values.")
logger.info("=====")


def update_permissions_for_resource(
Expand Down Expand Up @@ -89,7 +90,7 @@ def update_permissions_for_resource(
headers = {"Authorization": f"Bearer {token}"}
response = requests.put(url, headers=headers, json=payload, timeout=5)
assert response.status_code == 200
logger.info(f"Updated permissions for {resource_iri}")
logger.info(f"Updated permissions of resource {resource_iri}")


def __update_permissions_for_value(
Expand Down Expand Up @@ -121,17 +122,17 @@ def __update_permissions_for_value(
if response.status_code == 400 and response.text:
already = "dsp.errors.BadRequestException: The submitted permissions are the same as the current ones"
if already in response.text:
msg = f"Permissions for value {value.value_iri} of resource {resource_iri} are already up to date"
msg = f"Permissions of resource {resource_iri}, value {value.value_iri} are already up to date"
logger.warning(msg)
elif response.status_code != 200:
logger.error(
f"Error while updating permissions for value {value.value_iri} of resource {resource_iri}. "
f"Error while updating permissions of resource {resource_iri}, value {value.value_iri}. "
f"Response status code: {response.status_code}. "
f"Response text: {response.text}. "
f"Payload: {json.dumps(payload, indent=4)}"
)
else:
logger.info(f"Updated permissions for {value.value_iri} of resource {resource_iri}")
logger.info(f"Updated permissions of resource {resource_iri}, value {value.value_iri}")


def __get_value_iris(resource: dict[str, Any]) -> list[ValueUpdate]:
Expand Down

0 comments on commit 0d231b9

Please sign in to comment.