diff --git a/dsp_permissions_scripts/oap/update_iris.py b/dsp_permissions_scripts/oap/update_iris.py index c967d2e..08d36ad 100644 --- a/dsp_permissions_scripts/oap/update_iris.py +++ b/dsp_permissions_scripts/oap/update_iris.py @@ -25,33 +25,34 @@ @dataclass class IRIUpdater(metaclass=ABCMeta): iri: str - res_dict: dict[str, Any] + dsp_client: DspClient + res_dict: dict[str, Any] = field(init=False) err_msg: str | None = field(init=False, default=None) @abstractmethod - def update_iri(self, new_scope: PermissionScope, dsp_client: DspClient) -> None: + def update_iri(self, new_scope: PermissionScope) -> None: pass @staticmethod def from_string(string: str, dsp_client: DspClient) -> ResourceIRIUpdater | ValueIRIUpdater: - res_iri = re.sub(r"/values/[^/]+$", "", string) - res_dict = _get_res_dict(res_iri, dsp_client) if re.search(r"^http://rdfh\.ch/[^/]{4}/[^/]{22}/values/[^/]{22}$", string): - return ValueIRIUpdater(string, res_dict) + return ValueIRIUpdater(string, dsp_client) elif re.search(r"^http://rdfh\.ch/[^/]{4}/[^/]{22}$", string): - return ResourceIRIUpdater(string, res_dict) + return ResourceIRIUpdater(string, dsp_client) else: raise ValueError(f"Could not parse IRI {string}") - -@cache -def _get_res_dict(res_iri: str, dsp_client: DspClient) -> dict[str, Any]: - return dsp_client.get(f"/v2/resources/{quote_plus(res_iri, safe='')}") + @cache + def _get_res_dict(self, res_iri: str) -> dict[str, Any]: + return self.dsp_client.get(f"/v2/resources/{quote_plus(res_iri, safe='')}") @dataclass class ResourceIRIUpdater(IRIUpdater): - def update_iri(self, new_scope: PermissionScope, dsp_client: DspClient) -> None: + def __post_init__(self) -> None: + self.res_dict = self._get_res_dict(self.iri, self.dsp_client) + + def update_iri(self, new_scope: PermissionScope) -> None: try: update_permissions_for_resource( resource_iri=self.iri, @@ -59,7 +60,7 @@ def update_iri(self, new_scope: PermissionScope, dsp_client: DspClient) -> None: resource_type=self.res_dict["@type"], context=self.res_dict["@context"] | {"knora-admin": KNORA_ADMIN_ONTO_NAMESPACE}, scope=new_scope, - dsp_client=dsp_client, + dsp_client=self.dsp_client, ) except ApiError as err: self.err_msg = err.message @@ -68,7 +69,11 @@ def update_iri(self, new_scope: PermissionScope, dsp_client: DspClient) -> None: @dataclass class ValueIRIUpdater(IRIUpdater): - def update_iri(self, new_scope: PermissionScope, dsp_client: DspClient) -> None: + def __post_init__(self) -> None: + res_iri = re.sub(r"/values/[^/]{22}$", "", self.iri) + self.res_dict = self._get_res_dict(res_iri, self.dsp_client) + + def update_iri(self, new_scope: PermissionScope) -> None: val_oap = next((v for v in get_value_oaps(self.res_dict) if v.value_iri == self.iri), None) if not val_oap: self.err_msg = f"Could not find value {self.iri} in resource {self.res_dict['@id']}" @@ -81,7 +86,7 @@ def update_iri(self, new_scope: PermissionScope, dsp_client: DspClient) -> None: value=val_oap, resource_type=self.res_dict["@type"], context=self.res_dict["@context"] | {"knora-admin": KNORA_ADMIN_ONTO_NAMESPACE}, - dsp_client=dsp_client, + dsp_client=self.dsp_client, ) except ApiError as err: self.err_msg = err.message @@ -95,7 +100,7 @@ def update_iris( ) -> None: iri_updaters = _initialize_iri_updaters(iri_file, dsp_client) for iri in iri_updaters: - iri.update_iri(new_scope, dsp_client) + iri.update_iri(new_scope) _tidy_up(iri_updaters, iri_file)