| The Gaudi Framework v30r0 (c919700c)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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 {
27 // Declare the startup script Property
28 declareProperty( "StartupScript", m_startupScript = "" );
29 }
30
31 //----------------------------------------------------------------------------------
32 PythonScriptingSvc::~PythonScriptingSvc() {}
33 //----------------------------------------------------------------------------------
34
35 //----------------------------------------------------------------------------------
36 StatusCode PythonScriptingSvc::initialize()
37 //----------------------------------------------------------------------------------
38 {
39 // initialize the Service Base class
40 StatusCode sc = Service::initialize();
41 if ( sc.isFailure() ) return sc;
42
43 // Setup startup script. If none is explicitly specified, then
44 // use the ApplicationMgr JobOptionsPath property as long as
45 // the JobOptionsType property is set to "NONE".
46 if ( m_startupScript.empty() ) {
47 auto prpMgr = serviceLocator()->as<IProperty>();
48 if ( prpMgr ) {
49 Gaudi::Property<std::string> tmp;
50 tmp.assign( prpMgr->getProperty( "JobOptionsType" ) );
51 if ( tmp.value() == "NONE" ) {
52 tmp.assign( prpMgr->getProperty( "JobOptionsPath" ) );
53 m_startupScript = tmp;
54 }
55 }
56 }
57
58 char* progName[] = {const_cast<char*>( "GaudiPython" )};
59
60 // Initialize the Python interpreter. Required.
61 Py_Initialize();
62 // Set argv for Tkinter (needs program name)
63 PySys_SetArgv( 1, progName );
64 // Get the Python version
65 std::string fullversion = Py_GetVersion();
66 std::string version( fullversion, 0, fullversion.find_first_of( ' ' ) );
67 std::string vers( version, 0, version.find_first_of( '.', version.find_first_of( '.' ) + 1 ) );
68 info() << "Python version: [" << vers << "]" << endmsg;
69
70 #if defined( __linux )
71 // This is hack to make global the python symbols
72 // which are needed by the other python modules
73 // (eg. readline, math, etc,) libraries.
74 std::string libname = "libpython" + vers + ".so";
75 dlopen( libname.c_str(), RTLD_GLOBAL | RTLD_LAZY );
76 #endif
77
78 // Startup commands
79 PyRun_SimpleString( "from gaudimodule import *" );
80 PyRun_SimpleString( "g = AppMgr()" );
81 // backward compatibility with SIPython
82 PyRun_SimpleString( "theApp = g" );
83 PyRun_SimpleString( "def Service(n): return g.service(n)" );
84 PyRun_SimpleString( "def Algorithm(n): return g.algorithm(n)" );
85 PyRun_SimpleString( "def Property(n): return g.service(n)" );
86 // For command-line completion (unix only)
87 #if !defined( _WIN32 )
88 PyRun_SimpleString( "import rlcompleter" );
89 PyRun_SimpleString( "rlcompleter.readline.parse_and_bind('tab: complete')" );
90 #endif
91 return StatusCode::SUCCESS;
92 }
93
94 //----------------------------------------------------------------------------------
95 StatusCode PythonScriptingSvc::finalize()
96 //----------------------------------------------------------------------------------
97 {
98 // Finalize this specific service
99 StatusCode sc = Service::finalize();
100 if ( sc.isFailure() ) return sc;
101
102 // Shutdown the Python interpreter
103 Py_Finalize();
104 return StatusCode::SUCCESS;
105 }
106
107 //----------------------------------------------------------------------------------
108 StatusCode PythonScriptingSvc::run()
109 //----------------------------------------------------------------------------------
110 {
111 if ( !m_startupScript.empty() ) {
112 std::ifstream file{m_startupScript};
113 std::stringstream stream;
114 if ( file ) {
115 std::string buffer;
116 file.seekg( 0, std::ios::end );
117 buffer.reserve( file.tellg() );
118 file.seekg( 0, std::ios::beg );
119 buffer.assign( ( std::istreambuf_iterator<char>{file} ), std::istreambuf_iterator<char>{} );
120 file.close();
121 PyRun_SimpleString( buffer.c_str() );
122 } else {
123 warning() << "Python startup file " << m_startupScript << " not found" << endmsg;
124 }
125 }
126 PyRun_InteractiveLoop( stdin, "\0" );
127 return StatusCode::SUCCESS;
128 }
PythonScriptingSvc.h
StatusCode::SUCCESS
Definition: StatusCode.h:29
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:64
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:32
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:32
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:174
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:319
PythonScriptingSvc::run
StatusCode run() override
Run the service by taking full control. [IRunable::run()].
Definition: PythonScriptingSvc.cpp:108
MsgStream.h
CommonMessagingBase::info
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:112
std::istreambuf_iterator
std
STL namespace.
ISvcLocator::as
SmartIF< IFace > as()
Definition: ISvcLocator.h:109
PythonScriptingSvc::finalize
StatusCode finalize() override
Finalize the service. [IService::finalize()].
Definition: PythonScriptingSvc.cpp:95
StatusCode::isFailure
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:61
std::stringstream
STL class.
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:33
std::string
STL class.
CommonMessagingBase::warning
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
Definition: CommonMessaging.h:109
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none")
Declare a property (templated)
SmartIF.h
PythonScriptingSvc::m_startupScript
std::string m_startupScript
Startup script.
Definition: PythonScriptingSvc.h:37
std::string::find_first_of
T find_first_of(T...args)
end
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:64
PythonScriptingSvc
This service handles scripting implemented using Python.
Definition: PythonScriptingSvc.h:15
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:36
CommonMessaging
Definition: CommonMessaging.h:62
Gaudi::Property::assign
bool assign(const Details::PropertyBase &source) override
get the value from another property
Definition: Property.h:630
Gaudi::Property::value
const ValueType & value() const
Backward compatibility (.
Definition: Property.h:478
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
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:292
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
std::ifstream
STL class.
std::string::reserve
T reserve(T...args)