Skip to content

Commit

Permalink
Merge branch 'add-config-obj' into add-config-obj-replace-schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
irm-codebase committed Nov 20, 2024
2 parents 3186660 + e35ee0e commit 3a71e97
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
4 changes: 1 addition & 3 deletions docs/hooks/generate_readable_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
TEMPDIR = tempfile.TemporaryDirectory()

SCHEMAS = {
"config_schema": config_schema.CalliopeConfig().model_json_schema(
replace_refs=True
),
"config_schema": config_schema.CalliopeConfig().model_no_ref_schema(),
"model_schema": schema.MODEL_SCHEMA,
"math_schema": schema.MATH_SCHEMA,
"data_table_schema": schema.DATA_TABLE_SCHEMA,
Expand Down
8 changes: 6 additions & 2 deletions src/calliope/attrdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def as_dict_flat(self):
d[k] = self.get_key(k)
return d

def as_yaml_str(self) -> str:
def to_yaml(self, path: str | None = None) -> str:
"""Return a serialised YAML string."""
result = self.copy()
yaml_ = ruamel_yaml.YAML()
Expand Down Expand Up @@ -357,7 +357,11 @@ def as_yaml_str(self) -> str:

stream = io.StringIO()
yaml_.dump(result, stream)
return stream.getvalue()
yaml_str = stream.getvalue()
if path:
with open(path, "w") as f:
f.write(yaml_str)
return yaml_str

def save_yaml(self, path: str) -> None:
"""Save AttrDict as a yaml file.
Expand Down
3 changes: 2 additions & 1 deletion src/calliope/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ def solve(self, force: bool = False, warmstart: bool = False, **kwargs) -> None:
to_drop = []

solve_config = self.config.update({"solve": kwargs}).solve
mode = self.config.build.mode
# FIXME: find a way to avoid overcomplicated passing of settings between modes
mode = self.config.update(self.config.applied_keyword_overrides).build.mode
self._model_data.attrs["timestamp_solve_start"] = log_time(
LOGGER,
self._timings,
Expand Down
12 changes: 4 additions & 8 deletions src/calliope/schemas/config_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,15 @@ def update(self, update_dict: dict, deep: bool = False) -> Self:
self._kwargs = update_dict
return updated

def model_json_schema(self, replace_refs=False) -> AttrDict:
"""Generate an AttrDict with the schema of this class.
Args:
replace_refs (bool, optional): If True, replace $ref/$def for better readability. Defaults to False.
def model_no_ref_schema(self) -> AttrDict:
"""Generate an AttrDict with the schema replacing $ref/$def for better readability.
Returns:
AttrDict: class schema.
"""
schema_dict = AttrDict(super().model_json_schema())
if replace_refs:
schema_dict = AttrDict(jsonref.replace_refs(schema_dict))
schema_dict.del_key("$defs")
schema_dict = AttrDict(jsonref.replace_refs(schema_dict))
schema_dict.del_key("$defs")
return schema_dict

@property
Expand Down

0 comments on commit 3a71e97

Please sign in to comment.