The Gaudi Framework  v33r0 (d5ea422b)
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 33 of file PythonConfig.h.

Constructor & Destructor Documentation

◆ PythonConfig()

PythonConfig::PythonConfig ( IJobOptionsSvc jos)
inline

Definition at line 35 of file PythonConfig.h.

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

Member Function Documentation

◆ evaluateConfig()

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

Definition at line 18 of file PythonConfig.cpp.

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

Member Data Documentation

◆ m_IJobOptionsSvc

SmartIF<IJobOptionsSvc> PythonConfig::m_IJobOptionsSvc
private

Definition at line 39 of file PythonConfig.h.


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