CMS 3D CMS Logo

PythonProcessDesc.cc
Go to the documentation of this file.
6 
7 #include <sstream>
8 
9 using namespace boost::python;
10 
12  theProcessPSet(),
13  theMainModule(),
14  theMainNamespace() {
15 }
16 
19  theMainModule(),
21  prepareToRead();
22  read(config);
23  Py_Finalize();
24 }
25 
28  theMainModule(),
30  prepareToRead();
31  PySys_SetArgv(argc, argv);
32  read(config);
33  Py_Finalize();
34 }
35 
38 
39  theMainModule = object(handle<>(borrowed(PyImport_AddModule(const_cast<char*>("__main__")))));
40 
41  theMainNamespace = theMainModule.attr("__dict__");
42  theMainNamespace["processDesc"] = ptr(this);
43  theMainNamespace["processPSet"] = ptr(&theProcessPSet);
44 }
45 
47  try {
48  // if it ends with py, it's a file
49  if(config.substr(config.size()-3) == ".py") {
50  readFile(config);
51  } else {
52  readString(config);
53  }
54  }
55  catch(error_already_set) {
56  edm::pythonToCppException("Configuration");
57  Py_Finalize();
58  }
59 }
60 
62  std::string initCommand("import FWCore.ParameterSet.Config as cms\n"
63  "execfile('");
64  initCommand += fileName + "')";
65 
66  handle<>(PyRun_String(initCommand.c_str(),
67  Py_file_input,
68  theMainNamespace.ptr(),
69  theMainNamespace.ptr()));
70  std::string command("process.fillProcessDesc(processPSet)");
71  handle<>(PyRun_String(command.c_str(),
72  Py_eval_input,
73  theMainNamespace.ptr(),
74  theMainNamespace.ptr()));
75 }
76 
78  std::string command = pyConfig;
79  command += "\nprocess.fillProcessDesc(processPSet)";
80  handle<>(PyRun_String(command.c_str(),
81  Py_file_input,
82  theMainNamespace.ptr(),
83  theMainNamespace.ptr()));
84 }
85 
86 std::shared_ptr<edm::ParameterSet> PythonProcessDesc::parameterSet() const {
87  return std::make_shared<edm::ParameterSet>(theProcessPSet.pset());
88 }
89 
91  std::ostringstream os;
92  os << theProcessPSet.dump();
93  return os.str();
94 }
95 
96 // For backward compatibility only. Remove when no longer used.
97 std::shared_ptr<edm::ProcessDesc> PythonProcessDesc::processDesc() const {
98  return std::make_shared<edm::ProcessDesc>(parameterSet());
99 }
boost::python::object theMainModule
std::string dump() const
PythonParameterSet theProcessPSet
void pythonToCppException(const std::string &iType)
Definition: PythonWrapper.cc:6
Definition: config.py:1
void readFile(std::string const &fileName)
void read(std::string const &config)
boost::python::object theMainNamespace
std::string dump() const
std::shared_ptr< edm::ParameterSet > parameterSet() const
edm::ParameterSet & pset()
void initializeModule()
void readString(std::string const &pyConfig)
std::shared_ptr< edm::ProcessDesc > processDesc() const