Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api-raw): raise a 404 Not Found error when a resource is missing in the study #2078

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading