CMS 3D CMS Logo

Public Member Functions | Private Member Functions | Private Attributes

PythonProcessDesc Class Reference

#include <PythonProcessDesc.h>

List of all members.

Public Member Functions

void addService (PythonParameterSet &pset)
std::string dump () const
PythonParameterSet newPSet () const
boost::shared_ptr
< edm::ProcessDesc
processDesc ()
 PythonProcessDesc (std::string const &config)
 PythonProcessDesc ()
 PythonProcessDesc (std::string const &config, int argc, char *argv[])

Private Member Functions

void prepareToRead ()
void read (std::string const &config)
void readFile (std::string const &fileName)
void readString (std::string const &pyConfig)

Private Attributes

boost::python::object theMainModule
boost::python::object theMainNamespace
PythonParameterSet theProcessPSet
std::vector< PythonParameterSettheServices

Detailed Description

Definition at line 16 of file PythonProcessDesc.h.


Constructor & Destructor Documentation

PythonProcessDesc::PythonProcessDesc ( )

Definition at line 11 of file PythonProcessDesc.cc.

PythonProcessDesc::PythonProcessDesc ( std::string const &  config)

This constructor will parse the given file or string and create two objects in python-land: a PythonProcessDesc named 'processDesc' a PythonParameterSet named 'processPSet' It decides whether it's a file or string by seeing if it ends in '.py'

Definition at line 20 of file PythonProcessDesc.cc.

References prepareToRead(), and read().

PythonProcessDesc::PythonProcessDesc ( std::string const &  config,
int  argc,
char *  argv[] 
)

Definition at line 31 of file PythonProcessDesc.cc.

References prepareToRead(), and read().

:  theProcessPSet(),
   theServices(),
   theMainModule(),
   theMainNamespace()
{
  prepareToRead();
  PySys_SetArgv(argc, argv);
  read(config);
  Py_Finalize();
}

Member Function Documentation

void PythonProcessDesc::addService ( PythonParameterSet pset) [inline]

Definition at line 31 of file PythonProcessDesc.h.

References theServices.

Referenced by BOOST_PYTHON_MODULE().

{theServices.push_back(pset);}
std::string PythonProcessDesc::dump ( void  ) const

Definition at line 117 of file PythonProcessDesc.cc.

References PythonParameterSet::dump(), theProcessPSet, and theServices.

Referenced by BOOST_PYTHON_MODULE().

{
  std::ostringstream os;
  os << theProcessPSet.dump();
  BOOST_FOREACH(PythonParameterSet service, theServices)
  {
    os << service.dump();
  }
  return os.str();
}
PythonParameterSet PythonProcessDesc::newPSet ( ) const [inline]

Definition at line 33 of file PythonProcessDesc.h.

Referenced by BOOST_PYTHON_MODULE().

{return PythonParameterSet();}
void PythonProcessDesc::prepareToRead ( ) [private]

Definition at line 44 of file PythonProcessDesc.cc.

References edm::python::initializeModule(), dbtoconf::object, theMainModule, theMainNamespace, and theProcessPSet.

Referenced by PythonProcessDesc().

{
  edm::python::initializeModule();

  theMainModule = object(handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__")))));

  theMainNamespace = theMainModule.attr("__dict__");
  theMainNamespace["processDesc"] = ptr(this);
  theMainNamespace["processPSet"] = ptr(&theProcessPSet);
}
boost::shared_ptr< edm::ProcessDesc > PythonProcessDesc::processDesc ( )
void PythonProcessDesc::read ( std::string const &  config) [private]

Definition at line 56 of file PythonProcessDesc.cc.

References pythonToCppException(), readFile(), and readString().

Referenced by PythonProcessDesc().

{  
  try {
    // if it ends with py, it's a file
    if(config.substr(config.size()-3) == ".py")
    {
      readFile(config);
    }
    else
    {
      readString(config);
    }
  }
  catch( error_already_set ) {
     edm::pythonToCppException("Configuration");
     Py_Finalize();
  }
}
void PythonProcessDesc::readFile ( std::string const &  fileName) [private]

Definition at line 76 of file PythonProcessDesc.cc.

References edmPickEvents::command, and theMainNamespace.

Referenced by read().

{
  std::string initCommand("import FWCore.ParameterSet.Config as cms\n"
                      "execfile('");
  initCommand += fileName + "')";


  handle<>(PyRun_String(initCommand.c_str(),
                        Py_file_input,
                        theMainNamespace.ptr(),
                        theMainNamespace.ptr()));
  std::string command("process.fillProcessDesc(processDesc, processPSet)");
  handle<>(PyRun_String(command.c_str(),
                        Py_eval_input,
                        theMainNamespace.ptr(),
                        theMainNamespace.ptr()));
}
void PythonProcessDesc::readString ( std::string const &  pyConfig) [private]

Definition at line 95 of file PythonProcessDesc.cc.

References edmPickEvents::command, and theMainNamespace.

Referenced by read().

{
  std::string command = pyConfig;
  command += "\nprocess.fillProcessDesc(processDesc, processPSet)";
  handle<>(PyRun_String(command.c_str(),
                        Py_file_input,
                        theMainNamespace.ptr(),
                        theMainNamespace.ptr()));
}

Member Data Documentation

boost::python::object PythonProcessDesc::theMainModule [private]

Definition at line 48 of file PythonProcessDesc.h.

Referenced by prepareToRead().

boost::python::object PythonProcessDesc::theMainNamespace [private]

Definition at line 49 of file PythonProcessDesc.h.

Referenced by prepareToRead(), readFile(), and readString().

Definition at line 46 of file PythonProcessDesc.h.

Referenced by dump(), prepareToRead(), and processDesc().

Definition at line 47 of file PythonProcessDesc.h.

Referenced by addService(), dump(), and processDesc().