Skip to content

Commit

Permalink
refactor(upgrader): eliminated code repetition and removed unused imp…
Browse files Browse the repository at this point in the history
…orts
  • Loading branch information
laurent-laporte-pro committed Apr 16, 2024
1 parent 0a45825 commit 89600bc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
16 changes: 10 additions & 6 deletions antarest/study/storage/study_upgrader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from pathlib import Path

from antarest.core.exceptions import StudyValidationError

from .upgrader_710 import upgrade_710
from .upgrader_720 import upgrade_720
from .upgrader_800 import upgrade_800
Expand All @@ -22,6 +21,11 @@
from .upgrader_870 import upgrade_870
from .upgrader_880 import upgrade_880

STUDY_ANTARES = "study.antares"
"""
Main file of an Antares study containing the caption, the version, the creation date, etc.
"""

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -106,7 +110,7 @@ def get_current_version(study_path: Path) -> str:
`study.antares` file or does not match the expected format.
"""

antares_path = study_path / "study.antares"
antares_path = study_path / STUDY_ANTARES
pattern = r"version\s*=\s*([\w.-]+)\s*"
with antares_path.open(encoding="utf-8") as lines:
for line in lines:
Expand Down Expand Up @@ -164,8 +168,8 @@ def can_upgrade_version(from_version: str, to_version: str) -> t.List[Path]:


def _update_study_antares_file(target_version: str, study_path: Path) -> None:
file = study_path / "study.antares"
content = file.read_text(encoding="utf-8")
antares_path = study_path / STUDY_ANTARES
content = antares_path.read_text(encoding="utf-8")
content = re.sub(
r"^version\s*=.*$",
f"version = {target_version}",
Expand All @@ -178,7 +182,7 @@ def _update_study_antares_file(target_version: str, study_path: Path) -> None:
content,
flags=re.MULTILINE,
)
file.write_text(content, encoding="utf-8")
antares_path.write_text(content, encoding="utf-8")


def _copies_only_necessary_files(files_to_upgrade: t.List[Path], study_path: Path, tmp_path: Path) -> t.List[Path]:
Expand All @@ -193,7 +197,7 @@ def _copies_only_necessary_files(files_to_upgrade: t.List[Path], study_path: Pat
without any children that has parents already in the list.
"""
files_to_copy = _filters_out_children_files(files_to_upgrade)
files_to_copy.append(Path("study.antares"))
files_to_copy.append(Path(STUDY_ANTARES))
files_to_retrieve = []
for path in files_to_copy:
entire_path = study_path / path
Expand Down
5 changes: 0 additions & 5 deletions antarest/study/storage/study_upgrader/upgrader_880.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import glob
import typing as t
from pathlib import Path

import numpy as np
import numpy.typing as npt
import pandas as pd

from antarest.study.storage.rawstudy.ini_reader import IniReader
from antarest.study.storage.rawstudy.ini_writer import IniWriter
from antarest.study.storage.rawstudy.model.filesystem.root.settings.generaldata import DUPLICATE_KEYS
Expand Down
7 changes: 6 additions & 1 deletion tests/integration/study_data_blueprint/test_st_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ class TestSTStorage:
@pytest.mark.parametrize("study_type", ["raw", "variant"])
@pytest.mark.parametrize("study_version", [860, 880])
def test_lifecycle__nominal(
self, client: TestClient, user_access_token: str, study_id: str, study_type: str, study_version: int
self,
client: TestClient,
user_access_token: str,
study_id: str,
study_type: str,
study_version: int,
) -> None:
"""
The purpose of this integration test is to test the endpoints
Expand Down

0 comments on commit 89600bc

Please sign in to comment.