CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/FWCore/PythonParameterSet/src/PythonProcessDesc.cc

Go to the documentation of this file.
00001 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00002 #include "FWCore/ParameterSet/interface/ProcessDesc.h"
00003 #include "FWCore/PythonParameterSet/interface/PythonProcessDesc.h"
00004 #include "FWCore/PythonParameterSet/src/initializeModule.h"
00005 #include "FWCore/PythonParameterSet/src/PythonWrapper.h"
00006 
00007 #include <sstream>
00008 
00009 using namespace boost::python;
00010 
00011 PythonProcessDesc::PythonProcessDesc() :
00012    theProcessPSet(),
00013    theMainModule(),
00014    theMainNamespace() {
00015 }
00016 
00017 PythonProcessDesc::PythonProcessDesc(std::string const& config) :
00018    theProcessPSet(),
00019    theMainModule(),
00020    theMainNamespace() {
00021   prepareToRead();
00022   read(config);
00023   Py_Finalize();
00024 }
00025 
00026 PythonProcessDesc::PythonProcessDesc(std::string const& config, int argc, char* argv[]) :
00027    theProcessPSet(),
00028    theMainModule(),
00029    theMainNamespace() {
00030   prepareToRead();
00031   PySys_SetArgv(argc, argv);
00032   read(config);
00033   Py_Finalize();
00034 }
00035 
00036 void PythonProcessDesc::prepareToRead() {
00037   edm::python::initializeModule();
00038 
00039   theMainModule = object(handle<>(borrowed(PyImport_AddModule(const_cast<char*>("__main__")))));
00040 
00041   theMainNamespace = theMainModule.attr("__dict__");
00042   theMainNamespace["processDesc"] = ptr(this);
00043   theMainNamespace["processPSet"] = ptr(&theProcessPSet);
00044 }
00045 
00046 void PythonProcessDesc::read(std::string const& config) {
00047   try {
00048     // if it ends with py, it's a file
00049     if(config.substr(config.size()-3) == ".py") {
00050       readFile(config);
00051     } else {
00052       readString(config);
00053     }
00054   }
00055   catch(error_already_set) {
00056      edm::pythonToCppException("Configuration");
00057      Py_Finalize();
00058   }
00059 }
00060 
00061 void PythonProcessDesc::readFile(std::string const& fileName) {
00062   std::string initCommand("import FWCore.ParameterSet.Config as cms\n"
00063                           "execfile('");
00064   initCommand += fileName + "')";
00065 
00066   handle<>(PyRun_String(initCommand.c_str(),
00067                         Py_file_input,
00068                         theMainNamespace.ptr(),
00069                         theMainNamespace.ptr()));
00070   std::string command("process.fillProcessDesc(processPSet)");
00071   handle<>(PyRun_String(command.c_str(),
00072                         Py_eval_input,
00073                         theMainNamespace.ptr(),
00074                         theMainNamespace.ptr()));
00075 }
00076 
00077 void PythonProcessDesc::readString(std::string const& pyConfig) {
00078   std::string command = pyConfig;
00079   command += "\nprocess.fillProcessDesc(processPSet)";
00080   handle<>(PyRun_String(command.c_str(),
00081                         Py_file_input,
00082                         theMainNamespace.ptr(),
00083                         theMainNamespace.ptr()));
00084 }
00085 
00086 boost::shared_ptr<edm::ParameterSet> PythonProcessDesc::parameterSet() {
00087   boost::shared_ptr<edm::ParameterSet> result(new edm::ParameterSet(theProcessPSet.pset()));
00088   return result;
00089 }
00090 
00091 std::string PythonProcessDesc::dump() const {
00092   std::ostringstream os;
00093   os << theProcessPSet.dump();
00094   return os.str();
00095 }
00096 
00097 // For backward compatibility only.  Remove when no longer used.
00098 boost::shared_ptr<edm::ProcessDesc> PythonProcessDesc::processDesc() {
00099   boost::shared_ptr<edm::ProcessDesc> result(new edm::ProcessDesc(parameterSet()));
00100   return result;
00101 }