Skip to content

Commit

Permalink
feat(api-raw): raise a 404 Not Found error when a resource is missing…
Browse files Browse the repository at this point in the history
… in the study (#2078)
  • Loading branch information
laurent-laporte-pro authored Jun 27, 2024
1 parent b6d778d commit 5ad8c1d
Show file tree
Hide file tree
Showing 9 changed files with 304 additions and 376 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from antarest.core.utils.utils import StopWatch
from antarest.study.storage.rawstudy.model.filesystem.config.model import FileStudyTreeConfig
from antarest.study.storage.rawstudy.model.filesystem.context import ContextServer
from antarest.study.storage.rawstudy.model.filesystem.folder_node import ChildNotFoundError
from antarest.study.storage.rawstudy.model.filesystem.matrix.matrix import MatrixFrequency, MatrixNode

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -49,23 +50,28 @@ def parse(
try:
# sourcery skip: extract-method
stopwatch = StopWatch()
if self.get_link_path().exists():
link = self.get_link_path().read_text()
link_path = self.get_link_path()
if link_path.exists():
link = link_path.read_text()
matrix_json = self.context.resolver.resolve(link)
matrix_json = cast(JSON, matrix_json)
matrix: pd.DataFrame = pd.DataFrame(
data=matrix_json["data"],
columns=matrix_json["columns"],
index=matrix_json["index"],
)
matrix: pd.DataFrame = pd.DataFrame(**matrix_json)
else:
matrix = pd.read_csv(
file_path,
sep="\t",
dtype=float,
header=None,
float_precision="legacy",
)
try:
matrix = pd.read_csv(
file_path,
sep="\t",
dtype=float,
header=None,
float_precision="legacy",
)
except FileNotFoundError as e:
# Raise 404 'Not Found' if the TSV file is not found
logger.warning(f"Matrix file'{file_path}' not found")
study_id = self.config.study_id
relpath = file_path.relative_to(self.config.study_path).as_posix()
raise ChildNotFoundError(f"File '{relpath}' not found in the study '{study_id}'") from e

stopwatch.log_elapsed(lambda x: logger.info(f"Matrix parsed in {x}s"))
matrix.dropna(how="any", axis=1, inplace=True)
if return_dataframe:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def get_lazy_content(
depth: int = -1,
expanded: bool = False,
) -> str:
# noinspection SpellCheckingInspection
return f"matrixfile://{self.config.path.name}"

def parse_dataframe(
Expand All @@ -58,14 +59,21 @@ def parse_dataframe(
tmp_dir: Any = None,
) -> DataFrame:
file_path = file_path or self.config.path
df = pd.read_csv(
file_path,
sep="\t",
skiprows=4,
header=[0, 1, 2],
na_values="N/A",
float_precision="legacy",
)
try:
df = pd.read_csv(
file_path,
sep="\t",
skiprows=4,
header=[0, 1, 2],
na_values="N/A",
float_precision="legacy",
)
except FileNotFoundError as e:
# Raise 404 'Not Found' if the TSV file is not found
logger.warning(f"Matrix file'{file_path}' not found")
study_id = self.config.study_id
relpath = file_path.relative_to(self.config.study_path).as_posix()
raise ChildNotFoundError(f"File '{relpath}' not found in the study '{study_id}'") from e

if tmp_dir:
tmp_dir.cleanup()
Expand Down
Loading

0 comments on commit 5ad8c1d

Please sign in to comment.