The Gaudi Framework  v32r2 (46d42edc)
PythonConfig Class Reference

#include <src/JobOptionsSvc/PythonConfig.h>

Collaboration diagram for PythonConfig:

Public Member Functions

 PythonConfig (IJobOptionsSvc *jos)
 
StatusCode evaluateConfig (const std::string &filename, const std::string &preAction, const std::string &postAction)
 

Private Attributes

SmartIF< IJobOptionsSvcm_IJobOptionsSvc
 

Detailed Description

Definition at line 23 of file PythonConfig.h.

Constructor & Destructor Documentation

◆ PythonConfig()

PythonConfig::PythonConfig ( IJobOptionsSvc jos)
inline

Definition at line 25 of file PythonConfig.h.

25 : m_IJobOptionsSvc( jos ){};
SmartIF< IJobOptionsSvc > m_IJobOptionsSvc
Definition: PythonConfig.h:29

Member Function Documentation

◆ evaluateConfig()

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

Definition at line 8 of file PythonConfig.cpp.

9  {
10  try {
11  // Prepare a Python environment
12  Py_Initialize();
13  object main_module( ( handle<>( borrowed( PyImport_AddModule( "__main__" ) ) ) ) );
14  object main_namespace = main_module.attr( "__dict__" );
15 
16  // make the translator class known to Python
17  main_namespace["PythonAdaptor"] = class_<PythonAdaptor>( "PythonAdaptor", boost::python::init<IJobOptionsSvc*>() )
18  .def( "addPropertyToJobOptions", &PythonAdaptor::addPropertyToJobOptions );
19 
20  // create an instance of it and pass it to Python
22  main_namespace["adaptor"] = ptr( &adaptor );
23 
24  // some python helper
25  std::string command( preAction );
26  command += "\nfor name in '";
27  command += filename + "'.split(','):\n";
28  command += " with open(name) as f:\n";
29  command += " code = compile(f.read(), name, 'exec')\n";
30  command += " exec(code)\n";
31  command += "from GaudiKernel.Configurable import expandvars\nfrom GaudiKernel.Proxy.Configurable import "
32  "applyConfigurableUsers\napplyConfigurableUsers()\n";
33  command += postAction;
34  command += "\nfor n, c in Configurable.allConfigurables.items():\n for p, v in c.getValuedProperties().items() "
35  ":\n v = expandvars(v)\n if type(v) == str : v = '\"%s\"' % v # need double quotes\n elif "
36  "type(v) == long: v = '%d' % v # prevent pending 'L'\n adaptor.addPropertyToJobOptions(n,p,str(v))";
37 
38  // Now fire off the translation
39  handle<> ignored( ( PyRun_String( command.c_str(), Py_file_input, main_namespace.ptr(), main_namespace.ptr() ) ) );
40  } catch ( const error_already_set& ) {
41  std::cout << "Error in python script:" << std::endl;
42  PyErr_Print();
43  return StatusCode::FAILURE;
44  }
45 
46  return StatusCode::SUCCESS;
47 }
T endl(T... args)
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
void addPropertyToJobOptions(const std::string &client, const std::string &name, const std::string &value)
Definition: PythonConfig.h:14
STL class.
SmartIF< IJobOptionsSvc > m_IJobOptionsSvc
Definition: PythonConfig.h:29
constexpr static const auto FAILURE
Definition: StatusCode.h:86

Member Data Documentation

◆ m_IJobOptionsSvc

SmartIF<IJobOptionsSvc> PythonConfig::m_IJobOptionsSvc
private

Definition at line 29 of file PythonConfig.h.


The documentation for this class was generated from the following files: