CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/FWCore/PythonParameterSet/src/MakeParameterSets.cc

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