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

Constructor & Destructor Documentation

◆ ConfigJsonPatcher()

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

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

183  {
184  m_current = m_json.get();
185  m_json_stack.clear();
186  m_path_stack.clear();
187  if (!path.empty())
188  cd(path);
189  }
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 172 of file IterationConfig.cc.

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

Referenced by replace().

172  {
173  if (m_json_stack.empty())
174  throw std::runtime_error("JSON stack empty on cd_up");
175 
176  m_current = m_json_stack.back();
177  m_json_stack.pop_back();
178  m_path_stack.pop_back();
179  if (!path.empty())
180  cd(path);
181  }
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 332 of file IterationConfig.cc.

References util.rrClient::indent, and m_json.

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

332 { 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 126 of file IterationConfig.cc.

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

Referenced by replace().

126  {
127  std::string s;
128  s.reserve(128);
129  s = "ConfigJsonPatcher";
130  if (func) {
131  s += "::";
132  s += func;
133  }
134  s += " '";
135  s += get_abs_path();
136  s += "' ";
137  return s;
138  }
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 118 of file IterationConfig.cc.

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

Referenced by exc_hdr(), and replace().

118  {
119  std::string s;
120  s.reserve(64);
121  for (auto &p : m_path_stack)
122  s += p;
123  return s;
124  }
std::vector< std::string > m_path_stack

◆ load()

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

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

141  {
142  m_json = std::make_unique<nlohmann::json>();
143  *m_json = o;
144  cd_top();
145  }
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 201 of file IterationConfig.cc.

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

201  {
202  nlohmann::json::json_pointer jp(path);
203  for (int i = first; i <= last; ++i) {
204  m_current->at(i).at(jp) = val;
205  }
206  }
nlohmann::json * m_current

◆ replace() [3/3]

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

Definition at line 216 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.

216  {
217  if (j.is_null())
218  throw std::runtime_error(exc_hdr(__func__) + "null not expected");
219 
220  if (j.is_boolean() || j.is_number() || j.is_string()) {
221  throw std::runtime_error(exc_hdr(__func__) + "value not expected on this parsing level");
222  }
223 
224  int n_replaced = 0;
225 
226  if (j.is_object()) {
227  static const std::regex index_range_re("^\\[(\\d+)..(\\d+)\\]$", std::regex::optimize);
228 
229  for (auto &[key, value] : j.items()) {
230  std::smatch m;
231  std::regex_search(key, m, index_range_re);
232 
233  if (m.size() == 3) {
234  if (!m_current->is_array())
235  throw std::runtime_error(exc_hdr(__func__) + "array range encountered when current json is not an array");
236  int first = std::stoi(m.str(1));
237  int last = std::stoi(m.str(2));
238  for (int i = first; i <= last; ++i) {
239  std::string s("/");
240  s += std::to_string(i);
241  cd(s);
242  if (value.is_array()) {
243  for (auto &el : value)
244  n_replaced += replace(el);
245  } else {
246  n_replaced += replace(value);
247  }
248  cd_up();
249  }
250  } else if (value.is_array() || value.is_object()) {
251  std::string s("/");
252  s += key;
253  cd(s);
254  n_replaced += replace(value);
255  cd_up();
256  } else if (value.is_number() || value.is_boolean() || value.is_string()) {
257  std::string s("/");
258  s += key;
259  nlohmann::json::json_pointer jp(s);
260  if (m_current->at(jp) != value) {
261  if (m_verbose)
262  std::cout << " " << get_abs_path() << s << ": " << m_current->at(jp) << " -> " << value << "\n";
263 
264  m_current->at(jp) = value;
265  ++n_replaced;
266  }
267  } else {
268  throw std::runtime_error(exc_hdr(__func__) + "unexpected value type");
269  }
270  }
271  } else if (j.is_array() && j.empty()) {
272  } else if (j.is_array()) {
273  // Arrays are somewhat tricky.
274  // At the moment all elements are expected to be objects.
275  // This means arrays of basic types are not supported (like layer index arrays).
276  // Should not be too hard to add support for this.
277  // Now, the objects in the array can be of two kinds:
278  // a) Their keys can be json_pointer strings starting with numbers or ranges [i_low..i_high].
279  // b) They can be actual elements of the array. In this case we require the length of
280  // the array to be equal to existing length in the configuration.
281  // It is not allowed for these two kinds to mix.
282 
283  // Determine the kind of array: json_ptr or object
284 
285  static const std::regex index_re("^(?:\\[\\d+..\\d+\\]|\\d+(?:/.*)?)$", std::regex::optimize);
286 
287  bool has_index = false, has_plain = false;
288  for (int i = 0; i < (int)j.size(); ++i) {
289  const nlohmann::json &el = j[i];
290 
291  if (!el.is_object())
292  throw std::runtime_error(exc_hdr(__func__) + "array elements expected to be objects");
293 
294  for (nlohmann::json::const_iterator it = el.begin(); it != el.end(); ++it) {
295  if (std::regex_search(it.key(), index_re)) {
296  has_index = true;
297  if (has_plain)
298  throw std::runtime_error(exc_hdr(__func__) + "indexed array entry following plain one");
299  } else {
300  has_plain = true;
301  if (has_index)
302  throw std::runtime_error(exc_hdr(__func__) + "plain array entry following indexed one");
303  }
304  }
305  }
306  if (has_index) {
307  for (auto &element : j) {
308  n_replaced += replace(element);
309  }
310  } else {
311  if (m_current && !m_current->is_array())
312  throw std::runtime_error(exc_hdr(__func__) + "plain array detected when current is not an array");
313  if (m_current->size() != j.size())
314  throw std::runtime_error(exc_hdr(__func__) + "plain array of different size than at current pos");
315 
316  std::string s;
317  for (int i = 0; i < (int)j.size(); ++i) {
318  s = "/";
319  s += std::to_string(i);
320  cd(s);
321  n_replaced += replace(j[i]);
322  cd_up();
323  }
324  }
325  } else {
326  throw std::runtime_error(exc_hdr(__func__) + "unexpected json type");
327  }
328 
329  return n_replaced;
330  }
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 158 of file IterationConfig.cc.

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

158  {
159  auto &itc_arr = m_json->at("m_iterations");
160  for (int i = 0; i < o.size(); ++i) {
161  from_json(itc_arr[i], o[i]);
162  }
163  }
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 150 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().

150  {
151  from_json(*m_json, o);
152  }
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 300 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 299 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 303 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 304 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 306 of file IterationConfig.h.

Referenced by replace().