Skip to content

Commit

Permalink
Handle pathlib.Path in Attrdict.from_yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
brynpickering committed Oct 25, 2023
1 parent 3af4201 commit 228049e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/calliope/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def new(path, template, debug):
with format_exceptions(debug):
if template is None:
template = "national_scale"
source_path = examples._PATHS[template]
source_path = examples.EXAMPLE_MODEL_DIR / template
click.echo("Copying {} template to target directory: {}".format(template, path))
shutil.copytree(source_path, path)

Expand Down
2 changes: 1 addition & 1 deletion src/calliope/core/attrdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def from_yaml(cls, f, resolve_imports=True):
overrides definitions in the imported file.
"""
if isinstance(f, str) or isinstance(f, Path):
if isinstance(f, (str, Path)):
with open(
f,
"r",
Expand Down
8 changes: 4 additions & 4 deletions src/calliope/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Model(object):

def __init__(
self,
model_definition: Optional[Union[str, dict]] = None,
model_definition: Optional[Union[str, Path, dict]] = None,
model_data: Optional[xarray.Dataset] = None,
debug: bool = False,
scenario: Optional[str] = None,
Expand All @@ -71,7 +71,7 @@ def __init__(
configuration file or a dict fully specifying the model.
Args:
config (Optional[Union[str, dict]]):
config (Optional[Union[str, Path, dict]]):
If str, must be the path to a model configuration file.
If dict or AttrDict, must fully specify the model.
model_data (Optional[xarray.Dataset], optional):
Expand Down Expand Up @@ -104,8 +104,8 @@ def __init__(
# try to set logging output format assuming python interactive. Will
# use CLI logging format if model called from CLI
log_time(LOGGER, self._timings, "model_creation", comment="Model: initialising")
if isinstance(model_definition, str):
self._model_def_path = model_definition
if isinstance(model_definition, (str, Path)):
self._model_def_path = Path(model_definition).as_posix()
model_run, debug_data = model_run_from_yaml(
model_definition,
scenario,
Expand Down
3 changes: 2 additions & 1 deletion src/calliope/core/util/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import importlib
import os
import sys
from pathlib import Path
from typing import TypeVar

import jsonschema
Expand Down Expand Up @@ -67,7 +68,7 @@ def relative_path(base_path_file, path):
"""
# Check if base_path_file is a string because it might be an AttrDict
if not os.path.isabs(path) and isinstance(base_path_file, str):
if not os.path.isabs(path) and isinstance(base_path_file, (str, Path)):
path = os.path.join(os.path.dirname(base_path_file), path)
return path

Expand Down
3 changes: 2 additions & 1 deletion src/calliope/preprocess/model_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import itertools
import logging
import os
from pathlib import Path
from typing import Optional

import pandas as pd
Expand Down Expand Up @@ -64,7 +65,7 @@ def model_run_from_yaml(
"""
config = AttrDict.from_yaml(model_file)
config._model_def_path = model_file
config._model_def_path = Path(model_file).as_posix()

config_with_overrides, debug_comments, overrides, scenario = apply_overrides(
config, scenario=scenario, override_dict=override_dict
Expand Down
12 changes: 7 additions & 5 deletions tests/test_core_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -1479,18 +1479,20 @@ def model_national(self, load_timeseries_from_dataframes):
"""
if load_timeseries_from_dataframes:
# Create dictionary with dataframes
timeseries_data_path = os.path.join(
calliope.examples._PATHS["national_scale"], "timeseries_data/"
timeseries_data_path = (
calliope.examples.EXAMPLE_MODEL_DIR
/ "national_scale"
/ "timeseries_data"
)
timeseries_dataframes = {}
timeseries_dataframes["csp_resource"] = pd.read_csv(
os.path.join(timeseries_data_path, "csp_resource.csv"), index_col=0
timeseries_data_path / "csp_resource.csv", index_col=0
)
timeseries_dataframes["demand_1"] = pd.read_csv(
os.path.join(timeseries_data_path, "demand-1.csv"), index_col=0
timeseries_data_path / "demand-1.csv", index_col=0
)
timeseries_dataframes["demand_2"] = pd.read_csv(
os.path.join(timeseries_data_path, "demand-2.csv"), index_col=0
timeseries_data_path / "demand-2.csv", index_col=0
)
# Create override dict telling calliope to load timeseries from df
override_dict = {
Expand Down

0 comments on commit 228049e

Please sign in to comment.