Skip to content

Commit

Permalink
Add info level logging of config updates; remove unused method
Browse files Browse the repository at this point in the history
  • Loading branch information
brynpickering committed Dec 18, 2024
1 parent 19902d9 commit 15d371c
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions src/calliope/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Licensed under the Apache 2.0 License (see LICENSE file).
"""Implements the Calliope configuration class."""

import logging
from collections.abc import Hashable
from pathlib import Path
from typing import Annotated, Literal, TypeVar
Expand All @@ -16,6 +17,8 @@
MODES_T = Literal["plan", "operate", "spores"]
CONFIG_T = Literal["init", "build", "solve"]

LOGGER = logging.getLogger(__name__)

# ==
# Taken from https://github.com/pydantic/pydantic-core/pull/820#issuecomment-1670475909
T = TypeVar("T", bound=Hashable)
Expand All @@ -35,21 +38,6 @@ def _validate_unique_list(v: list[T]) -> list[T]:
# ==


def hide_from_schema(to_hide: list[str]):
"""Hide fields from the generated schema.
Args:
to_hide (list[str]): List of fields to hide.
"""

def _hide_from_schema(schema: dict):
for hide in to_hide:
schema.get("properties", {}).pop(hide, None)
return schema

return _hide_from_schema


class ConfigBaseModel(BaseModel):
"""A base class for creating pydantic models for Calliope configuration options."""

Expand All @@ -71,12 +59,16 @@ def update(self, update_dict: dict, deep: bool = False) -> Self:
BaseModel: New model instance.
"""
new_dict: dict = {}
# Iterate through dict to be updated and convert any sub-dicts into their respective pydantic model objects
for key, val in update_dict.items():
# Iterate through dict to be updated and convert any sub-dicts into their respective pydantic model objects.
# Wrapped in `AttrDict` to allow users to define dot notation nested configuration.
for key, val in AttrDict(update_dict).items():
key_class = getattr(self, key)
if isinstance(key_class, ConfigBaseModel):
new_dict[key] = key_class.update(val)
else:
LOGGER.info(
f"Updating {self.model_config["title"]} `{key}`: {key_class} -> {val}"
)
new_dict[key] = val
updated = super().model_copy(update=new_dict, deep=deep)
updated.model_validate(updated)
Expand All @@ -97,6 +89,7 @@ def model_no_ref_schema(self) -> AttrDict:
class Init(ConfigBaseModel):
"""All configuration options used when initialising a Calliope model."""

model_config = {"title": "Model initialisation configuration"}
name: str | None = Field(default=None)
"""Model name"""

Expand Down Expand Up @@ -143,6 +136,7 @@ class Init(ConfigBaseModel):
class BuildOperate(ConfigBaseModel):
"""Operate mode configuration options used when building a Calliope optimisation problem (`calliope.Model.build`)."""

model_config = {"title": "Model build operate mode configuration"}
window: str = Field(default="24h")
"""
Operate mode rolling `window`, given as a pandas frequency string.
Expand All @@ -163,6 +157,7 @@ class BuildOperate(ConfigBaseModel):
class Build(ConfigBaseModel):
"""Base configuration options used when building a Calliope optimisation problem (`calliope.Model.build`)."""

model_config = {"title": "Model build configuration"}
mode: MODES_T = Field(default="plan")
"""Mode in which to run the optimisation."""

Expand Down Expand Up @@ -204,6 +199,7 @@ class Build(ConfigBaseModel):
class SolveSpores(ConfigBaseModel):
"""SPORES configuration options used when solving a Calliope optimisation problem (`calliope.Model.solve`)."""

model_config = {"title": "Model solve SPORES mode configuration"}
number: int = Field(default=3)
"""SPORES mode number of iterations after the initial base run."""

Expand Down Expand Up @@ -241,6 +237,7 @@ def require_save_per_spore_path(self) -> Self:
class Solve(ConfigBaseModel):
"""Base configuration options used when solving a Calliope optimisation problem (`calliope.Model.solve`)."""

model_config = {"title": "Model Solve Configuration"}
save_logs: str | None = Field(default=None)
"""If given, should be a path to a directory in which to save optimisation logs."""

Expand Down

0 comments on commit 15d371c

Please sign in to comment.