Skip to content

Commit

Permalink
fix(api): add documentation (#1646)
Browse files Browse the repository at this point in the history
  • Loading branch information
TLAIDI authored and laurent-laporte-pro committed Sep 12, 2023
1 parent f89c1c4 commit c7ab476
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 52 deletions.
16 changes: 14 additions & 2 deletions antarest/study/common/studystorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,13 @@ def unarchive_study_output(self, study: T, output_id: str, keep_src_zip: bool) -
raise NotImplementedError()

def unarchive(self, study: T) -> None:
"""
Unarchived a study
Args:
study: StudyFactory
"""
raise NotImplementedError()

# def export_study_flat(self, **kwargs) -> None:
# raise NotImplementedError()
def export_study_flat(
self,
path_study: Path,
Expand All @@ -267,4 +270,13 @@ def export_study_flat(
output_src_path: Optional[Path] = None,
output_list_filter: Optional[List[str]] = None,
) -> None:
"""
Export study to destination
Args:
path_study: source path.
dst_path: destination path.
outputs: list of outputs to keep.
output_src_path: list of source outputs path
output_list_filter:list of outputs to keep
"""
raise NotImplementedError()
6 changes: 3 additions & 3 deletions antarest/study/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import io
import json
import logging
import shutil
import os
import shutil
from datetime import datetime, timedelta
from http import HTTPStatus
from pathlib import Path
Expand Down Expand Up @@ -88,6 +88,7 @@
StudySimResultDTO,
)
from antarest.study.repository import StudyMetadataRepository
from antarest.study.storage.abstract_storage_service import export_study_flat
from antarest.study.storage.rawstudy.model.filesystem.config.model import FileStudyTreeConfigDTO
from antarest.study.storage.rawstudy.model.filesystem.folder_node import ChildNotFoundError
from antarest.study.storage.rawstudy.model.filesystem.ini_file_node import IniFileNode
Expand All @@ -107,7 +108,6 @@
remove_from_cache,
study_matcher,
)
from antarest.study.storage.abstract_storage_service import export_study_flat
from antarest.study.storage.variantstudy.model.command.icommand import ICommand
from antarest.study.storage.variantstudy.model.command.replace_matrix import ReplaceMatrix
from antarest.study.storage.variantstudy.model.command.update_comments import UpdateComments
Expand Down Expand Up @@ -1883,7 +1883,7 @@ def unarchive(self, uuid: str, params: RequestParameters) -> str:

def unarchive_task(notifier: TaskUpdateNotifier) -> TaskResult:
study_to_archive = self.get_study(uuid)
self.storage_service.raw_study_service.unarchive(study_to_archive)
self.storage_service.raw_study_service.unarchived(study_to_archive)
study_to_archive.archived = False

os.unlink(self.storage_service.raw_study_service.get_archive_path(study_to_archive))
Expand Down
35 changes: 17 additions & 18 deletions antarest/study/storage/abstract_storage_service.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import functools
import logging
import os
import shutil
import tempfile
import time
import zipfile
from abc import ABC
from pathlib import Path
from typing import IO, List, Optional, Union
from typing import IO, List, Optional, Union, Sequence
from uuid import uuid4
import time
from zipfile import ZipFile
import os


