The Gaudi Framework
master (37c0b60a)
PythonConfig.cpp
Go to the documentation of this file.
1
/***********************************************************************************\
2
* (c) Copyright 1998-2024 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
#include <iostream>
14
15
// boost includes
16
#include <boost/python.hpp>
17
using namespace
boost::python;
18
19
//-----------------------------------------------------------------------------
20
StatusCode
PythonConfig::evaluateConfig
(
const
std::string
&
filename
,
const
std::string
& preAction,
21
const
std::string
& postAction ) {
22
try
{
23
// Prepare a Python environment
24
Py_Initialize();
25
object
main_module( ( handle<>( borrowed( PyImport_AddModule(
"__main__"
) ) ) ) );
26
object
main_namespace = main_module.attr(
"__dict__"
);
27
28
// make the translator class known to Python
29
main_namespace[
"PythonAdaptor"
] =
30
class_<PythonAdaptor>(
"PythonAdaptor"
, boost::python::init<Gaudi::Interfaces::IOptionsSvc*>() )
31
.def(
"addPropertyToJobOptions"
, &
PythonAdaptor::addPropertyToJobOptions
);
32
33
// create an instance of it and pass it to Python
34
PythonAdaptor
adaptor( m_optsSvc );
35
main_namespace[
"adaptor"
] = ptr( &adaptor );
36
37
// some python helper
38
std::string
command
( preAction );
39
command
+=
"\nfor name in '"
;
40
command
+=
filename
+
"'.split(','):\n"
;
41
command
+=
" with open(name) as f:\n"
;
42
command
+=
" code = compile(f.read(), name, 'exec')\n"
;
43
command
+=
" exec(code)\n"
;
44
command
+=
"from GaudiKernel.Configurable import expandvars\nfrom GaudiKernel.Proxy.Configurable import "
45
"applyConfigurableUsers\napplyConfigurableUsers()\n"
;
46
command
+= postAction;
47
command
+=
"\nfor n, c in Configurable.allConfigurables.items():\n for p, v in c.getValuedProperties().items() "
48
":\n v = expandvars(v)\n if type(v) == str : v = repr(v) # need double quotes\n elif "
49
"type(v) == long: v = '%d' % v # prevent pending 'L'\n adaptor.addPropertyToJobOptions(n,p,str(v))"
;
50
51
// Now fire off the translation
52
handle<> ignored( ( PyRun_String(
command
.c_str(), Py_file_input, main_namespace.ptr(), main_namespace.ptr() ) ) );
53
}
catch
(
const
error_already_set& ) {
54
std::cout
<<
"Error in python script:"
<<
std::endl
;
55
PyErr_Print();
56
return
StatusCode::FAILURE
;
57
}
58
59
return
StatusCode::SUCCESS
;
60
}
PythonAdaptor::addPropertyToJobOptions
void addPropertyToJobOptions(const std::string &client, const std::string &name, const std::string &value)
Definition:
PythonConfig.h:24
std::string
STL class.
PythonConfig::evaluateConfig
StatusCode evaluateConfig(const std::string &filename, const std::string &preAction, const std::string &postAction)
Definition:
PythonConfig.cpp:20
brunelWrapper.command
command
Definition:
brunelWrapper.py:135
StatusCode
Definition:
StatusCode.h:65
std::cout
PythonAdaptor
Definition:
PythonConfig.h:21
PythonConfig.h
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition:
StatusCode.h:100
std::endl
T endl(T... args)
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition:
StatusCode.h:101
graphanalysis.filename
filename
Definition:
graphanalysis.py:130
GaudiCoreSvc
src
JobOptionsSvc
PythonConfig.cpp
Generated on Thu Dec 19 2024 15:34:58 for The Gaudi Framework by
1.8.18