The Gaudi Framework  master (594c33fa)
PythonConfig Class Reference

#include </builds/gaudi/Gaudi/GaudiCoreSvc/src/JobOptionsSvc/PythonConfig.h>

Collaboration diagram for PythonConfig:

Public Member Functions

 PythonConfig (Gaudi::Interfaces::IOptionsSvc *optsSvc)
 
StatusCode evaluateConfig (const std::string &filename, const std::string &preAction, const std::string &postAction)
 

Private Attributes

Gaudi::Interfaces::IOptionsSvcm_optsSvc
 

Detailed Description

Definition at line 33 of file PythonConfig.h.

Constructor & Destructor Documentation

◆ PythonConfig()

PythonConfig::PythonConfig ( Gaudi::Interfaces::IOptionsSvc optsSvc)
inline

Definition at line 35 of file PythonConfig.h.

35 : m_optsSvc( optsSvc ) {}

Member Function Documentation

◆ evaluateConfig()

StatusCode PythonConfig::evaluateConfig ( const std::string filename,
const std::string preAction,
const std::string postAction 
)

Definition at line 20 of file PythonConfig.cpp.

21  {
22  try {
23  // Prepare a Python environment
24  Py_Initialize();
25  object main_module( ( handle<>( borrowed( PyImport_AddModule( "__main__" ) ) ) ) );
26  object main_namespace = main_module.attr( "__dict__" );
27 
28  // make the translator class known to Python
29  main_namespace["PythonAdaptor"] =
30  class_<PythonAdaptor>( "PythonAdaptor", boost::python::init<Gaudi::Interfaces::IOptionsSvc*>() )
31  .def( "addPropertyToJobOptions", &PythonAdaptor::addPropertyToJobOptions );
32 
33  // create an instance of it and pass it to Python
34  PythonAdaptor adaptor( m_optsSvc );
35  main_namespace["adaptor"] = ptr( &adaptor );
36 
37  // some python helper
38  std::string command( preAction );
39  command += "\nfor name in '";
40  command += filename + "'.split(','):\n";
41  command += " with open(name) as f:\n";
42  command += " code = compile(f.read(), name, 'exec')\n";
43  command += " exec(code)\n";
44  command += "from GaudiKernel.Configurable import expandvars\nfrom GaudiKernel.Proxy.Configurable import "
45  "applyConfigurableUsers\napplyConfigurableUsers()\n";
46  command += postAction;
47  command += "\nfor n, c in Configurable.allConfigurables.items():\n for p, v in c.getValuedProperties().items() "
48  ":\n v = expandvars(v)\n if type(v) == str : v = repr(v) # need double quotes\n elif "
49  "type(v) == long: v = '%d' % v # prevent pending 'L'\n adaptor.addPropertyToJobOptions(n,p,str(v))";
50 
51  // Now fire off the translation
52  handle<> ignored( ( PyRun_String( command.c_str(), Py_file_input, main_namespace.ptr(), main_namespace.ptr() ) ) );
53  } catch ( const error_already_set& ) {
54  std::cout << "Error in python script:" << std::endl;
55  PyErr_Print();
56  return StatusCode::FAILURE;
57  }
58 
59  return StatusCode::SUCCESS;
60 }

Member Data Documentation

◆ m_optsSvc

Gaudi::Interfaces::IOptionsSvc* PythonConfig::m_optsSvc
private

Definition at line 39 of file PythonConfig.h.


The documentation for this class was generated from the following files:
PythonAdaptor::addPropertyToJobOptions
void addPropertyToJobOptions(const std::string &client, const std::string &name, const std::string &value)
Definition: PythonConfig.h:24
std::string
STL class.
brunelWrapper.command
command
Definition: brunelWrapper.py:135
std::cout
PythonConfig::m_optsSvc
Gaudi::Interfaces::IOptionsSvc * m_optsSvc
Definition: PythonConfig.h:39
PythonAdaptor
Definition: PythonConfig.h:21
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
std::endl
T endl(T... args)
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
graphanalysis.filename
filename
Definition: graphanalysis.py:130