Skip to content

Commit

Permalink
feat: allow prefixed env vars
Browse files Browse the repository at this point in the history
For label_predicates, description_predicates etc. this commit allows you
to set the values as prefixed values. for example

# .env
LABEL_PREDICATES='["schema:headline", "dcterms:title"]'

this should work for any of the default bound prefixes in rdflib
  • Loading branch information
lalewis1 committed Nov 20, 2024
1 parent 6014a42 commit f0db786
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
12 changes: 9 additions & 3 deletions prez/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import toml
from pydantic import field_validator
from pydantic_settings import BaseSettings
from rdflib import DCTERMS, RDFS, SDO, URIRef
from rdflib import DCTERMS, RDFS, SDO, Graph, URIRef
from rdflib.namespace import SKOS

from prez.reference_data.prez_ns import EP, REG
Expand Down Expand Up @@ -130,14 +130,20 @@ def get_version(cls, v):
"other_predicates",
)
def validate_predicates(cls, v):
nm = Graph().namespace_manager
predicates = []
try:
v = [URIRef(predicate) for predicate in v]
for predicate in v:
if predicate.startswith("http"):
predicates.append(URIRef(predicate))
else:
predicates.append(URIRef(nm.expand_curie(predicate)))
except ValueError as e:
raise ValueError(
"Could not parse predicates. predicates must be valid URIs no prefixes allowed "
f"original message: {e}"
)
return v
return predicates


settings = Settings()
21 changes: 11 additions & 10 deletions tests/test_predicates.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from pydantic import ValidationError

from prez.config import Settings

Expand All @@ -7,8 +8,8 @@
"label_predicates, error",
[
[["https://schema.org/name"], None],
[["1", "2", "3"], None],
[[1], TypeError],
[["1", "2", "3"], ValidationError],
[[1], AttributeError],
["not a list", ValueError],
],
)
Expand All @@ -24,8 +25,8 @@ def test_label_predicates(label_predicates, error):
"description_predicates, error",
[
[["https://schema.org/description"], None],
[["1", "2", "3"], None],
[[1], TypeError],
[["1", "2", "3"], ValidationError],
[[1], AttributeError],
["not a list", ValueError],
],
)
Expand All @@ -41,8 +42,8 @@ def test_description_predicates(description_predicates, error):
"provenance_predicates, error",
[
[["https://schema.org/provenance"], None],
[["1", "2", "3"], None],
[[1], TypeError],
[["1", "2", "3"], ValidationError],
[[1], AttributeError],
["not a list", ValueError],
],
)
Expand All @@ -58,8 +59,8 @@ def test_provenance_predicates(provenance_predicates, error):
"search_predicates, error",
[
[["https://schema.org/search"], None],
[["1", "2", "3"], None],
[[1], TypeError],
[["1", "2", "3"], ValidationError],
[[1], AttributeError],
["not a list", ValueError],
],
)
Expand All @@ -75,8 +76,8 @@ def test_search_predicates(search_predicates, error):
"other_predicates, error",
[
[["https://schema.org/other"], None],
[["1", "2", "3"], None],
[[1], TypeError],
[["1", "2", "3"], ValidationError],
[[1], AttributeError],
["not a list", ValueError],
],
)
Expand Down

0 comments on commit f0db786

Please sign in to comment.