From 3eeb0adfaa227644a2a8eb877000ce273e20361a Mon Sep 17 00:00:00 2001 From: killian-scalian Date: Wed, 18 Dec 2024 15:23:40 +0100 Subject: [PATCH] mypy --- requirements.txt | 2 +- .../input_converter/src/converter.py | 7 +- src/andromede/input_converter/src/main.py | 164 ------------------ src/andromede/input_converter/src/utils.py | 30 ++-- tests/input_converter/test_converter.py | 9 +- 5 files changed, 24 insertions(+), 188 deletions(-) delete mode 100644 src/andromede/input_converter/src/main.py diff --git a/requirements.txt b/requirements.txt index 16f1ef6..bc68a1c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,4 @@ scipy==1.10.1 antlr4-python3-runtime==4.13.1 PyYAML~=6.0.1 pydantic~=2.6.1 -antares>=0.1.1 \ No newline at end of file +antares>=0.3.25 \ No newline at end of file diff --git a/src/andromede/input_converter/src/converter.py b/src/andromede/input_converter/src/converter.py index f970343..14c50cb 100644 --- a/src/andromede/input_converter/src/converter.py +++ b/src/andromede/input_converter/src/converter.py @@ -10,7 +10,7 @@ # # This file is part of the Antares project. from pathlib import Path -from antares.model.study import Study +from antares.model.study import Study # type: ignore from typing import Optional from pydantic import BaseModel from andromede.study.parsing import InputComponents @@ -30,9 +30,10 @@ def __init__(self, study_path: Optional[Path]): self.study_path = resolve_path(study_path) if study_path else None self.study: Study = None - def convert_study_to_input_components(self) -> BaseModel: + def convert_study_to_input_components(self) -> InputComponents: areas = self.study.read_areas() - return convert_area_to_components(areas) + area_components = convert_area_to_components(areas) + return InputComponents(nodes=area_components) def validate_with_pydantic( self, data: dict, model_class: type[BaseModel] diff --git a/src/andromede/input_converter/src/main.py b/src/andromede/input_converter/src/main.py deleted file mode 100644 index 3966f0b..0000000 --- a/src/andromede/input_converter/src/main.py +++ /dev/null @@ -1,164 +0,0 @@ -# Copyright (c) 2024, RTE (https://www.rte-france.com) -# -# See AUTHORS.txt -# -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. -# -# SPDX-License-Identifier: MPL-2.0 -# -# This file is part of the Antares project. - -import argparse -import configparser -from pathlib import Path -import os -from andromede.input_converter.src import __version__ -import sys -from andromede.input_converter.src.converter import StudyConverter - -DEFAULT: dict = {} - - -class PathType: - """file or directory path type for `argparse` parser - - The `PathType` class represents a type of argument that can be used - with the `argparse` library. - This class takes three boolean arguments, `exists`, `file_ok`, and `dir_ok`, - which specify whether the path argument must exist, whether it can be a file, - and whether it can be a directory, respectively. - - Example Usage:: - - import argparse - from antarest.main import PathType - - parser = argparse.ArgumentParser() - parser.add_argument("--input", type=PathType(file_ok=True, exists=True)) - args = parser.parse_args() - - print(args.input) - - In the above example, `PathType` is used to specify the type of the `--input` - argument for the `argparse` parser. The argument must be an existing file path. - If the given path is not an existing file, the argparse library raises an error. - The Path object representing the given path is then printed to the console. - """ - - def __init__( - self, - exists: bool = False, - file_ok: bool = False, - dir_ok: bool = False, - ) -> None: - if not (file_ok or dir_ok): - msg = "Either `file_ok` or `dir_ok` must be set at a minimum." - raise ValueError(msg) - self.exists = exists - self.file_ok = file_ok - self.dir_ok = dir_ok - - def __call__(self, string: str) -> Path: - """ - Check whether the given string represents a valid path. - - If `exists` is `False`, the method simply returns the given path. - If `exists` is True, it checks whether the path exists and whether it is - a file or a directory, depending on the values of `file_ok` and `dir_ok`. - If the path exists and is of the correct type, the method returns the path; - otherwise, it raises an :class:`argparse.ArgumentTypeError` with an - appropriate error message. - - Args: - string: file or directory path - - Returns: - the file or directory path - - Raises - argparse.ArgumentTypeError: if the path is invalid - """ - file_path = Path(string).expanduser() - if not self.exists: - return file_path - if self.file_ok and self.dir_ok: - if file_path.exists(): - return file_path - msg = f"The file or directory path does not exist: '{file_path}'" - raise argparse.ArgumentTypeError(msg) - elif self.file_ok: - if file_path.is_file(): - return file_path - elif file_path.exists(): - msg = f"The path is not a regular file: '{file_path}'" - else: - msg = f"The file path does not exist: '{file_path}'" - raise argparse.ArgumentTypeError(msg) - elif self.dir_ok: - if file_path.is_dir(): - return file_path - elif file_path.exists(): - msg = f"The path is not a directory: '{file_path}'" - else: - msg = f"The directory path does not exist: '{file_path}'" - raise argparse.ArgumentTypeError(msg) - else: # pragma: no cover - raise NotImplementedError((self.file_ok, self.dir_ok)) - - -def parse_commandline() -> argparse.Namespace: - # Parse command-line arguments using argparse to specify configuration file paths, - # logging options, and version display. Returns the parsed arguments. - parser = argparse.ArgumentParser() - parser.add_argument( - "-c", - "--conf", - type=PathType(exists=True, file_ok=True), - default="../data/config.ini", - ) - parser.add_argument( - "-v", - "--version", - action="version", - help="Display the server version and exit", - version=__version__, - ) - return parser.parse_args() - - -def handle_env(config_parser: configparser.ConfigParser) -> dict: - # Read configuration items from the config parser and set them as environment variables. - # Return a dictionary of the environment variables that were set. - config_env = {} - for i, j in config_parser.items(): - for k, value in config_parser.items(i): - env_name = i + "_" + k - if not os.environ.get(env_name): - os.environ[env_name] = value - config_env.update({env_name: value}) - else: - config_env.update({env_name: os.environ.get(env_name, "")}) - return config_env - - -if __name__ == "__main__": - # Main entry point of the script. - config: dict = {} - args = parse_commandline() - config_parser = configparser.ConfigParser() - - # Load the default configuration dictionary into the config parser. - config_parser.read_dict(DEFAULT) - if args.conf: - # Check if the specified config file exists, if not, exit with an error message. - if not os.path.exists(args.conf): - sys.exit(f"Aborting: missing config file at {args.conf}") - else: - os.environ["CONFIG"] = str(os.path.abspath(args.conf)) - config_parser.read(args.conf) - - env_cfg = handle_env(config_parser) - processor = StudyConverter(study_path=None) - processor.process_all() diff --git a/src/andromede/input_converter/src/utils.py b/src/andromede/input_converter/src/utils.py index ab77fe2..e3211f5 100644 --- a/src/andromede/input_converter/src/utils.py +++ b/src/andromede/input_converter/src/utils.py @@ -1,5 +1,5 @@ from pathlib import Path -from antares.model.area import Area +from antares.model.area import Area # type: ignore from andromede.study.parsing import ( InputComponent, InputComponents, @@ -17,15 +17,11 @@ def resolve_path(path_str: Path) -> Path: return absolute_path -def convert_area_to_components(areas: list[Area]) -> BaseModel: - return InputComponents( - nodes=[InputComponent(id=area.id, model="area") for area in areas], - components=[], - connections=[], - ) +def convert_area_to_components(areas: list[Area]) -> list[InputComponent]: + return [InputComponent(id=area.id, model="area") for area in areas] -def convert_renewable_to_components(area: Area) -> list: +def convert_renewable_to_components(area: Area) -> list[InputComponent]: renewables = area.read_renewables() return [ InputComponent( @@ -44,7 +40,7 @@ def convert_renewable_to_components(area: Area) -> list: ), InputComponentParameter( name=renewable.id, - type="timeserie", + type="timeseries", timeseries=str(renewable.get_timeseries()), ), ], @@ -53,33 +49,33 @@ def convert_renewable_to_components(area: Area) -> list: ] -def convert_hydro_to_components(area: Area) -> list: +def convert_hydro_to_components(area: Area) -> list[InputComponent]: raise NotImplementedError -def convert_st_storages_to_components(area: Area) -> list: +def convert_st_storages_to_components(area: Area) -> list[InputComponent]: raise NotImplementedError -def convert_thermals_to_components(area: Area) -> list: +def convert_thermals_to_components(area: Area) -> list[InputComponent]: raise NotImplementedError -def convert_load_matrix_to_components(area: Area) -> list: +def convert_load_matrix_to_components(area: Area) -> list[InputComponent]: raise NotImplementedError -def convert_misc_gen_to_components(area: Area) -> list: +def convert_misc_gen_to_components(area: Area) -> list[InputComponent]: raise NotImplementedError -def convert_reserves_matrix_to_components(area: Area) -> list: +def convert_reserves_matrix_to_components(area: Area) -> list[InputComponent]: raise NotImplementedError -def convert_wind_matrix_to_components(area: Area) -> list: +def convert_wind_matrix_to_components(area: Area) -> list[InputComponent]: raise NotImplementedError -def convert_solar_matrix_to_components(area: Area) -> list: +def convert_solar_matrix_to_components(area: Area) -> list[InputComponent]: raise NotImplementedError diff --git a/tests/input_converter/test_converter.py b/tests/input_converter/test_converter.py index 535e8d1..665cc5e 100644 --- a/tests/input_converter/test_converter.py +++ b/tests/input_converter/test_converter.py @@ -11,14 +11,16 @@ # This file is part of the Antares project. -from andromede.input_converter.src.utils import convert_area_to_components +from andromede.input_converter.src.utils import convert_area_to_components +from andromede.input_converter.src.converter import StudyConverter from andromede.study.parsing import InputComponent, InputComponents class TestConverter: def test_convert_area_to_input_components(self, local_study_w_areas): - areas = local_study_w_areas.read_areas() - area_components = convert_area_to_components(areas) + converter = StudyConverter(study_path=None) + converter.study = local_study_w_areas + area_components = converter.convert_study_to_input_components() expected_area_components = InputComponents( nodes=[ InputComponent(id="it", model="area", parameters=None), @@ -27,4 +29,5 @@ def test_convert_area_to_input_components(self, local_study_w_areas): components=[], connections=[], ) + assert area_components == expected_area_components