| The Gaudi Framework v32r1 (f65d50dc)

PythonScriptingSvc.cpp
Go to the documentation of this file.
1 #include "Python.h"
2
3 // Include Files
4 #include "GaudiKernel/ISvcLocator.h"
5 #include "GaudiKernel/MsgStream.h"
6 #include "GaudiKernel/SmartIF.h"
7
8 #include "PythonScriptingSvc.h"
9
10 #include <fstream>
11 #include <sstream>
12
13 // Special for Unixes
14 #if defined( __linux )
15 # include "dlfcn.h"
16 #endif
17
18 // Instantiation of a static factory class used by clients to create
19 // instances of this service
20 DECLARE_COMPONENT( PythonScriptingSvc )
21
22 //----------------------------------------------------------------------------------
23 PythonScriptingSvc::PythonScriptingSvc( const std::string& name, ISvcLocator* svc )
24 //----------------------------------------------------------------------------------
25 : base_class( name, svc ) {
26 // Declare the startup script Property
27 declareProperty( "StartupScript", m_startupScript = "" );
28 }
29
30 //----------------------------------------------------------------------------------
31 PythonScriptingSvc::~PythonScriptingSvc() {}
32 //----------------------------------------------------------------------------------
33
34 //----------------------------------------------------------------------------------
35 StatusCode PythonScriptingSvc::initialize()
36 //----------------------------------------------------------------------------------
37 {
38 // initialize the Service Base class
39 StatusCode sc = Service::initialize();
40 if ( sc.isFailure() ) return sc;
41
42 // Setup startup script. If none is explicitly specified, then
43 // use the ApplicationMgr JobOptionsPath property as long as
44 // the JobOptionsType property is set to "NONE".
45 if ( m_startupScript.empty() ) {
46 auto prpMgr = serviceLocator()->as<IProperty>();
47 if ( prpMgr ) {
48 Gaudi::Property<std::string> tmp;
49 tmp.assign( prpMgr->getProperty( "JobOptionsType" ) );
50 if ( tmp.value() == "NONE" ) {
51 tmp.assign( prpMgr->getProperty( "JobOptionsPath" ) );
52 m_startupScript = tmp;
53 }
54 }
55 }
56
57 char* progName[] = {const_cast<char*>( "GaudiPython" )};
58
59 // Initialize the Python interpreter. Required.
60 Py_Initialize();
61 // Set argv for Tkinter (needs program name)
62 PySys_SetArgv( 1, progName );
63 // Get the Python version
64 std::string fullversion = Py_GetVersion();
65 std::string version( fullversion, 0, fullversion.find_first_of( ' ' ) );
66 std::string vers( version, 0, version.find_first_of( '.', version.find_first_of( '.' ) + 1 ) );
67 info() << "Python version: [" << vers << "]" << endmsg;
68
69 #if defined( __linux )
70 // This is hack to make global the python symbols
71 // which are needed by the other python modules
72 // (eg. readline, math, etc,) libraries.
73 std::string libname = "libpython" + vers + ".so";
74 dlopen( libname.c_str(), RTLD_GLOBAL | RTLD_LAZY );
75 #endif
76
77 // Startup commands
78 PyRun_SimpleString( "from gaudimodule import *" );
79 PyRun_SimpleString( "g = AppMgr()" );
80 // backward compatibility with SIPython
81 PyRun_SimpleString( "theApp = g" );
82 PyRun_SimpleString( "def Service(n): return g.service(n)" );
83 PyRun_SimpleString( "def Algorithm(n): return g.algorithm(n)" );
84 PyRun_SimpleString( "def Property(n): return g.service(n)" );
85 // For command-line completion (unix only)
86 #if !defined( _WIN32 )
87 PyRun_SimpleString( "import rlcompleter" );
88 PyRun_SimpleString( "rlcompleter.readline.parse_and_bind('tab: complete')" );
89 #endif
90 return StatusCode::SUCCESS;
91 }
92
93 //----------------------------------------------------------------------------------
94 StatusCode PythonScriptingSvc::finalize()
95 //----------------------------------------------------------------------------------
96 {
97 // Finalize this specific service
98 StatusCode sc = Service::finalize();
99 if ( sc.isFailure() ) return sc;
100
101 // Shutdown the Python interpreter
102 Py_Finalize();
103 return StatusCode::SUCCESS;
104 }
105
106 //----------------------------------------------------------------------------------
107 StatusCode PythonScriptingSvc::run()
108 //----------------------------------------------------------------------------------
109 {
110 if ( !m_startupScript.empty() ) {
111 std::ifstream file{m_startupScript};
112 std::stringstream stream;
113 if ( file ) {
114 std::string buffer;
115 file.seekg( 0, std::ios::end );
116 buffer.reserve( file.tellg() );
117 file.seekg( 0, std::ios::beg );
118 buffer.assign( ( std::istreambuf_iterator<char>{file} ), std::istreambuf_iterator<char>{} );
119 file.close();
120 PyRun_SimpleString( buffer.c_str() );
121 } else {
122 warning() << "Python startup file " << m_startupScript << " not found" << endmsg;
123 }
124 }
125 PyRun_InteractiveLoop( stdin, "\0" );
126 return StatusCode::SUCCESS;
127 }
PythonScriptingSvc.h
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:60
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:277
std::string::empty
T empty(T... args)
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
PythonScriptingSvc::~PythonScriptingSvc
~PythonScriptingSvc() override
Destructor.
Definition: PythonScriptingSvc.cpp:31
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:41
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:164
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:352
PythonScriptingSvc::run
StatusCode run() override
Run the service by taking full control. [IRunable::run()].
Definition: PythonScriptingSvc.cpp:107
MsgStream.h
CommonMessagingBase::warning
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
Definition: CommonMessaging.h:101
std::istreambuf_iterator
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
std
STL namespace.
ISvcLocator::as
SmartIF< IFace > as()
Definition: ISvcLocator.h:103
CommonMessagingBase::info
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:104
PythonScriptingSvc::finalize
StatusCode finalize() override
Finalize the service. [IService::finalize()].
Definition: PythonScriptingSvc.cpp:94
std::stringstream
STL class.
std::string
STL class.
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
Gaudi::Property::value
const ValueType & value() const
Backward compatibility (.
Definition: Property.h:526
SmartIF.h
PythonScriptingSvc::m_startupScript
std::string m_startupScript
Startup script.
Definition: PythonScriptingSvc.h:35
std::string::find_first_of
T find_first_of(T... args)
PythonScriptingSvc
This service handles scripting implemented using Python.
Definition: PythonScriptingSvc.h:15
IOTest.end
def end
Definition: IOTest.py:112
std::string::assign
T assign(T... args)
std::string::c_str
T c_str(T... args)
PythonScriptingSvc::initialize
StatusCode initialize() override
Initialize the service. [IService::initialize()].
Definition: PythonScriptingSvc.cpp:35
CommonMessaging
Definition: CommonMessaging.h:56
Gaudi::Property::assign
bool assign(const Details::PropertyBase &source) override
get the value from another property
Definition: Property.h:659
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:130
IProperty
The IProperty is the basic interface for all components which have properties that can be set or get.
Definition: IProperty.h:20
ISvcLocator.h
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
std::ifstream
STL class.
std::string::reserve
T reserve(T... args)