From c7ab476249717767ab693874d07069c2ecc6d150 Mon Sep 17 00:00:00 2001 From: TLAIDI Date: Tue, 1 Aug 2023 14:19:03 +0200 Subject: [PATCH] fix(api): add documentation (#1646) --- antarest/study/common/studystorage.py | 16 +++++++-- antarest/study/service.py | 6 ++-- .../study/storage/abstract_storage_service.py | 35 +++++++++---------- .../storage/rawstudy/raw_study_service.py | 6 ++-- .../variantstudy/variant_study_service.py | 28 ++------------- 5 files changed, 39 insertions(+), 52 deletions(-) diff --git a/antarest/study/common/studystorage.py b/antarest/study/common/studystorage.py index 1eca3a0f1d..552697b25d 100644 --- a/antarest/study/common/studystorage.py +++ b/antarest/study/common/studystorage.py @@ -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, @@ -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() diff --git a/antarest/study/service.py b/antarest/study/service.py index 756954e51a..fb487cb2c8 100644 --- a/antarest/study/service.py +++ b/antarest/study/service.py @@ -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 @@ -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 @@ -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 @@ -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)) diff --git a/antarest/study/storage/abstract_storage_service.py b/antarest/study/storage/abstract_storage_service.py index fb11a5bf50..6fbe0c0a7f 100644 --- a/antarest/study/storage/abstract_storage_service.py +++ b/antarest/study/storage/abstract_storage_service.py @@ -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, @@ -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, @@ -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(): @@ -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( @@ -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) diff --git a/antarest/study/storage/rawstudy/raw_study_service.py b/antarest/study/storage/rawstudy/raw_study_service.py index e8946b4fca..c4552cc350 100644 --- a/antarest/study/storage/rawstudy/raw_study_service.py +++ b/antarest/study/storage/rawstudy/raw_study_service.py @@ -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( @@ -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", diff --git a/antarest/study/storage/variantstudy/variant_study_service.py b/antarest/study/storage/variantstudy/variant_study_service.py index 5ecc992900..cc18c03bb7 100644 --- a/antarest/study/storage/variantstudy/variant_study_service.py +++ b/antarest/study/storage/variantstudy/variant_study_service.py @@ -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 @@ -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 @@ -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, @@ -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(