Skip to content

Commit

Permalink
chore: simplify value-PR (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum authored Apr 17, 2024
1 parent ee449e8 commit e76b024
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
4 changes: 2 additions & 2 deletions dsp_permissions_scripts/oap/oap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
logger = get_logger(__name__)


def _get_all_resource_oaps_of_resclass(resclass_iri: str, project_iri: str, dsp_client: DspClient) -> list[Oap]:
def _get_all_oaps_of_resclass(resclass_iri: str, project_iri: str, dsp_client: DspClient) -> list[Oap]:
logger.info(f"Getting all resource OAPs of class {resclass_iri}...")
headers = {"X-Knora-Accept-Project": project_iri}
all_oaps: list[Oap] = []
Expand Down Expand Up @@ -103,7 +103,7 @@ def get_all_resource_oaps_of_project(
resclass_iris = [x for x in resclass_iris if x not in excluded_class_iris]
all_oaps = []
for resclass_iri in resclass_iris:
oaps = _get_all_resource_oaps_of_resclass(resclass_iri, project_iri, dsp_client)
oaps = _get_all_oaps_of_resclass(resclass_iri, project_iri, dsp_client)
all_oaps.extend(oaps)
logger.info(f"Retrieved a TOTAL of {len(all_oaps)} OAPs")
return all_oaps
23 changes: 11 additions & 12 deletions dsp_permissions_scripts/oap/oap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,21 @@ def _update_permissions_for_resource_and_values(
return resource_iri, success


def _write_failed_res_iris_to_file(
failed_res_iris: list[str],
def _write_failed_iris_to_file(
failed_iris: list[str],
shortcode: str,
host: str,
filename: str,
) -> None:
with open(filename, "w", encoding="utf-8") as f:
f.write(f"Problems occurred while updating the OAPs of these resources (project {shortcode}, host {host}):\n")
f.write("\n".join(failed_res_iris))
f.write("\n".join(failed_iris))


def _launch_thread_pool(resource_oaps: list[Oap], nthreads: int, dsp_client: DspClient) -> list[str]:
counter = 0
total = len(resource_oaps)
failed_res_iris: list[str] = []
all_failed_iris: list[str] = []
with ThreadPoolExecutor(max_workers=nthreads) as pool:
jobs = [
pool.submit(
Expand All @@ -165,11 +165,11 @@ def _launch_thread_pool(resource_oaps: list[Oap], nthreads: int, dsp_client: Dsp
resource_iri, success = result.result()
counter += 1
if not success:
failed_res_iris.append(resource_iri)
all_failed_iris.append(resource_iri)
logger.info(f"Failed updating resource {counter}/{total} ({resource_iri}) and its values.")
else:
logger.info(f"Updated resource {counter}/{total} ({resource_iri}) and its values.")
return failed_res_iris
return all_failed_iris


def apply_updated_oaps_on_server(
Expand All @@ -188,19 +188,18 @@ def apply_updated_oaps_on_server(
return
logger.info(f"******* Updating OAPs of {len(oaps)} resources on {host}... *******")

failed_res_iris = _launch_thread_pool(oaps, nthreads, dsp_client)

if failed_res_iris:
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_{timestamp}.txt"
_write_failed_res_iris_to_file(
failed_res_iris=failed_res_iris,
_write_failed_iris_to_file(
failed_iris=failed_iris,
shortcode=shortcode,
host=host,
filename=filename,
)
msg = (
f"ERROR: {len(failed_res_iris)} resources could not (or only partially) be updated. "
f"ERROR: {len(failed_iris)} resources could not (or only partially) be updated. "
f"They were written to {filename}."
)
logger.error(msg)
Expand Down

0 comments on commit e76b024

Please sign in to comment.