Skip to content

Commit

Permalink
Merge pull request #543 from AntaresSimulatorTeam/release/handle_play…
Browse files Browse the repository at this point in the history
…list_properly

Playlist properly handled even with usage of ConfigParser
  • Loading branch information
JasonMarechal25 authored Sep 30, 2022
2 parents beeab32 + 3e2b2dc commit 3c009a4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
30 changes: 28 additions & 2 deletions src/python/antares_xpansion/general_data_processor.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import configparser
import os
import shutil
from pathlib import Path

from antares_xpansion.flushed_print import flushed_print
import configparser
from antares_xpansion.general_data_reader import GeneralDataIniReader


class GeneralDataFileExceptions:
class GeneralDataFileNotFound(Exception):
Expand Down Expand Up @@ -37,15 +40,38 @@ def get_general_data_ini_file(self) -> Path:

def change_general_data_file_to_configure_antares_execution(self):
flushed_print("-- pre antares")
config = configparser.ConfigParser()
ini_file_backup = self._general_data_ini_file.with_suffix(self._general_data_ini_file.suffix + ".with-playlist")
shutil.copyfile(self._general_data_ini_file, ini_file_backup)
config = configparser.ConfigParser(strict=False)
config.read(self._general_data_ini_file)
value_to_change = self._get_values_to_change_general_data_file()
for (section, key) in value_to_change:
if not config.has_section(section):
config.add_section(section)
config.set(section, key, value_to_change[(section, key)])
with open(self._general_data_ini_file, "w") as writer:
has_playlist = config.has_section("playlist")
if has_playlist:
playlist_options = dict(config.items("playlist"))
config.remove_section("playlist")
config.write(writer)
if has_playlist:
self.backport_playlist(ini_file_backup, writer, playlist_options)
os.remove(ini_file_backup)

def backport_playlist(self, ini_file_backup, writer, playlist_options: dict):
ini_reader = GeneralDataIniReader(ini_file_backup)
ini_reader.get_active_years()
active_years = ini_reader.get_raw_active_years()
inactive_years = ini_reader.get_raw_inactive_years()
writer.write("[playlist]\n")
for option in playlist_options:
if option != "playlist_year +" and option != "playlist_year -":
writer.write(f"{option} = {playlist_options[option]}\n")
for year in active_years:
writer.write(f"playlist_year + = {year}\n")
for year in inactive_years:
writer.write(f"playlist_year - = {year}\n")

general_data_ini_file = property(
get_general_data_ini_file, set_general_data_ini_file
Expand Down
8 changes: 7 additions & 1 deletion src/python/antares_xpansion/general_data_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ def get_active_years(self):
self._set_playlist_year_lists()
return self._get_active_years()
else:
return list(range(1, self._mc_years+1))
return list(range(1, self._mc_years + 1))

def get_raw_active_years(self):
return self._active_year_list

def get_raw_inactive_years(self):
return self._inactive_year_list

def _get_active_years(self):
if self._playlist_reset_option is True:
Expand Down
44 changes: 39 additions & 5 deletions tests/python/test_antares_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
GeneralDataFileExceptions,
GeneralDataProcessor,
)
from antares_xpansion.general_data_reader import IniReader
from antares_xpansion.general_data_reader import IniReader, GeneralDataIniReader

from tests.build_config_reader import get_antares_solver_path

Expand All @@ -35,17 +35,51 @@ def test_general_data_file_path(self, tmp_path):
gen_data_proc = GeneralDataProcessor(settings_dir, True)
assert gen_data_proc.general_data_ini_file == expected_path

def test_no_changes_in_empty_file(self, tmp_path):
def test_preserve_playlist(self, tmp_path):
settings_dir = TestGeneralDataProcessor.get_settings_dir(tmp_path)
settings_dir.mkdir()
gen_data_path = settings_dir / self.generaldata_filename

with open(gen_data_path, "w") as writer:
writer.write("[general]\nnbyears=2\nuser-playlist = true\n")
writer.write("[playlist]\n")
writer.write("dummy_entry = value\n")
writer.write("playlist_year + = 1\n")
writer.write("playlist_year + = 42\n")
writer.write("playlist_year - = 5\n")
writer.write("playlist_year - = 0\n")

gen_data_proc = GeneralDataProcessor(settings_dir, True)

gen_data_proc.change_general_data_file_to_configure_antares_execution()
general_data_ini_file = gen_data_proc.general_data_ini_file

xpansion_ini_reader = GeneralDataIniReader(general_data_ini_file)
xpansion_ini_reader.get_active_years()
config_reader = configparser.ConfigParser(strict=False)
config_reader.read(gen_data_path)
active_years = xpansion_ini_reader.get_raw_active_years()
inactive_years = xpansion_ini_reader.get_raw_inactive_years()
assert config_reader.get("playlist", "dummy_entry") == "value"
assert active_years == [1, 42]
assert inactive_years == [5, 0]

def test_empty_file_should_be_populated_by_default_values(self, tmp_path):
settings_dir = TestGeneralDataProcessor.get_settings_dir(tmp_path)
settings_dir.mkdir()
gen_data_path = settings_dir / self.generaldata_filename
gen_data_path.touch()
gen_data_proc = GeneralDataProcessor(settings_dir, True)
gen_data_proc.change_general_data_file_to_configure_antares_execution()

with open(gen_data_proc.general_data_ini_file, "r") as reader:
lines = reader.readlines()

assert len(lines) == 0
assert len(lines) != 0
parser = configparser.ConfigParser()
parser.read(gen_data_proc.general_data_ini_file)
for option in gen_data_proc._get_values_to_change_general_data_file():
assert parser.get(option[0], option[1]) is not None

def test_values_change_in_general_file_accurate_mode(self, tmp_path):

Expand Down Expand Up @@ -137,7 +171,7 @@ def test_values_change_in_general_file_fast_mode(self, tmp_path):

general_data_path.write_text(default_val)

#Removing '[' and ']' from sections name
# Removing '[' and ']' from sections name
optimization = "optimization"
general = "general"
random_section = "random_section"
Expand All @@ -164,7 +198,7 @@ def test_values_change_in_general_file_fast_mode(self, tmp_path):
)

def verify_that_general_data_contains_expected_vals(
self, general_data_ini_file, expected_val
self, general_data_ini_file, expected_val
):
actual_config = configparser.ConfigParser()
actual_config.read(general_data_ini_file)
Expand Down

0 comments on commit 3c009a4

Please sign in to comment.