CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
List of all members | Classes | Public Member Functions | Private Member Functions | Private Attributes
mkfit::ConfigJsonPatcher Class Reference

#include <IterationConfig.h>

Classes

struct  PatchReport
 

Public Member Functions

void cd (const std::string &path)
 
void cd_top (const std::string &path="")
 
void cd_up (const std::string &path="")
 
 ConfigJsonPatcher (bool verbose=false)
 
std::string dump (int indent=2)
 
nlohmann::jsonget (const std::string &path)
 
template<class T >
void load (const T &o)
 
template<typename T >
void replace (const std::string &path, T val)
 
template<typename T >
void replace (int first, int last, const std::string &path, T val)
 
int replace (const nlohmann::json &j)
 
template<>
void save (IterationsInfo &o)
 
template<class T >
void save (T &o)
 
 ~ConfigJsonPatcher ()
 

Private Member Functions

std::string exc_hdr (const char *func=nullptr) const
 
std::string get_abs_path () const
 

Private Attributes

nlohmann::jsonm_current = nullptr
 
std::unique_ptr< nlohmann::jsonm_json
 
std::vector< nlohmann::json * > m_json_stack
 
std::vector< std::string > m_path_stack
 
bool m_verbose = false
 

Detailed Description

Definition at line 317 of file IterationConfig.h.

Constructor & Destructor Documentation

◆ ConfigJsonPatcher()

mkfit::ConfigJsonPatcher::ConfigJsonPatcher ( bool  verbose = false)

Definition at line 220 of file IterationConfig.cc.

◆ ~ConfigJsonPatcher()

mkfit::ConfigJsonPatcher::~ConfigJsonPatcher ( )
default

Member Function Documentation

◆ cd()

void mkfit::ConfigJsonPatcher::cd ( const std::string &  path)

◆ cd_top()

void mkfit::ConfigJsonPatcher::cd_top ( const std::string &  path = "")

Definition at line 289 of file IterationConfig.cc.

References cd(), m_current, m_json, m_json_stack, m_path_stack, and castor_dqm_sourceclient_file_cfg::path.

Referenced by load(), mkfit::ConfigJson::patch_Files(), mkfit::ConfigJson::patchLoad_File(), and mkfit::ConfigJson::test_Patcher().

289  {
290  m_current = m_json.get();
291  m_json_stack.clear();
292  m_path_stack.clear();
293  if (!path.empty())
294  cd(path);
295  }
nlohmann::json * m_current
std::vector< nlohmann::json * > m_json_stack
void cd(const std::string &path)
std::vector< std::string > m_path_stack
std::unique_ptr< nlohmann::json > m_json

◆ cd_up()

void mkfit::ConfigJsonPatcher::cd_up ( const std::string &  path = "")

Definition at line 278 of file IterationConfig.cc.

References cd(), m_current, m_json_stack, m_path_stack, and castor_dqm_sourceclient_file_cfg::path.

Referenced by replace().

278  {
279  if (m_json_stack.empty())
280  throw std::runtime_error("JSON stack empty on cd_up");
281 
282  m_current = m_json_stack.back();
283  m_json_stack.pop_back();
284  m_path_stack.pop_back();
285  if (!path.empty())
286  cd(path);
287  }
nlohmann::json * m_current
std::vector< nlohmann::json * > m_json_stack
void cd(const std::string &path)
std::vector< std::string > m_path_stack

◆ dump()

std::string mkfit::ConfigJsonPatcher::dump ( int  indent = 2)

Definition at line 438 of file IterationConfig.cc.

References validateAlignments::indent, and m_json.

Referenced by mkfit::ConfigJson::test_Patcher().

438 { return m_json->dump(indent); }
std::unique_ptr< nlohmann::json > m_json

◆ exc_hdr()

std::string mkfit::ConfigJsonPatcher::exc_hdr ( const char *  func = nullptr) const
private

Definition at line 232 of file IterationConfig.cc.

References EcalMonitorTask_cff::func, get_abs_path(), alignCSCRings::s, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by replace().

232  {
233  std::string s;
234  s.reserve(128);
235  s = "ConfigJsonPatcher";
236  if (func) {
237  s += "::";
238  s += func;
239  }
240  s += " '";
241  s += get_abs_path();
242  s += "' ";
243  return s;
244  }
std::string get_abs_path() const

◆ get()

nlohmann::json & mkfit::ConfigJsonPatcher::get ( const std::string &  path)

◆ get_abs_path()

std::string mkfit::ConfigJsonPatcher::get_abs_path ( ) const
private

Definition at line 224 of file IterationConfig.cc.

