The Gaudi Framework  v33r0 (d5ea422b)
PythonConfig.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #include "PythonConfig.h"
12 
13 // boost includes
14 #include "boost/python.hpp"
15 using namespace boost::python;
16 
17 //-----------------------------------------------------------------------------
19  const std::string& postAction ) {
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
31  PythonAdaptor adaptor( m_IJobOptionsSvc );
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
StatusCode evaluateConfig(const std::string &filename, const std::string &preAction, const std::string &postAction)
void addPropertyToJobOptions(const std::string &client, const std::string &name, const std::string &value)
Definition: PythonConfig.h:24
STL class.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
constexpr static const auto FAILURE
Definition: StatusCode.h:97