Skip to content

Commit

Permalink
fix(study-filesystem): change JsonReader to return an empty diction…
Browse files Browse the repository at this point in the history
…ary when the file is missing
  • Loading branch information
laurent-laporte-pro committed Mar 9, 2024
1 parent 22aff75 commit 32d6275
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
40 changes: 34 additions & 6 deletions antarest/study/storage/rawstudy/model/filesystem/json_file_node.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
from pathlib import Path
from typing import Any, Dict, Optional, cast
import typing as t

from antarest.core.model import JSON
from antarest.study.storage.rawstudy.ini_reader import IReader
Expand All @@ -11,13 +11,41 @@


class JsonReader(IReader):
def read(self, path: Any) -> JSON:
if isinstance(path, Path):
return cast(JSON, json.loads(path.read_text(encoding="utf-8")))
return cast(JSON, json.loads(path))
"""
JSON file reader.
"""

def read(self, path: t.Any) -> JSON:
content: t.Union[str, bytes]

if isinstance(path, (Path, str)):
try:
with open(path, mode="r", encoding="utf-8") as f:
content = f.read()
except FileNotFoundError:
# If the file is missing, an empty dictionary is returned,
# to mimic the behavior of `configparser.ConfigParser`.
return {}

elif hasattr(path, "read"):
with path:
content = path.read()

else: # pragma: no cover
raise TypeError(repr(type(path)))

try:
return t.cast(JSON, json.loads(content))
except json.JSONDecodeError as exc:
err_msg = f"Failed to parse JSON file '{path}'"
raise ValueError(err_msg) from exc


class JsonWriter(IniWriter):
"""
JSON file writer.
"""

def write(self, data: JSON, path: Path) -> None:
with open(path, "w") as fh:
json.dump(data, fh)
Expand All @@ -28,6 +56,6 @@ def __init__(
self,
context: ContextServer,
config: FileStudyTreeConfig,
types: Optional[Dict[str, Any]] = None,
types: t.Optional[t.Dict[str, t.Any]] = None,
) -> None:
super().__init__(context, config, types, JsonReader(), JsonWriter())
5 changes: 2 additions & 3 deletions tests/storage/repository/filesystem/test_folder_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ def test_get_user_expansion_sensitivity_sensitivity_in(tmp_path: Path) -> None:

# Add the "sensitivity" directory
tmp_path.joinpath("user/expansion/sensitivity").mkdir()
# fixme: we should have `{}`
with pytest.raises(FileNotFoundError, match=r"No such file or directory"):
file_study.tree.get(url)
actual = file_study.tree.get(url)
assert actual == {}

# Add the "sensitivity_in.json" file
sensitivity_obj = {"epsilon": 10000.0, "projection": ["pv", "battery"], "capex": True}
Expand Down

0 comments on commit 32d6275

Please sign in to comment.