References m_path_stack, AlCaHLTBitMon_ParallelJobs::p, alignCSCRings::s, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by exc_hdr(), and replace().

224  {
225  std::string s;
226  s.reserve(64);
227  for (auto &p : m_path_stack)
228  s += p;
229  return s;
230  }
std::vector< std::string > m_path_stack

◆ load()

template<class T >
template void mkfit::ConfigJsonPatcher::load< IterationConfig > ( const T o)

Definition at line 247 of file IterationConfig.cc.

References cd_top(), m_json, and EcalTangentSkim_cfg::o.

Referenced by mkfit::ConfigJson::patch_Files(), mkfit::ConfigJson::patchLoad_File(), and mkfit::ConfigJson::test_Patcher().

247  {
248  m_json = std::make_unique<nlohmann::json>();
249  *m_json = o;
250  cd_top();
251  }
std::unique_ptr< nlohmann::json > m_json
void cd_top(const std::string &path="")

◆ replace() [1/3]

template<typename T >
void mkfit::ConfigJsonPatcher::replace ( const std::string &  path,
T  val 
)

◆ replace() [2/3]

template<typename T >
void mkfit::ConfigJsonPatcher::replace ( int  first,
int  last,
const std::string &  path,
T  val 
)

Definition at line 307 of file IterationConfig.cc.

References first, mps_fire::i, dqmdumpme::last, m_current, castor_dqm_sourceclient_file_cfg::path, and heppy_batch::val.

307  {
308  nlohmann::json::json_pointer jp(path);
309  for (int i = first; i <= last; ++i) {
310  m_current->at(i).at(jp) = val;
311  }
312  }
nlohmann::json * m_current

◆ replace() [3/3]

int mkfit::ConfigJsonPatcher::replace ( const nlohmann::json j)

Definition at line 322 of file IterationConfig.cc.

References cd(), cd_up(), gather_cfg::cout, exc_hdr(), first, get_abs_path(), mps_fire::i, createfilelist::int, dqmiolumiharvest::j, crabWrapper::key, dqmdumpme::last, visualization-live-secondInstance_cfg::m, m_current, m_verbose, replace(), alignCSCRings::s, AlCaHLTBitMon_QueryRunRegistry::string, cond::impl::to_string(), and relativeConstraints::value.

322  {
323  if (j.is_null())
324  throw std::runtime_error(exc_hdr(__func__) + "null not expected");
325 
326  if (j.is_boolean() || j.is_number() || j.is_string()) {
327  throw std::runtime_error(exc_hdr(__func__) + "value not expected on this parsing level");
328  }
329 
330  int n_replaced = 0;
331 
332  if (j.is_object()) {
333  static const std::regex index_range_re("^\\[(\\d+)..(\\d+)\\]$", std::regex::optimize);
334 
335  for (auto &[key, value] : j.items()) {
336  std::smatch m;
337  std::regex_search(key, m, index_range_re);
338 
339  if (m.size() == 3) {
340  if (!m_current->is_array())
341  throw std::runtime_error(exc_hdr(__func__) + "array range encountered when current json is not an array");
342  int first = std::stoi(m.str(1));
343  int last = std::stoi(m.str(2));
344  for (int i = first; i <= last; ++i) {
345  std::string s("/");
346  s += std::to_string(i);
347  cd(s);
348  if (value.is_array()) {
349  for (auto &el : value)
350  n_replaced += replace(el);
351  } else {
352  n_replaced += replace(value);
353  }
354  cd_up();
355  }
356  } else if (value.is_array() || value.is_object()) {
357  std::string s("/");
358  s += key;
359  cd(s);
360  n_replaced += replace(value);
361  cd_up();
362  } else if (value.is_number() || value.is_boolean() || value.is_string()) {
363  std::string s("/");
364  s += key;
365  nlohmann::json::json_pointer jp(s);
366  if (m_current->at(jp) != value) {
367  if (m_verbose)
368  std::cout << " " << get_abs_path() << s << ": " << m_current->at(jp) << " -> " << value << "\n";
369 
370  m_current->at(jp) = value;
371  ++n_replaced;
372  }
373  } else {
374  throw std::runtime_error(exc_hdr(__func__) + "unexpected value type");
375  }
376  }
377  } else if (j.is_array() && j.empty()) {
378  } else if (j.is_array()) {
379  // Arrays are somewhat tricky.
380  // At the moment all elements are expected to be objects.
381  // This means arrays of basic types are not supported (like layer index arrays).
382  // Should not be too hard to add support for this.
383  // Now, the objects in the array can be of two kinds:
384  // a) Their keys can be json_pointer strings starting with numbers or ranges [i_low..i_high].
385  // b) They can be actual elements of the array. In this case we require the length of
386  // the array to be equal to existing length in the configuration.
387  // It is not allowed for these two kinds to mix.
388 
389  // Determine the kind of array: json_ptr or object
390 
391  static const std::regex index_re("^(?:\\[\\d+..\\d+\\]|\\d+(?:/.*)?)$", std::regex::optimize);
392 
393  bool has_index = false, has_plain = false;
394  for (int i = 0; i < (int)j.size(); ++i) {
395  const nlohmann::json &el = j[i];
396 
397  if (!el.is_object())
398  throw std::runtime_error(exc_hdr(__func__) + "array elements expected to be objects");
399 
400  for (nlohmann::json::const_iterator it = el.begin(); it != el.end(); ++it) {
401  if (std::regex_search(it.key(), index_re)) {
402  has_index = true;
403  if (has_plain)
404  throw std::runtime_error(exc_hdr(__func__) + "indexed array entry following plain one");
405  } else {
406  has_plain = true;
407  if (has_index)
408  throw std::runtime_error(exc_hdr(__func__) + "plain array entry following indexed one");
409  }
410  }
411  }
412  if (has_index) {
413  for (auto &element : j) {
414  n_replaced += replace(element);
415  }
416  } else {
417  if (m_current && !m_current->is_array())
418  throw std::runtime_error(exc_hdr(__func__) + "plain array detected when current is not an array");
419  if (m_current->size() != j.size())
420  throw std::runtime_error(exc_hdr(__func__) + "plain array of different size than at current pos");
421 
422  std::string s;
423  for (int i = 0; i < (int)j.size(); ++i) {
424  s = "/";
425  s += std::to_string(i);
426  cd(s);
427  n_replaced += replace(j[i]);
428  cd_up();
429  }
430  }
431  } else {
432  throw std::runtime_error(exc_hdr(__func__) + "unexpected json type");
433  }
434 
435  return n_replaced;
436  }
nlohmann::json * m_current
nlohmann::json json
std::string to_string(const V &value)
Definition: OMSAccess.h:71
void replace(const std::string &path, T val)
std::string exc_hdr(const char *func=nullptr) const
void cd(const std::string &path)
Definition: value.py:1
void cd_up(const std::string &path="")
std::string get_abs_path() const

