Skip to content
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
29 changes: 18 additions & 11 deletions skore-local-project/src/skore_local_project/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import io
import os
from functools import wraps
from operator import itemgetter
from pathlib import Path
from types import SimpleNamespace
Expand Down Expand Up @@ -35,6 +36,21 @@ class Metadata(TypedDict): # noqa: D101
predict_time: float


def ensure_project_is_not_deleted(method):
"""Ensure project is not deleted, before executing any other operation."""

@wraps(method)
def wrapper(self, *args, **kwargs):
if self.name not in self._Project__projects_storage:
raise RuntimeError(
f"Skore could not proceed because {repr(self)} does not exist anymore."
)

return method(self, *args, **kwargs)

return wrapper


class Project:
r"""
API to manage a collection of key-report pairs persisted in a local storage.
Expand Down Expand Up @@ -168,6 +184,7 @@ def pickle(report: EstimatorReport | CrossValidationReport) -> tuple[str, bytes]

return pickle_hash, pickle_bytes

@ensure_project_is_not_deleted
def put(self, key: str, report: EstimatorReport | CrossValidationReport):
"""
Put a key-report pair to the local project.
Expand Down Expand Up @@ -202,12 +219,6 @@ def put(self, key: str, report: EstimatorReport | CrossValidationReport):
f"ort` (found '{type(report)}')"
)

if self.name not in self.__projects_storage:
raise RuntimeError(
f"Bad condition: {repr(self)} does not exist anymore, "
f"it had to be removed."
)

pickle_hash, pickle_bytes = Project.pickle(report)

if pickle_hash not in self.__artifacts_storage:
Expand All @@ -224,13 +235,9 @@ def put(self, key: str, report: EstimatorReport | CrossValidationReport):
)

@property
@ensure_project_is_not_deleted
def reports(self):
"""Accessor for interaction with the persisted reports."""
if self.name not in self.__projects_storage:
raise RuntimeError(
f"Bad condition: {repr(self)} does not exist anymore, "
f"it had to be removed."
)

def get(id: str) -> EstimatorReport:
"""Get a persisted report by its id."""
Expand Down
10 changes: 6 additions & 4 deletions skore-local-project/tests/unit/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ def test_put_exception(self, tmp_path, regression):
with raises(
RuntimeError,
match=re.escape(
f"Bad condition: {repr(project)} does not exist anymore, "
f"it had to be removed.",
f"Skore could not proceed because "
f"Project(mode='local', name='<project>', workspace='{tmp_path}') "
f"does not exist anymore."
),
):
project.put("<key>", regression)
Expand Down Expand Up @@ -296,8 +297,9 @@ def test_reports_exception(self, tmp_path):
with raises(
RuntimeError,
match=re.escape(
f"Bad condition: {repr(project)} does not exist anymore, "
f"it had to be removed.",
f"Skore could not proceed because "
f"Project(mode='local', name='<project>', workspace='{tmp_path}') "
f"does not exist anymore."
),
):
project.reports # noqa: B018
Expand Down
Loading