Skip to content

Commit

Permalink
Add better exception message when failing to create PseudoFieldRequest (
Browse files Browse the repository at this point in the history
#405)

* Add better exception message when failing to create PseudoFieldRequest

* add missing append
  • Loading branch information
mallport authored Jun 20, 2024
1 parent ef6c8dd commit f4d174d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dapla-toolbelt-pseudo"
version = "2.1.0"
version = "2.1.1"
description = "Pseudonymization extensions for Dapla"
authors = ["Dapla Developers <[email protected]>"]
license = "MIT"
Expand Down
79 changes: 49 additions & 30 deletions src/dapla_pseudo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from dapla import FileClient
from gcsfs.core import GCSFile
from google.auth.exceptions import DefaultCredentialsError
from pydantic import ValidationError

from dapla_pseudo.constants import PseudoOperation
from dapla_pseudo.exceptions import FileInvalidError
Expand Down Expand Up @@ -98,45 +99,63 @@ def build_pseudo_field_request(
"""Builds a FieldRequest object."""
mutable_df.match_rules(rules, target_rules)
matched_fields = mutable_df.get_matched_fields()
requests: list[PseudoFieldRequest | DepseudoFieldRequest | RepseudoFieldRequest] = (
[]
)
req: PseudoFieldRequest | DepseudoFieldRequest | RepseudoFieldRequest
match pseudo_operation:
case PseudoOperation.PSEUDONYMIZE:
return [
PseudoFieldRequest(
pseudo_func=field.func,
name=field.path,
pattern=field.pattern,
values=field.get_value(),
keyset=KeyWrapper(custom_keyset).keyset,
)
for field in matched_fields.values()
]
for field in matched_fields.values():
try:
req = PseudoFieldRequest(
pseudo_func=field.func,
name=field.path,
pattern=field.pattern,
values=field.get_value(),
keyset=KeyWrapper(custom_keyset).keyset,
)
requests.append(req)
except ValidationError as e:
raise Exception(
f"Path: {field.path}, Values: {field.get_value()}"
) from e
case PseudoOperation.DEPSEUDONYMIZE:
return [
DepseudoFieldRequest(
pseudo_func=field.func,
name=field.path,
pattern=field.pattern,
values=field.get_value(),
keyset=KeyWrapper(custom_keyset).keyset,
)
for field in matched_fields.values()
]
case PseudoOperation.REPSEUDONYMIZE:
if target_rules is not None:
return [
RepseudoFieldRequest(
source_pseudo_func=field.func,
target_pseudo_func=field.target_func,
for field in matched_fields.values():
try:
req = DepseudoFieldRequest(
pseudo_func=field.func,
name=field.path,
pattern=field.pattern,
values=field.get_value(),
source_keyset=KeyWrapper(custom_keyset).keyset,
target_keyset=KeyWrapper(target_custom_keyset).keyset,
keyset=KeyWrapper(custom_keyset).keyset,
)
for field in matched_fields.values()
]
requests.append(req)
except ValidationError as e:
raise Exception(
f"Path: {field.path}, Values: {field.get_value()}"
) from e

case PseudoOperation.REPSEUDONYMIZE:
if target_rules is not None:
for field in matched_fields.values():
try:
req = RepseudoFieldRequest(
source_pseudo_func=field.func,
target_pseudo_func=field.target_func,
name=field.path,
pattern=field.pattern,
values=field.get_value(),
source_keyset=KeyWrapper(custom_keyset).keyset,
target_keyset=KeyWrapper(target_custom_keyset).keyset,
)
requests.append(req)
except ValidationError as e:
raise Exception(
f"Path: {field.path}, Values: {field.get_value()}"
) from e
else:
raise ValueError("Found no target rules")
return requests


def build_pseudo_file_request(
Expand Down

0 comments on commit f4d174d

Please sign in to comment.