◆ save() [1/2]

template<>
void mkfit::ConfigJsonPatcher::save ( IterationsInfo o)

Definition at line 264 of file IterationConfig.cc.

References mkfit::from_json(), mps_fire::i, and EcalTangentSkim_cfg::o.

264  {
265  auto &itc_arr = m_json->at("m_iterations");
266  for (int i = 0; i < o.size(); ++i) {
267  from_json(itc_arr[i], o[i]);
268  }
269  }
void from_json(const nlohmann::json &nlohmann_json_j, mkfit::LayerControl &nlohmann_json_t)
std::unique_ptr< nlohmann::json > m_json

◆ save() [2/2]

template<class T >
template void mkfit::ConfigJsonPatcher::save< IterationConfig > ( T o)

Definition at line 256 of file IterationConfig.cc.

References mkfit::from_json(), m_json, and EcalTangentSkim_cfg::o.

Referenced by mkfit::ConfigJson::patch_Files(), mkfit::ConfigJson::patchLoad_File(), and mkfit::ConfigJson::test_Patcher().

256  {
257  from_json(*m_json, o);
258  }
void from_json(const nlohmann::json &nlohmann_json_j, mkfit::LayerControl &nlohmann_json_t)
std::unique_ptr< nlohmann::json > m_json

Member Data Documentation

◆ m_current

nlohmann::json* mkfit::ConfigJsonPatcher::m_current = nullptr
private

Definition at line 339 of file IterationConfig.h.

Referenced by cd(), cd_top(), cd_up(), get(), and replace().

◆ m_json

std::unique_ptr<nlohmann::json> mkfit::ConfigJsonPatcher::m_json
private

Definition at line 338 of file IterationConfig.h.

Referenced by cd_top(), dump(), load(), and save().

◆ m_json_stack

std::vector<nlohmann::json *> mkfit::ConfigJsonPatcher::m_json_stack
private

Definition at line 342 of file IterationConfig.h.

Referenced by cd(), cd_top(), and cd_up().

◆ m_path_stack

std::vector<std::string> mkfit::ConfigJsonPatcher::m_path_stack
private

Definition at line 343 of file IterationConfig.h.

Referenced by cd(), cd_top(), cd_up(), and get_abs_path().

◆ m_verbose

bool mkfit::ConfigJsonPatcher::m_verbose = false
private

Definition at line 345 of file IterationConfig.h.

Referenced by replace().