Skip to content

Commit 75ad66d

Browse files
committed
Add configurable blocklist action
1 parent 6e8c9bc commit 75ad66d

1 file changed

Lines changed: 28 additions & 5 deletions

File tree

src/curies/preprocessing.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@
77
from typing import Any, Literal, TypeVar, overload
88

99
from pydantic import BaseModel, Field
10-
from typing_extensions import Self
10+
from typing_extensions import Self, TypeAlias
1111

1212
from .api import Converter, Reference, ReferenceTuple
1313

1414
__all__ = [
15+
"BlockAction",
1516
"BlocklistError",
1617
"PreprocessingBlocklists",
1718
"PreprocessingConverter",
1819
"PreprocessingRewrites",
1920
"PreprocessingRules",
2021
]
2122

23+
#: The action taken when the blocklist is invoked
24+
BlockAction: TypeAlias = Literal["raise", "pass"]
2225
X = TypeVar("X", bound=Reference)
2326

2427

@@ -215,7 +218,12 @@ def parse(
215218
) -> ReferenceTuple | None: ...
216219

217220
def parse(
218-
self, str_or_uri_or_curie: str, *, strict: bool = False, context: str | None = None
221+
self,
222+
str_or_uri_or_curie: str,
223+
*,
224+
strict: bool = False,
225+
context: str | None = None,
226+
block_action: BlockAction = "raise",
219227
) -> ReferenceTuple | None:
220228
"""Parse a string, CURIE, or URI."""
221229
if r1 := self.rules.remap_full(
@@ -227,7 +235,10 @@ def parse(
227235
str_or_uri_or_curie = self.rules.remap_prefix(str_or_uri_or_curie, context=context)
228236

229237
if self.rules.str_is_blocked(str_or_uri_or_curie, context=context):
230-
raise BlocklistError
238+
if block_action == "raise":
239+
raise BlocklistError
240+
else:
241+
return None
231242

232243
if strict:
233244
return super().parse(str_or_uri_or_curie, strict=strict)
@@ -246,7 +257,12 @@ def parse_curie(
246257
) -> ReferenceTuple: ...
247258

248259
def parse_curie(
249-
self, curie: str, *, strict: bool = False, context: str | None = None
260+
self,
261+
curie: str,
262+
*,
263+
strict: bool = False,
264+
context: str | None = None,
265+
block_action: BlockAction = "raise",
250266
) -> ReferenceTuple | None:
251267
"""Parse and standardize a CURIE.
252268
@@ -255,6 +271,10 @@ def parse_curie(
255271
to false.
256272
:param context: Is there a context, e.g., an ontology prefix that should be
257273
applied to the remapping and blocklist rules?
274+
:param block_action: What action should be taken when the blocklist is invoked?
275+
276+
- **raise** - raise an exception
277+
- **pass** - return ``None``
258278
259279
:returns: A tuple representing a parsed and standardized CURIE
260280
@@ -267,7 +287,10 @@ def parse_curie(
267287
curie = self.rules.remap_prefix(curie, context=context)
268288

269289
if self.rules.str_is_blocked(curie, context=context):
270-
raise BlocklistError
290+
if block_action == "raise":
291+
raise BlocklistError
292+
else:
293+
return None
271294

272295
if strict:
273296
return super().parse_curie(curie, strict=strict)

0 commit comments

Comments
 (0)