Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use query parameters wherever possible in Neo4jStore #330

Merged
merged 11 commits into from
Nov 19, 2024
27 changes: 25 additions & 2 deletions alchemiscale/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from pydantic import BaseModel, Field, validator, root_validator
from gufe.tokenization import GufeKey
from re import fullmatch
import unicodedata
import string


class Scope(BaseModel):
Expand Down Expand Up @@ -114,6 +116,9 @@ def specific(self) -> bool:
return all(self.to_tuple())


class InvalidGufeKeyError(ValueError): ...


class ScopedKey(BaseModel):
"""Unique identifier for GufeTokenizables in state store.

Expand All @@ -131,8 +136,26 @@ class Config:
frozen = True

@validator("gufe_key")
def cast_gufe_key(cls, v):
return GufeKey(v)
def gufe_key_validator(cls, v):
v = str(v)

# GufeKey is of form <prefix>-<hex>
try:
_prefix, _token = v.split("-")
except ValueError:
raise InvalidGufeKeyError("gufe_key must be of the form '<prefix>-<hex>'")

# Normalize the input to NFC form
v_normalized = unicodedata.normalize("NFC", v)

# Allowed characters: letters, numbers, underscores, hyphens
allowed_chars = set(string.ascii_letters + string.digits + "_-")

if not set(v_normalized).issubset(allowed_chars):
raise InvalidGufeKeyError("gufe_key contains invalid characters")

# Cast to GufeKey
return GufeKey(v_normalized)

def __repr__(self): # pragma: no cover
return f"<ScopedKey('{str(self)}')>"
Expand Down
Loading
Loading