The Gaudi Framework
master (bdbe7744)
Toggle main menu visibility
Loading...
Searching...
No Matches
PythonConfig.cpp
Go to the documentation of this file.
1
/***********************************************************************************\
2
* (c) Copyright 1998-2025 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
#include <boost/python.hpp>
13
#include <iostream>
14
15
using namespace
boost::python;
16
17
StatusCode
PythonConfig::evaluateConfig
(
const
std::string& filename,
const
std::string& preAction,
18
const
std::string& postAction ) {
19
try
{
20
// Prepare a Python environment
21
Py_Initialize();
22
object
main_module( ( handle<>( borrowed( PyImport_AddModule(
"__main__"
) ) ) ) );
23
object
main_namespace = main_module.attr(
"__dict__"
);
24
25
// make the translator class known to Python
26
main_namespace[
"PythonAdaptor"
] =
27
class_<PythonAdaptor>(
"PythonAdaptor"
, boost::python::init<Gaudi::Interfaces::IOptionsSvc*>() )
28
.def(
"addPropertyToJobOptions"
, &
PythonAdaptor::addPropertyToJobOptions
);
29
30
// create an instance of it and pass it to Python
31
PythonAdaptor
adaptor(
m_optsSvc
);
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 = repr(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
}
PythonConfig.h
PythonAdaptor
Definition
PythonConfig.h:18
PythonAdaptor::addPropertyToJobOptions
void addPropertyToJobOptions(const std::string &client, const std::string &name, const std::string &value)
Definition
PythonConfig.h:21
PythonConfig::evaluateConfig
StatusCode evaluateConfig(const std::string &filename, const std::string &preAction, const std::string &postAction)
Definition
PythonConfig.cpp:17
PythonConfig::m_optsSvc
Gaudi::Interfaces::IOptionsSvc * m_optsSvc
Definition
PythonConfig.h:36
StatusCode
This class is used for returning status codes from appropriate routines.
Definition
StatusCode.h:64
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition
StatusCode.h:99
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition
StatusCode.h:100
GaudiCoreSvc
src
JobOptionsSvc
PythonConfig.cpp
Generated on
for The Gaudi Framework by
1.17.0