CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Static Public Member Functions | Private Attributes
PythonService Class Reference

#include <FWCore/Python/src/PythonService.h>

Public Member Functions

void postBeginJob ()
 
void postEndJob ()
 
void postProcessEvent (const edm::Event &, const edm::EventSetup &)
 
void preProcessEvent (const edm::EventID &, const edm::Timestamp &)
 
 PythonService (const edm::ParameterSet &, edm::ActivityRegistry &)
 
 ~PythonService ()
 

Static Public Member Functions

static void fillDescriptions (edm::ConfigurationDescriptions &descriptions)
 

Private Attributes

std::string command_
 
std::string fileName_
 
PythonManagerHandle handle_
 
boost::python::object service_
 

Detailed Description

Description: interface to write EDMService in Python

Usage: <usage>

Definition at line 32 of file PythonService.h.

Constructor & Destructor Documentation

PythonService::PythonService ( const edm::ParameterSet iConfig,
edm::ActivityRegistry iRegistry 
)

Definition at line 24 of file PythonService.cc.

References command_, gather_cfg::cout, edm::hlt::Exception, convertXMLtoSQLite_cfg::fileName, edm::ParameterSet::getParameter(), postBeginJob(), postEndJob(), postProcessEvent(), query::result, service_, edm::ActivityRegistry::watchPostBeginJob(), edm::ActivityRegistry::watchPostEndJob(), and edm::ActivityRegistry::watchPostProcessEvent().

24  :
26 
27  std::cout << "Start preparing PythonService" << std::endl;
28  std::string const fileName = iConfig.getParameter<std::string>("fileName");
29 
30  using namespace boost::python;
31 
32  command_ = "from " + fileName + " import *\n";
33  object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char*>("__main__"))))));
34  object main_namespace = main_module.attr("__dict__");
35  try {
36  object result((boost::python::handle<>(PyRun_String(command_.c_str(),
37  Py_file_input,
38  main_namespace.ptr(),
39  main_namespace.ptr()))));
40  service_ = main_namespace["service"];
41  } catch(...) {
42  throw cms::Exception("Configuration") << "No 'service' python variable defined in given fileName parameter.\n Please create an instance of the python class you want to use and pass that instance to the variable named 'service'.";
43  }
44 
45  // connect methods and signals
46  // later on here will be a check what python methods are present
47  // for now we expect postBeginJob, postEndJob and postProcessEvent
48 
52 }
T getParameter(std::string const &) const
void watchPostEndJob(PostEndJob::slot_type const &iSlot)
PythonManagerHandle handle_
Definition: PythonService.h:44
void watchPostProcessEvent(PostProcessEvent::slot_type const &iSlot)
tuple result
Definition: query.py:137
boost::python::object service_
Definition: PythonService.h:45
void postBeginJob()
static PythonManagerHandle handle()
tuple cout
Definition: gather_cfg.py:121
void postProcessEvent(const edm::Event &, const edm::EventSetup &)
void watchPostBeginJob(PostBeginJob::slot_type const &iSlot)
convenience function for attaching to signal
std::string command_
Definition: PythonService.h:47
PythonService::~PythonService ( )

Definition at line 57 of file PythonService.cc.

57  {
58 }

Member Function Documentation

void PythonService::fillDescriptions ( edm::ConfigurationDescriptions descriptions)
static

Definition at line 115 of file PythonService.cc.

References edm::ParameterSetDescription::add(), and edm::ConfigurationDescriptions::addDefault().

115  {
117  desc.add<std::string>("fileName");
118  descriptions.addDefault(desc);
119 }
void addDefault(ParameterSetDescription const &psetDescription)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void PythonService::postBeginJob ( )

Definition at line 63 of file PythonService.cc.

References pythonToCppException(), query::result, and service_.

Referenced by PythonService().

63  {
64  using namespace boost::python;
65  object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));
66  object main_namespace = main_module.attr("__dict__");
67  main_namespace["tempService"] = service_;
68 
69  try {
70  object result((boost::python::handle<>(PyRun_String("tempService.postBeginJob()",
71  Py_eval_input,
72  main_namespace.ptr(),
73  main_namespace.ptr()))));
74  } catch(error_already_set) {
75  pythonToCppException("RuntimeError");
76  }
77 }
void pythonToCppException(const std::string &iType)
Definition: PythonManager.cc:5
tuple result
Definition: query.py:137
boost::python::object service_
Definition: PythonService.h:45
void PythonService::postEndJob ( )

Definition at line 79 of file PythonService.cc.

References pythonToCppException(), query::result, and service_.

Referenced by PythonService().

79  {
80  using namespace boost::python;
81  object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));
82  object main_namespace = main_module.attr("__dict__");
83  main_namespace["tempService"] = service_;
84 
85  try {
86  object result((boost::python::handle<>(PyRun_String("tempService.postEndJob()",
87  Py_eval_input,
88  main_namespace.ptr(),
89  main_namespace.ptr()))));
90  } catch(error_already_set) {
91  pythonToCppException("RuntimeError");
92  }
93 }
void pythonToCppException(const std::string &iType)
Definition: PythonManager.cc:5
tuple result
Definition: query.py:137
boost::python::object service_
Definition: PythonService.h:45
void PythonService::postProcessEvent ( const edm::Event ,
const edm::EventSetup  
)

Definition at line 98 of file PythonService.cc.

References pythonToCppException(), query::result, and service_.

Referenced by PythonService().

98  {
99  using namespace boost::python;
100  object main_module((boost::python::handle<>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));
101  object main_namespace = main_module.attr("__dict__");
102  main_namespace["tempService"] = service_;
103 
104  try {
105  object result((boost::python::handle<>(PyRun_String("tempService.postProcessEvent()",
106  Py_eval_input,
107  main_namespace.ptr(),
108  main_namespace.ptr()))));
109  } catch(error_already_set) {
110  pythonToCppException("RuntimeError");
111  }
112 
113 }
void pythonToCppException(const std::string &iType)
Definition: PythonManager.cc:5
tuple result
Definition: query.py:137
boost::python::object service_
Definition: PythonService.h:45
void PythonService::preProcessEvent ( const edm::EventID ,
const edm::Timestamp  
)

Definition at line 95 of file PythonService.cc.

95  {
96 }

Member Data Documentation

std::string PythonService::command_
private

Definition at line 47 of file PythonService.h.

Referenced by PythonService().

std::string PythonService::fileName_
private

Definition at line 46 of file PythonService.h.

PythonManagerHandle PythonService::handle_
private

Definition at line 44 of file PythonService.h.

boost::python::object PythonService::service_
private

Definition at line 45 of file PythonService.h.

Referenced by postBeginJob(), postEndJob(), postProcessEvent(), and PythonService().