99// granted to it by virtue of its status as an Intergovernmental Organization
1010// or submit itself to any jurisdiction.
1111
12- // first version 8/2018, Sandro Wenzel
12+ // first version 8/2018, Sandro Wenzel
1313
1414#include " CommonUtils/ConfigurableParam.h"
1515#include " CommonUtils/StringUtils.h"
1616#include " CommonUtils/KeyValParam.h"
17+ #include " CommonUtils/ConfigurableParamReaders.h"
1718#define BOOST_BIND_GLOBAL_PLACEHOLDERS
1819#include < boost/algorithm/string/predicate.hpp>
1920#include < boost/property_tree/ptree.hpp>
@@ -47,7 +48,6 @@ std::vector<ConfigurableParam*>* ConfigurableParam::sRegisteredParamClasses = nu
4748boost::property_tree::ptree* ConfigurableParam::sPtree = nullptr ;
4849std::map<std::string, std::pair<std::type_info const &, void *>>* ConfigurableParam::sKeyToStorageMap = nullptr ;
4950std::map<std::string, ConfigurableParam::EParamProvenance>* ConfigurableParam::sValueProvenanceMap = nullptr ;
50- std::string ConfigurableParam::sInputDir = " " ;
5151std::string ConfigurableParam::sOutputDir = " " ;
5252EnumRegistry* ConfigurableParam::sEnumRegistry = nullptr ;
5353
@@ -194,60 +194,28 @@ void ConfigurableParam::writeINI(std::string const& filename, std::string const&
194194
195195bool ConfigurableParam::configFileExists (std::string const & filepath)
196196{
197- return std::filesystem::exists (o2::utils::Str::concat_string (sInputDir , filepath));
197+ return std::filesystem::exists (o2::utils::Str::concat_string (ConfigurableParamReaders::getInputDir () , filepath));
198198}
199199
200200// ------------------------------------------------------------------
201201
202- boost::property_tree::ptree ConfigurableParam::readConfigFile (std::string const & filepath )
202+ void ConfigurableParam::setValue (std::string const & key, std::string const & valuestring )
203203{
204- auto inpfilename = o2::utils::Str::concat_string (sInputDir , filepath);
205- if (!std::filesystem::exists (inpfilename)) {
206- LOG (fatal) << inpfilename << " : config file does not exist!" ;
207- }
208-
209- boost::property_tree::ptree pt;
210-
211- if (boost::iends_with (inpfilename, " .ini" )) {
212- pt = readINI (inpfilename);
213- } else if (boost::iends_with (inpfilename, " .json" )) {
214- pt = readJSON (inpfilename);
215- } else {
216- LOG (fatal) << " Configuration file must have either .ini or .json extension" ;
217- }
218-
219- return pt;
220- }
221-
222- // ------------------------------------------------------------------
223-
224- boost::property_tree::ptree ConfigurableParam::readINI (std::string const & filepath)
225- {
226- boost::property_tree::ptree pt;
227- try {
228- boost::property_tree::read_ini (filepath, pt);
229- } catch (const boost::property_tree::ptree_error& e) {
230- LOG (fatal) << " Failed to read INI config file " << filepath << " (" << e.what () << " )" ;
231- } catch (...) {
232- LOG (fatal) << " Unknown error when reading INI config file " ;
204+ if (!sIsFullyInitialized ) {
205+ initialize ();
233206 }
234-
235- return pt;
236- }
237-
238- // ------------------------------------------------------------------
239-
240- boost::property_tree::ptree ConfigurableParam::readJSON (std::string const & filepath)
241- {
242- boost::property_tree::ptree pt;
243-
207+ assert (sPtree );
244208 try {
245- boost::property_tree::read_json (filepath, pt);
246- } catch (const boost::property_tree::ptree_error& e) {
247- LOG (fatal) << " Failed to read JSON config file " << filepath << " (" << e.what () << " )" ;
209+ if (sPtree ->get_optional <std::string>(key).is_initialized ()) {
210+ sPtree ->put (key, valuestring);
211+ auto changed = updateThroughStorageMapWithConversion (key, valuestring);
212+ if (changed != EParamUpdateStatus::Failed) {
213+ sValueProvenanceMap ->find (key)->second = kRT ; // set to runtime
214+ }
215+ }
216+ } catch (std::exception const & e) {
217+ std::cerr << " Error in setValue (string) " << e.what () << " \n " ;
248218 }
249-
250- return pt;
251219}
252220
253221// ------------------------------------------------------------------
@@ -258,7 +226,7 @@ void ConfigurableParam::writeJSON(std::string const& filename, std::string const
258226 LOG (info) << " ignoring writing of json file " << filename;
259227 return ;
260228 }
261- initPropertyTree (); // update the boost tree before writing
229+ initPropertyTree (); // update the boost tree before writing
262230 auto outfilename = o2::utils::Str::concat_string (sOutputDir , filename);
263231 if (!keyOnly.empty ()) { // write ini for selected key only
264232 try {
@@ -409,7 +377,7 @@ void ConfigurableParam::updateFromFile(std::string const& configFile, std::strin
409377 return ;
410378 }
411379
412- boost::property_tree::ptree pt = readConfigFile (cfgfile);
380+ boost::property_tree::ptree pt = ConfigurableParamReaders:: readConfigFile (cfgfile);
413381
414382 std::vector<std::pair<std::string, std::string>> keyValPairs;
415383 auto request = o2::utils::Str::tokenize (paramsList, ' ,' , true );
@@ -522,7 +490,7 @@ void ConfigurableParam::updateFromString(std::string const& configString)
522490
523491 const auto & kv = o2::conf::KeyValParam::Instance ();
524492 if (getProvenance (" keyval.input_dir" ) != kCODE ) {
525- sInputDir = o2::utils::Str::concat_string (o2::utils::Str::rectifyDirectory (kv.input_dir ));
493+ ConfigurableParamReaders::setInputDir ( o2::utils::Str::concat_string (o2::utils::Str::rectifyDirectory (kv.input_dir ) ));
526494 }
527495 if (getProvenance (" keyval.output_dir" ) != kCODE ) {
528496 if (kv.output_dir == " /dev/null" ) {
0 commit comments