CMS 3D CMS Logo

ConfigurationDescriptions.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: ParameterSet
4 // Class : ConfigurationDescriptions
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: W. David Dagenhart
10 // Created: 17 December 2008
11 //
12 
18 
19 #include <fstream>
20 #include <iostream>
21 #include <iomanip>
22 #include <sstream>
23 #include <cstring>
24 #include <cerrno>
25 #include <cstring>
26 
27 namespace {
28  void matchLabel(std::pair<std::string, edm::ParameterSetDescription> const& thePair,
29  std::string const& moduleLabel,
30  edm::ParameterSetDescription const*& psetDesc) {
31  if (thePair.first == moduleLabel) {
32  psetDesc = &thePair.second;
33  }
34  }
35 } // namespace
36 
37 static const char* const kSource = "Source";
38 static const char* const kService = "Service";
39 static const char* const k_source = "source";
40 
41 namespace edm {
42 
44  : baseType_(baseType), pluginName_(pluginName), defaultDescDefined_(false) {}
45 
47 
49 
51 
52  void ConfigurationDescriptions::add(char const* label, ParameterSetDescription const& psetDescription) {
53  std::string labelString(label);
54  add(labelString, psetDescription);
55  }
56 
58  if (0 == strcmp(baseType_.c_str(), kSource)) {
59  if (0 != strcmp(label.c_str(), k_source)) {
61  "ConfigurationDescriptions::add, when adding a ParameterSetDescription for a source the "
62  "label must be \"source\"\n");
63  }
64  if (!descriptions_.empty() || defaultDescDefined_ == true) {
65  throw edm::Exception(
67  "ConfigurationDescriptions::add, for a source only 1 ParameterSetDescription may be added\n");
68  }
69  } else if (0 == strcmp(baseType_.c_str(), kService)) {
70  if (!descriptions_.empty() || defaultDescDefined_ == true) {
71  throw edm::Exception(
73  "ConfigurationDescriptions::add, for a service only 1 ParameterSetDescription may be added\n");
74  }
75  }
76 
77  // To minimize the number of copies involved create an empty description first
78  // and push it into the vector. Then perform the copy.
79  std::pair<std::string, ParameterSetDescription> pairWithEmptyDescription;
80  descriptions_.push_back(pairWithEmptyDescription);
81  std::pair<std::string, ParameterSetDescription>& pair = descriptions_.back();
82 
83  pair.first = label;
84  pair.second = psetDescription;
85  }
86 
89  if (kService == baseType_) {
91  } else if (kSource == baseType_) {
92  label = "source";
93  } else {
95  }
96  add(label, psetDescription);
97  }
98 
100  if (0 == strcmp(baseType_.c_str(), kSource) || 0 == strcmp(baseType_.c_str(), kService)) {
101  if (!descriptions_.empty() || defaultDescDefined_ == true) {
103  "ConfigurationDescriptions::addDefault, for a source or service only 1 "
104  "ParameterSetDescription may be added\n");
105  }
106  }
107 
108  defaultDescDefined_ = true;
109  defaultDesc_ = psetDescription;
110  }
111 
113  if (defaultDescDefined_) {
114  return &defaultDesc_;
115  }
116  return nullptr;
117  }
118 
120 
122 
124  ParameterSetDescription const* psetDesc = nullptr;
125  for_all(descriptions_, std::bind(&matchLabel, std::placeholders::_1, std::cref(moduleLabel), std::ref(psetDesc)));
126 
127  // If there is a matching label
128  if (psetDesc != nullptr) {
129  psetDesc->validate(pset);
130  }
131  // Is there an explicit description to be used for a non standard label
132  else if (defaultDescDefined_) {
134  }
135  // Otherwise use the first one.
136  else if (!descriptions_.empty()) {
137  descriptions_[0].second.validate(pset);
138  }
139  // It is possible for no descriptions to be defined and no validation occurs
140  // for this module ever.
141  }
142 
143  void ConfigurationDescriptions::writeCfis(std::set<std::string>& usedCfiFileNames) const {
144  bool wroteClassFile = false;
146  if (defaultDescDefined_) {
148  wroteClassFile = true;
149  } else if (descriptions_.size() == 1) {
150  paths = writeClassFile(descriptions_.begin()->second, true);
151  wroteClassFile = true;
152  }
153  CfiOptions ops = wroteClassFile ? CfiOptions{cfi::Untyped{paths}} : CfiOptions{cfi::Typed{}};
154  for (auto& d : descriptions_) {
156  d, baseType_, pluginName_, (not defaultDescDefined_) and (1 == descriptions_.size()), ops, usedCfiFileNames);
157  }
158  }
159 
160  namespace {
161  std::string modifyPluginName(std::string iName) {
162  auto found = iName.find("::");
163  while (found != std::string::npos) {
164  iName.replace(found, 2, "_");
165  found = iName.find("::");
166  }
167  //Symbols that can appear in our plugin names but can't in python function names
168  const std::string toReplace("@<>,");
169  found = iName.find_first_of(toReplace);
170  while (found != std::string::npos) {
171  iName.replace(found, 1, "_");
172  found = iName.find_first_of(toReplace, found);
173  }
174  return iName;
175  }
176  } // namespace
177 
179  bool willUseWithCfis) const {
180  std::string pluginName = modifyPluginName(pluginName_);
181 
182  std::string fileName = pluginName + ".py";
183  std::ofstream outFile(fileName.c_str());
184  if (outFile.fail()) {
185  edm::Exception ex(edm::errors::LogicError, "Creating class file failed.\n");
186  ex << "Opening a file '" << fileName << "' failed.\n";
187  ex << "Error code from errno " << errno << ": " << std::strerror(errno) << "\n";
188 
189  ex.addContext("Executing function ConfigurationDescriptions::writeDefault");
190  throw ex;
191  }
192  outFile << "import FWCore.ParameterSet.Config as cms\n\n";
193  outFile << "def " << pluginName
194  << "(**kwargs):\n"
195  " mod = cms."
196  << baseType_ << "('" << pluginName_ << "'";
197 
198  bool startWithComma = true;
199  int indentation = 4;
200  CfiOptions ops = willUseWithCfis ? CfiOptions{cfi::ClassFile{}} : CfiOptions{cfi::Typed{}};
201  iDesc.writeCfi(outFile, startWithComma, indentation, ops);
202 
203  outFile << ")\n"
204  " for k,v in kwargs.items():\n"
205  " setattr(mod, k, v)\n"
206  " return mod\n";
207 
208  outFile.close();
209  return std::holds_alternative<cfi::ClassFile>(ops) ? std::get<cfi::ClassFile>(ops).releasePaths() : cfi::Paths{};
210  }
211 
212  void ConfigurationDescriptions::writeCfiForLabel(std::pair<std::string, ParameterSetDescription> const& labelAndDesc,
213  std::string const& baseType,
214  std::string const& pluginName,
215  bool isSameAsDefault,
217  std::set<std::string>& usedCfiFileNames) {
218  if (0 == strcmp(baseType.c_str(), kService) && labelAndDesc.first != pluginName) {
220  "ConfigurationDescriptions::writeCfiForLabel\nFor a service the label and the plugin name "
221  "must be the same.\n")
222  << "This error is probably caused by an incorrect label being passed\nto the ConfigurationDescriptions::add "
223  "function earlier.\n"
224  << "plugin name = \"" << pluginName << "\" label name = \"" << labelAndDesc.first << "\"\n";
225  }
226 
227  std::string cfi_filename;
228  if (0 == strcmp(baseType.c_str(), kSource)) {
229  cfi_filename = pluginName + "_cfi.py";
230  } else {
231  cfi_filename = labelAndDesc.first + "_cfi.py";
232  }
233  if (!usedCfiFileNames.insert(cfi_filename).second) {
235  "Two cfi files are being generated with the same name in the same directory.\n");
236  ex << "The cfi file name is '" << cfi_filename << "' and\n"
237  << "the module label is \'" << labelAndDesc.first << "\'.\n"
238  << "This error is probably caused by an error in one or more fillDescriptions functions\n"
239  << "where duplicate module labels are being passed to the ConfigurationDescriptions::add\n"
240  << "function. All such module labels must be unique within a package.\n"
241  << "If you do not want the generated cfi file and do not need more than one\n"
242  << "description for a plugin, then a way to fix this is to use the addDefault\n"
243  << "function instead of the add function.\n"
244  << "There are 3 common ways this problem can happen.\n"
245  << "1. This can happen when a module label is explicitly duplicated in one or more\n"
246  << "fillDescriptions functions. Fix these by changing the module labels to be unique.\n"
247  << "2. This can also happen when a module class is a template class and plugins are\n"
248  << "defined by instantiations with differing template parameters and these plugins\n"
249  << "share the same fillDescriptions function. Fix these by specializing the fillDescriptions\n"
250  << "function for each template instantiation.\n"
251  << "3. This can also happen when there is an inheritance heirarchy and multiple plugin modules\n"
252  << "are defined using derived classes and the base class which share the same fillDescriptions\n"
253  << "function. Fix these by redefining the fillDescriptions function in each derived class.\n";
254  ex.addContext("Executing function ConfigurationDescriptions::writeCfiForLabel");
255  throw ex;
256  }
257  std::ofstream outFile(cfi_filename.c_str());
258  if (outFile.fail()) {
259  edm::Exception ex(edm::errors::LogicError, "Creating cfi file failed.\n");
260  ex << "Opening a file '" << cfi_filename << "' for module '" << labelAndDesc.first << "' failed.\n";
261  ex << "Error code from errno " << errno << ": " << std::strerror(errno) << "\n";
262 
263  ex.addContext("Executing function ConfigurationDescriptions::writeCfiForLabel");
264  throw ex;
265  }
266 
267  bool startWithComma = true;
268  if (not shouldWriteUntyped(options)) {
269  outFile << "import FWCore.ParameterSet.Config as cms\n\n";
270  outFile << labelAndDesc.first << " = cms." << baseType << "('" << pluginName << "'";
271  } else {
272  outFile << "import FWCore.ParameterSet.Config as cms\n\n";
273 
274  auto pythonName = modifyPluginName(pluginName);
275  outFile << "from ." << pythonName << " import " << pythonName << "\n\n";
276  outFile << labelAndDesc.first << " = " << pythonName << "(";
277  startWithComma = false;
278  }
279  if (not isSameAsDefault) {
280  int indentation = 2;
281  labelAndDesc.second.writeCfi(outFile, startWithComma, indentation, options);
282  }
283  outFile << ")\n";
284 
285  outFile.close();
286 
287  if (0 == strcmp(baseType.c_str(), kSource)) {
288  std::cout << pluginName << "\n";
289  } else {
290  std::cout << labelAndDesc.first << "\n";
291  }
292  }
293 
295  std::string const& moduleLabel,
296  bool brief,
297  bool printOnlyLabels,
298  size_t lineWidth,
299  int indentation,
300  int iPlugin) const {
301  if (!brief) {
302  if (!comment().empty()) {
304  }
305  os << "\n";
306  }
307 
308  if (descriptions_.empty() && !defaultDescDefined_) {
309  char oldFill = os.fill();
311  os << std::setfill(' ') << std::setw(indentation) << "";
312  os << "There are no PSet descriptions defined for this plugin.\n";
313  os << std::setfill(' ') << std::setw(indentation) << "";
314  os << "PSets will not be validated and no cfi files will be generated.\n";
315  os << std::setfill(oldFill);
316  if (!brief)
317  os << "\n";
318  return;
319  }
320 
323  char oldFill = os.fill();
324  os << std::setfill(' ') << std::setw(indentation) << "";
325  os << "This plugin has not implemented the function which defines its\n";
326  os << std::setfill(' ') << std::setw(indentation) << "";
327  os << "configuration descriptions yet. No descriptions are available.\n";
328  os << std::setfill(' ') << std::setw(indentation) << "";
329  os << "Its PSets will not be validated, and no cfi files will be generated.\n";
330  os << std::setfill(oldFill);
331  if (!brief)
332  os << "\n";
333  return;
334  }
335 
336  if (!brief) {
337  std::stringstream ss;
338  if (defaultDescDefined_) {
339  if (descriptions_.empty()) {
340  ss << "This plugin has only one PSet description. "
341  << "This description is always used to validate configurations. "
342  << "Because this configuration has no label, no cfi files will be generated.";
343  } else {
344  ss << "This plugin has " << (descriptions_.size() + 1U) << " PSet descriptions. "
345  << "The description used to validate a configuration is selected by "
346  << "matching the module labels. If none match, then the last description, "
347  << "which has no label, is selected. "
348  << "A cfi file will be generated for each configuration with a module label.";
349  }
350  } else {
351  if (descriptions_.size() == 1U) {
352  ss << "This plugin has " << descriptions_.size() << " PSet description. "
353  << "This description is always used to validate configurations. "
354  << "The label below is used when generating the cfi file.";
355  } else {
356  ss << "This plugin has " << descriptions_.size() << " PSet descriptions. "
357  << "The description used to validate a configuration is selected by "
358  << "matching the module labels. If none match the first description below is used. "
359  << "The module labels below are also used when generating the cfi files.";
360  }
361  }
363  os << "\n";
364  }
365 
367 
369  counter.iPlugin = iPlugin;
370  counter.iSelectedModule = 0;
371  counter.iModule = 0;
372 
373  for (auto const& d : descriptions_) {
374  printForLabel(d, os, moduleLabel, brief, printOnlyLabels, lineWidth, indentation, counter);
375  }
376 
377  if (defaultDescDefined_) {
379  std::string("@default"),
380  defaultDesc_,
381  moduleLabel,
382  brief,
383  printOnlyLabels,
384  lineWidth,
385  indentation,
386  counter);
387  }
388  }
389 
390  void ConfigurationDescriptions::printForLabel(std::pair<std::string, ParameterSetDescription> const& labelAndDesc,
391  std::ostream& os,
392  std::string const& moduleLabel,
393  bool brief,
394  bool printOnlyLabels,
395  size_t lineWidth,
396  int indentation,
397  DescriptionCounter& counter) const {
399  labelAndDesc.first,
400  labelAndDesc.second,
401  moduleLabel,
402  brief,
403  printOnlyLabels,
404  lineWidth,
405  indentation,
406  counter);
407  }
408 
410  std::string const& label,
412  std::string const& moduleLabel,
413  bool brief,
414  bool printOnlyLabels,
415  size_t lineWidth,
416  int indentation,
417  DescriptionCounter& counter) const {
418  ++counter.iModule;
419  if (!moduleLabel.empty() && label != moduleLabel)
420  return;
421  ++counter.iSelectedModule;
422 
423  std::stringstream ss;
424  ss << counter.iPlugin << "." << counter.iSelectedModule;
425  std::string section = ss.str();
426 
427  char oldFill = os.fill();
428  os << std::setfill(' ') << std::setw(indentation) << "" << std::setfill(oldFill);
429  os << section << " ";
430  if (label == std::string("@default")) {
431  os << "description without a module label\n";
432  } else {
433  if (!brief) {
434  if (0 == strcmp(baseType_.c_str(), kSource) || 0 == strcmp(baseType_.c_str(), kService)) {
435  os << "label: ";
436  } else {
437  os << "module label: ";
438  }
439  }
440  os << label << "\n";
441  }
442 
443  if (!brief) {
444  if (!description.comment().empty()) {
446  }
447  os << "\n";
448  }
449  if (printOnlyLabels)
450  return;
451 
452  DocFormatHelper dfh;
453  dfh.setBrief(brief);
454  dfh.setLineWidth(lineWidth);
456  dfh.setSection(section);
458 
459  description.print(os, dfh);
460  }
461 } // namespace edm
bool shouldWriteUntyped(CfiOptions const &iOps) noexcept
ConfigurationDescriptions(std::string const &baseType, std::string const &pluginName)
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
void validate(ParameterSet &pset, std::string const &moduleLabel) const
static const char *const kService
void print(std::ostream &os, std::string const &moduleLabel, bool brief, bool printOnlyLabels, size_t lineWidth, int indentation, int iPlugin) const
static void wrapAndPrintText(std::ostream &os, std::string const &text, size_t indent, size_t suggestedWidth)
void printForLabel(std::pair< std::string, ParameterSetDescription > const &labelAndDesc, std::ostream &os, std::string const &moduleLabel, bool brief, bool printOnlyLabels, size_t lineWidth, int indentationn, DescriptionCounter &counter) const
cfi::Paths writeClassFile(ParameterSetDescription const &, bool willUseWithCfis) const
ParameterSetDescription * defaultDescription()
Returns 0 if no default has been assigned.
static const char *const kSource
void writeCfis(std::set< std::string > &usedCfiFileNames) const
Func for_all(ForwardSequence &s, Func f)
wrapper for std::for_each
Definition: Algorithms.h:14
static int offsetModuleLabel()
char const * label
void addDefault(ParameterSetDescription const &psetDescription)
void setBrief(bool value)
std::vector< std::pair< std::string, ParameterSetDescription > > descriptions_
std::string defaultModuleLabel(std::string label)
Definition: value.py:1
d
Definition: ztail.py:151
void setComment(std::string const &value)
static void writeCfiForLabel(std::pair< std::string, ParameterSetDescription > const &labelAndDesc, std::string const &baseType, std::string const &pluginName, bool isSameAsDefault, CfiOptions &options, std::set< std::string > &usedCfiFileNames)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void addContext(std::string const &context)
Definition: Exception.cc:169
void validate(ParameterSet &pset) const
HLT enums.
static std::atomic< unsigned int > counter
static const char *const k_source
static int offsetTopLevelPSet()
void setSection(std::string const &value)
std::vector< std::pair< std::string, ParameterSetDescription > >::iterator iterator
void writeCfi(std::ostream &os, bool startWithComma, int indentation, CfiOptions &) const
std::string const & comment() const
void setParent(DescriptionParent value)
cfi::CfiOptions CfiOptions
void setIndentation(int value)
void setLineWidth(size_t value)