CMS 3D CMS Logo

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 303 of file IterationConfig.h.

Constructor & Destructor Documentation

◆ ConfigJsonPatcher()

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

Definition at line 224 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 293 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().

293  {
294  m_current = m_json.get();
295  m_json_stack.clear();
296  m_path_stack.clear();
297  if (!path.empty())
298  cd(path);
299  }
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 282 of file IterationConfig.cc.

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

Referenced by replace().

282  {
283  if (m_json_stack.empty())
284  throw std::runtime_error("JSON stack empty on cd_up");
285 
286  m_current = m_json_stack.back();
287  m_json_stack.pop_back();
288  m_path_stack.pop_back();
289  if (!path.empty())
290  cd(path);
291  }
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 442 of file IterationConfig.cc.

References validateAlignments::indent, and m_json.

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

442 { 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 236 of file IterationConfig.cc.

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

Referenced by replace().

236  {
237  std::string s;
238  s.reserve(128);
239  s = "ConfigJsonPatcher";
240  if (func) {
241  s += "::";
242  s += func;
243  }
244  s += " '";
245  s += get_abs_path();
246  s += "' ";
247  return s;
248  }
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 228 of file IterationConfig.cc.

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

Referenced by exc_hdr(), and replace().

228  {
229  std::string s;
230  s.reserve(64);
231  for (auto &p : m_path_stack)
232  s += p;
233  return s;
234  }
std::vector< std::string > m_path_stack

◆ load()

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

Definition at line 251 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().

251  {
252  m_json = std::make_unique<nlohmann::json>();
253  *m_json = o;
254  cd_top();
255  }
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 311 of file IterationConfig.cc.

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

311  {
312  nlohmann::json::json_pointer jp(path);
313  for (int i = first; i <= last; ++i) {
314  m_current->at(i).at(jp) = val;
315  }
316  }
nlohmann::json * m_current

◆ replace() [3/3]

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

Definition at line 326 of file IterationConfig.cc.

References cd(), cd_up(), gather_cfg::cout, exc_hdr(), dqmdumpme::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, to_string(), and relativeConstraints::value.

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

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

268  {
269  auto &itc_arr = m_json->at("m_iterations");
270  for (int i = 0; i < o.size(); ++i) {
271  from_json(itc_arr[i], o[i]);
272  }
273  }
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 260 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().

260  {
261  from_json(*m_json, o);
262  }
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 325 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 324 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 328 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 329 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 331 of file IterationConfig.h.

Referenced by replace().