Skip to content

Commit

Permalink
Include validation of Maggma store in Pydantic settings (#1002)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-S-Rosen authored Sep 27, 2023
1 parent d58646e commit 478501f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/quacc/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from shutil import which
from typing import List, Optional, Union

from maggma.core import Store
from monty.json import MontyDecoder
from pydantic import BaseSettings, Field, root_validator, validator

from quacc.calculators.presets import vasp as vasp_defaults
Expand Down Expand Up @@ -94,7 +96,7 @@ class QuaccSettings(BaseSettings):
# ---------------------------
# Data Store Settings
# ---------------------------
PRIMARY_STORE: str = Field(
PRIMARY_STORE: Union[str, Store] = Field(
None,
description=(
"String-based JSON representation of the primary Maggma data store "
Expand Down Expand Up @@ -268,6 +270,10 @@ def resolve_and_make_paths(cls, v):
def expand_paths(cls, v):
return v.expanduser()

@validator("PRIMARY_STORE")
def generate_store(cls, v):
return MontyDecoder().decode(v) if v else None

class Config:
"""Pydantic config settings."""

Expand Down
10 changes: 3 additions & 7 deletions src/quacc/wflow/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import warnings
from typing import TYPE_CHECKING

from monty.json import MontyDecoder

if TYPE_CHECKING:
from maggma.core import Store

Expand Down Expand Up @@ -66,16 +64,15 @@ def covalent_to_db(
store.update(docs, key="dispatch_id")


def results_to_db(store: Store | str, results: dict | list[dict]) -> None:
def results_to_db(store: Store, results: dict | list[dict]) -> None:
"""
Store the results of a quacc recipe in a user-specified Maggma Store. A UUID
will be generated for each entry.
Parameters
----------
store
The Maggma Store object to store the results in or a str representation
of a Maggma Store (taken from `.to_json()`)
The Maggma Store object to store the results in
results
The output summary dictionary or list of dictionaries from a quacc
recipe
Expand All @@ -84,11 +81,10 @@ def results_to_db(store: Store | str, results: dict | list[dict]) -> None:
-------
None
"""
if isinstance(store, str):
store = MontyDecoder().decode(store)

if isinstance(results, dict):
results = [results]

for result in results:
result["uuid"] = str(uuid.uuid4())

Expand Down
3 changes: 1 addition & 2 deletions tests/settings/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ def test_file(monkeypatch, tmpdir):

def test_store(tmpdir):
tmpdir.chdir()
store = MemoryStore()
SETTINGS.PRIMARY_STORE = store.to_json()
SETTINGS.PRIMARY_STORE = MemoryStore()
atoms = bulk("Cu")
static_job(atoms)

Expand Down

0 comments on commit 478501f

Please sign in to comment.