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
30 changes: 28 additions & 2 deletions alchemiscale/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pydantic import BaseModel, Field, validator, root_validator
from gufe.tokenization import GufeKey
from re import fullmatch
import unicodedata


class Scope(BaseModel):
Expand Down Expand Up @@ -113,6 +114,7 @@
"""Return `True` if this Scope has no unspecified elements."""
return all(self.to_tuple())

class InvalidGufeKeyError(ValueError): ...

class ScopedKey(BaseModel):
"""Unique identifier for GufeTokenizables in state store.
Expand All @@ -131,8 +133,32 @@
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>
prefix, token = v.split("-")
if not prefix or not token:
raise InvalidGufeKeyError("gufe_key must be of the form '<prefix>-<hex>'")

Check warning on line 142 in alchemiscale/models.py

View check run for this annotation

Codecov / codecov/patch

alchemiscale/models.py#L142

Added line #L142 was not covered by tests

# Normalize the input to NFC form

v_normalized = unicodedata.normalize("NFC", v)

# Ensure that there are no control characters
if any(unicodedata.category(c) == "Cc" for c in v_normalized):
raise InvalidGufeKeyError("gufe_key contains invalid control characters")

Check warning on line 150 in alchemiscale/models.py

View check run for this annotation

Codecov / codecov/patch

alchemiscale/models.py#L150

Added line #L150 was not covered by tests

# Allowed characters: letters, numbers, underscores, hyphens
allowed_chars = set(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"
)

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