Skip to content

Commit

Permalink
remove unhashable argument from cached function
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Sep 10, 2024
1 parent 92cb8ee commit d593d29
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions dsp_permissions_scripts/oap/update_iris.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,42 @@
@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,
lmd=self.res_dict["lastModificationDate"],
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
Expand All @@ -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']}"
Expand All @@ -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
Expand All @@ -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)


Expand Down

0 comments on commit d593d29

Please sign in to comment.