Go to the documentation of this file.00001 #include "FWCore/PythonParameterSet/interface/MakeParameterSets.h"
00002 #include "FWCore/PythonParameterSet/interface/PythonProcessDesc.h"
00003 #include "FWCore/ParameterSet/interface/ProcessDesc.h"
00004
00005 #include "FWCore/PythonParameterSet/interface/PythonParameterSet.h"
00006 #include "FWCore/PythonParameterSet/src/initializeModule.h"
00007
00008
00009 using namespace boost::python;
00010
00011 static
00012 void
00013 makePSetsFromFile(const std::string& fileName, boost::python::object& mainNamespace)
00014 {
00015 std::string initCommand("from FWCore.ParameterSet.Types import makeCppPSet\n"
00016 "execfile('");
00017 initCommand += fileName + "')";
00018
00019
00020 handle<>(PyRun_String(initCommand.c_str(),
00021 Py_file_input,
00022 mainNamespace.ptr(),
00023 mainNamespace.ptr()));
00024 std::string command("makeCppPSet(locals(), topPSet)");
00025 handle<>(PyRun_String(command.c_str(),
00026 Py_eval_input,
00027 mainNamespace.ptr(),
00028 mainNamespace.ptr()));
00029 }
00030
00031 static
00032 void
00033 makePSetsFromString(const std::string& module, boost::python::object& mainNamespace)
00034 {
00035 std::string command = module;
00036 command += "\nfrom FWCore.ParameterSet.Types import makeCppPSet\nmakeCppPSet(locals(), topPSet)";
00037 handle<>(PyRun_String(command.c_str(),
00038 Py_file_input,
00039 mainNamespace.ptr(),
00040 mainNamespace.ptr()));
00041 }
00042
00043 namespace edm
00044 {
00045
00046 boost::shared_ptr<ProcessDesc>
00047 readConfig(const std::string & config)
00048 {
00049 PythonProcessDesc pythonProcessDesc(config);
00050 return pythonProcessDesc.processDesc();
00051 }
00052
00053 boost::shared_ptr<edm::ProcessDesc>
00054 readConfig(const std::string & config, int argc, char * argv[])
00055 {
00056 PythonProcessDesc pythonProcessDesc(config, argc, argv);
00057 return pythonProcessDesc.processDesc();
00058 }
00059
00060
00061 void
00062 makeParameterSets(std::string const& configtext,
00063 boost::shared_ptr<ParameterSet>& main,
00064 boost::shared_ptr<std::vector<ParameterSet> >& serviceparams)
00065 {
00066 PythonProcessDesc pythonProcessDesc(configtext);
00067 boost::shared_ptr<edm::ProcessDesc> processDesc = pythonProcessDesc.processDesc();
00068 main = processDesc->getProcessPSet();
00069 serviceparams = processDesc->getServicesPSets();
00070 }
00071
00072 boost::shared_ptr<ParameterSet>
00073 readPSetsFrom(const std::string& module) {
00074 edm::python::initializeModule();
00075
00076 boost::python::object mainModule = object(handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__")))));
00077
00078 boost::python::object mainNamespace = mainModule.attr("__dict__");
00079 PythonParameterSet theProcessPSet;
00080 mainNamespace["topPSet"] = ptr(&theProcessPSet);
00081
00082 try {
00083
00084 if(module.substr(module.size()-3) == ".py")
00085 {
00086 makePSetsFromFile(module,mainNamespace);
00087 }
00088 else
00089 {
00090 makePSetsFromString(module,mainNamespace);
00091 }
00092 }
00093 catch( error_already_set ) {
00094 edm::pythonToCppException("Configuration");
00095 Py_Finalize();
00096 }
00097 boost::shared_ptr<ParameterSet> returnValue(new ParameterSet);
00098 theProcessPSet.pset().swap(*returnValue);
00099 return returnValue;
00100
00101 }
00102
00103 }