Skip to content

Commit

Permalink
Merge pull request #681 from AntaresSimulatorTeam/feature/candidate-p…
Browse files Browse the repository at this point in the history
…arsing-issues

candidates parsing issues
  • Loading branch information
JasonMarechal25 authored Oct 31, 2023
2 parents 5faead5 + 5c611c6 commit 7693da1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
41 changes: 23 additions & 18 deletions src/cpp/lpnamer/input_reader/CandidatesINIReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,26 @@ std::vector<CandidateData> CandidatesINIReader::readCandidateData(
std::vector<CandidateData> result;

INIReader reader(candidateFile.string());
std::stringstream ss;
std::ostringstream err_msg;
const auto starting_pos = err_msg.tellp();
std::set<std::string> sections = reader.Sections();
for (auto const &sectionName : sections) {
CandidateData candidateData =
readCandidateSection(candidateFile, reader, sectionName);
readCandidateSection(candidateFile, reader, sectionName, err_msg);
result.push_back(candidateData);
}

err_msg.flush();
err_msg.seekp(0, std::ios_base::end);
if (err_msg.tellp() != starting_pos) {
(*logger_)(LogUtils::LOGLEVEL::FATAL) << err_msg.str();
std::exit(1);
}
return result;
}

CandidateData CandidatesINIReader::readCandidateSection(
const std::filesystem::path &candidateFile, const INIReader &reader,
const std::string &sectionName) const {
const std::string &sectionName, std::ostringstream &err_msg) const {
CandidateData candidateData;
candidateData.name = StringManip::StringUtils::ToLowercase(
getStrVal(reader, sectionName, "name"));
Expand All @@ -156,28 +162,27 @@ CandidateData CandidatesINIReader::readCandidateSection(
candidateData.linkex =
candidateData.link_name.substr(i + 3, candidateData.link_name.size());
if (!checkArea(candidateData.linkor)) {
(*logger_)(LogUtils::LOGLEVEL::FATAL)
<< LOGLOCATION << "Unrecognized area " << candidateData.linkor
<< " in section " << sectionName << " in "
<< candidateFile.string() + ".";
std::exit(1);
err_msg << LOGLOCATION << "Unrecognized area " << candidateData.linkor
<< " in section " << sectionName << " in "
<< candidateFile.string() << "\n";
}
if (!checkArea(candidateData.linkex)) {
(*logger_)(LogUtils::LOGLEVEL::FATAL)
<< LOGLOCATION << "Unrecognized area " << candidateData.linkex
<< " in section " << sectionName << " in " << candidateFile.string()
<< ".";
std::exit(1);
err_msg << LOGLOCATION << "Unrecognized area " << candidateData.linkex
<< " in section " << sectionName << " in "
<< candidateFile.string() << "\n";
}
}

// Check if interco is available
auto it = _intercoIndexMap.find(candidateData.link_name);
if (it == _intercoIndexMap.end()) {
(*logger_)(LogUtils::LOGLEVEL::FATAL)
<< LOGLOCATION << "cannot link candidate " << candidateData.name
<< " to interco id";
std::exit(1);
err_msg << LOGLOCATION << "cannot link candidate " << candidateData.name
<< " to interco id"
<< "\n";
}

if (err_msg.tellp() != 0) {
return {};
}
candidateData.link_id = it->second;
candidateData.direct_link_profile =
Expand Down
3 changes: 2 additions & 1 deletion src/cpp/lpnamer/input_reader/CandidatesINIReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class CandidatesINIReader {
bool checkArea(std::string const& areaName_p) const;
CandidateData readCandidateSection(const std::filesystem::path& candidateFile,
const INIReader& reader,
const std::string& sectionName) const;
const std::string& sectionName,
std::ostringstream& err_msg) const;

std::map<std::string, int> _intercoIndexMap;
std::vector<IntercoFileData> _intercoFileData;
Expand Down
10 changes: 5 additions & 5 deletions src/python/antares_xpansion/config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def _set_last_simulation_name(self):
"""
return last simulation name
"""
self._last_study = self.last_modified_study(self.antares_output())
self._last_study = self.last_modified_study(Path(self.antares_output()))

self._set_xpansion_simulation_name()
class NotAnXpansionOutputDir(Exception):
Expand Down Expand Up @@ -535,14 +535,14 @@ def update_last_study_with_sensitivity_results(self):
if(os.path.exists(self._xpansion_simulation_name)):
shutil.rmtree(self._xpansion_simulation_name)

def is_antares_study_output(self, study):
def is_antares_study_output(self, study: Path):
_, ext = os.path.splitext(study)
return ext == ".zip" or os.path.isdir(study)
return ext == ".zip" or os.path.isdir(study) and '-Xpansion' not in study.name

def last_modified_study(self, root_dir)-> Path:
def last_modified_study(self, root_dir:Path)-> Path:
list_dir = os.listdir(root_dir)
list_of_studies = filter(
lambda x: self.is_antares_study_output(os.path.join(root_dir, x)), list_dir
lambda x: self.is_antares_study_output(root_dir / x), list_dir
)
# Sort list of files based on last modification time in ascending order
sort_studies = sorted(
Expand Down
9 changes: 6 additions & 3 deletions src/python/antares_xpansion/input_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,15 @@ class MaxUnitsAndMaxInvestmentAreNullSimultaneously(Exception):

def _check_candidate_attributes(ini_file):
# check attributes types and values
err_msg = ""
for each_section in ini_file.sections():
for (option, value) in ini_file.items(each_section):
if not _check_candidate_option_type(option, value):
logger.error(
f"value {value} for option {option} has the wrong type!, it has to be {candidate_options_type[option]}")
raise CandidateFileWrongTypeValue
err_msg += f"value {value} for option {option} has the wrong type, it has to be {candidate_options_type[option]}\n"

if(err_msg!=""):
logger.error(err_msg)
raise CandidateFileWrongTypeValue


def _check_name_is_unique(ini_file):
Expand Down

0 comments on commit 7693da1

Please sign in to comment.