| The Gaudi Framework v32r1 (f65d50dc)

PythonConfig.cpp
Go to the documentation of this file.
1 #include "PythonConfig.h"
2
3 // boost includes
4 #include "boost/python.hpp"
5 using namespace boost::python;
6
7 //-----------------------------------------------------------------------------
8 StatusCode PythonConfig::evaluateConfig( const std::string& filename, const std::string& preAction,
9 const std::string& postAction ) {
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
21 PythonAdaptor adaptor( m_IJobOptionsSvc );
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(','): execfile(name)\n";
28 command += "from GaudiKernel.Configurable import expandvars\nfrom GaudiKernel.Proxy.Configurable import "
29 "applyConfigurableUsers\napplyConfigurableUsers()\n";
30 command += postAction;
31 command += "\nfor n, c in Configurable.allConfigurables.items():\n for p, v in c.getValuedProperties().items() "
32 ":\n v = expandvars(v)\n if type(v) == str : v = '\"%s\"' % v # need double quotes\n elif "
33 "type(v) == long: v = '%d' % v # prevent pending 'L'\n adaptor.addPropertyToJobOptions(n,p,str(v))";
34
35 // Now fire off the translation
36 handle<> ignored( ( PyRun_String( command.c_str(), Py_file_input, main_namespace.ptr(), main_namespace.ptr() ) ) );
37 } catch ( const error_already_set& ) {
38 std::cout << "Error in python script:" << std::endl;
39 PyErr_Print();
40 return StatusCode::FAILURE;
41 }
42
43 return StatusCode::SUCCESS;
44 }
PythonConfig.h
std::endl
T endl(T... args)
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
graphanalysis.filename
string filename
Definition: graphanalysis.py:119
PythonConfig::evaluateConfig
StatusCode evaluateConfig(const std::string &filename, const std::string &preAction, const std::string &postAction)
Definition: PythonConfig.cpp:8
PythonAdaptor::addPropertyToJobOptions
void addPropertyToJobOptions(const std::string &client, const std::string &name, const std::string &value)
Definition: PythonConfig.h:14
std::string
STL class.
brunelWrapper.command
string command
Definition: brunelWrapper.py:108
PythonAdaptor
Definition: PythonConfig.h:11
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
std::cout