from antarest.core.config import Config
from antarest.core.exceptions import BadOutputError, StudyOutputNotFoundError
from antarest.core.interfaces.cache import CacheConstants, ICache
from antarest.core.model import JSON
from antarest.core.utils.utils import StopWatch, assert_this, extract_zip, unzip, zip_dir
from antarest.core.utils.utils import StopWatch, extract_zip, unzip, zip_dir
from antarest.study.common.studystorage import IStudyStorageService, T
from antarest.study.common.utils import get_study_information
from antarest.study.model import (
PatchOutputs,
PatchStudy,
RawStudy,
StudyAdditionalData,
StudyMetadataDTO,
StudyMetadataPatchDTO,
Expand All @@ -36,6 +37,11 @@
logger = logging.getLogger(__name__)


# noinspection PyUnusedLocal
def _ignore_patterns(path_study: Path, directory: str, contents: Sequence[str]) -> Sequence[str]:
return ["output"] if Path(directory) == path_study else []


def export_study_flat(
path_study: Path,
dest: Path,
Expand All @@ -49,21 +55,16 @@ def export_study_flat(
Args:
path_study: Study source path
dest: Destination path.
study_factory: StudyFactory,
outputs: List of outputs to keep.
output_list_filter: List of outputs to keep (None indicate all outputs).
output_src_path: Denormalize the study (replace matrix links by real matrices).
output_src_path: list of source outputs path.
"""
start_time = time.time()
output_src_path = output_src_path or path_study / "output"
output_dest_path = dest / "output"
ignore_patterns = (
lambda directory, contents: ["output"]
if str(directory) == str(path_study)
else []
)

ignore_patterns = functools.partial(_ignore_patterns, path_study)
# noinspection PyTypeChecker
shutil.copytree(src=path_study, dst=dest, ignore=ignore_patterns)
if outputs and output_src_path.is_dir():
if output_dest_path.exists():
Expand All @@ -73,7 +74,7 @@ def export_study_flat(
for output in output_list_filter:
zip_path = output_src_path / f"{output}.zip"
if zip_path.exists():
with ZipFile(zip_path) as zf:
with zipfile.ZipFile(zip_path) as zf:
zf.extractall(output_dest_path / output)
else:
shutil.copytree(
Expand Down Expand Up @@ -304,9 +305,7 @@ def export_study(self, metadata: T, target: Path, outputs: bool = True) -> Path:
outputs=outputs,
output_src_path=output_src_path,
)
study = self.study_factory.create_from_fs(
tmp_study_path, "", use_cache=False
)
study = self.study_factory.create_from_fs(tmp_study_path, "", use_cache=False)
study.tree.denormalize()
stopwatch = StopWatch()
zip_dir(tmp_study_path, target)
Expand Down
6 changes: 2 additions & 4 deletions antarest/study/storage/rawstudy/raw_study_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,7 @@ def export_study_flat(
output_list_filter=output_list_filter,
output_src_path=output_src_path,
)
study = self.study_factory.create_from_fs(
dst_path, "", use_cache=False
)
study = self.study_factory.create_from_fs(dst_path, "", use_cache=False)
study.tree.denormalize()

def check_errors(
Expand Down Expand Up @@ -370,7 +368,7 @@ def archive(self, study: RawStudy) -> Path:
self.cache.invalidate(study.id)
return new_study_path

def unarchive(self, study: RawStudy) -> None:
def unarchived(self, study: RawStudy) -> None:
with open(
self.get_archive_path(study),
"rb",
Expand Down
28 changes: 3 additions & 25 deletions antarest/study/storage/variantstudy/variant_study_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@
from antarest.study.storage.rawstudy.model.filesystem.config.model import FileStudyTreeConfig, FileStudyTreeConfigDTO
from antarest.study.storage.rawstudy.model.filesystem.factory import FileStudy, StudyFactory
from antarest.study.storage.rawstudy.raw_study_service import RawStudyService
from antarest.study.storage.utils import (
assert_permission,
get_default_workspace_path,
is_managed,
remove_from_cache,
)
from antarest.study.storage.utils import assert_permission, get_default_workspace_path, is_managed, remove_from_cache
from antarest.study.storage.variantstudy.business.utils import transform_command_to_dto
from antarest.study.storage.variantstudy.command_factory import CommandFactory
from antarest.study.storage.variantstudy.model.command.icommand import ICommand
Expand Down Expand Up @@ -719,21 +714,6 @@ def _generate(

if last_executed_command_index is None:
if isinstance(parent_study, VariantStudy):
# self._safe_generation(parent_study)
# self.export_study_flat(
# metadata=parent_study,
# dst_path=dst_path,
# outputs=False,
# denormalize=False,
# )
# else:
# self.raw_study_service.export_study_flat(
# metadata=parent_study,
# dst_path=dst_path,
# outputs=False,
# denormalize=False,
# )

self._safe_generation(parent_study)
path_study = Path(parent_study.path)
snapshot_path = path_study / SNAPSHOT_RELATIVE_PATH
Expand All @@ -747,7 +727,7 @@ def _generate(
else:
path_study = Path(parent_study.path)
if parent_study.archived:
self.raw_study_service.unarchive(parent_study)
self.raw_study_service.unarchived(parent_study)
try:
self.raw_study_service.export_study_flat(
path_study=path_study,
Expand Down Expand Up @@ -1105,9 +1085,7 @@ def export_study_flat(
output_src_path=output_src_path,
output_list_filter=output_list_filter,
)
study = self.study_factory.create_from_fs(
dst_path, "", use_cache=False
)
study = self.study_factory.create_from_fs(dst_path, "", use_cache=False)
study.tree.denormalize()

def get_synthesis(
Expand Down

0 comments on commit c7ab476

Please sign in to comment.