| The Gaudi Framework v27r1

- 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/MsgStream.h"
5 #include "GaudiKernel/ISvcLocator.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
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 StringProperty 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
79 // Startup commands
80 PyRun_SimpleString( "from gaudimodule import *" );
81 PyRun_SimpleString( "g = AppMgr()" );
82 // backward compatibility with SIPython
83 PyRun_SimpleString( "theApp = g" );
84 PyRun_SimpleString( "def Service(n): return g.service(n)" );
85 PyRun_SimpleString( "def Algorithm(n): return g.algorithm(n)" );
86 PyRun_SimpleString( "def Property(n): return g.service(n)" );
87 // For command-line completion (unix only)
88 #if !defined( _WIN32 )
89 PyRun_SimpleString( "import rlcompleter");
90 PyRun_SimpleString( "rlcompleter.readline.parse_and_bind('tab: complete')");
91 #endif
92 return StatusCode::SUCCESS;
93 }
94
95 //----------------------------------------------------------------------------------
96 StatusCode PythonScriptingSvc::finalize()
97 //----------------------------------------------------------------------------------
98 {
99 // Finalize this specific service
100 StatusCode sc = Service::finalize();
101 if ( sc.isFailure() ) return sc;
102
103 // Shutdown the Python interpreter
104 Py_Finalize();
105 return StatusCode::SUCCESS;
106 }
107
108 //----------------------------------------------------------------------------------
109 StatusCode PythonScriptingSvc::run()
110 //----------------------------------------------------------------------------------
111 {
112 if ( !m_startupScript.empty() ) {
113 std::ifstream file{m_startupScript};
114 std::stringstream stream;
115 if( file ) {
116 std::string buffer;
117 file.seekg(0, std::ios::end);
118 buffer.reserve(file.tellg());
119 file.seekg(0, std::ios::beg);
120 buffer.assign((std::istreambuf_iterator<char>{file}),
121 std::istreambuf_iterator<char>{});
122 file.close();
123 PyRun_SimpleString( buffer.c_str() );
124 } else {
125 warning() << "Python startup file " << m_startupScript << " not found" << endmsg;
126 }
127 }
128 PyRun_InteractiveLoop(stdin, "\0");
129 return StatusCode::SUCCESS;
130 }
PythonScriptingSvc.h
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:68
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:324
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
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:28
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:193
MsgStream.h
CommonMessagingBase::info
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:124
std::istreambuf_iterator
std
STL namespace.
PythonScriptingSvc::finalize
virtual StatusCode finalize()
Finalize the service. [IService::finalize()].
Definition: PythonScriptingSvc.cpp:96
ISvcLocator::as
SmartIF< IFace > as()
Definition: ISvcLocator.h:106
merge_files.sc
int sc
Definition: merge_files.py:167
StatusCode::isFailure
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
std::stringstream
STL class.
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
std::string
STL class.
SimpleProperty< std::string >
CommonMessagingBase::warning
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
Definition: CommonMessaging.h:121
StatusCode::SUCCESS
Definition: StatusCode.h:30
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
SmartIF.h
PythonScriptingSvc::m_startupScript
std::string m_startupScript
Startup script.
Definition: PythonScriptingSvc.h:36
std::string::find_first_of
T find_first_of(T...args)
end
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:47
PythonScriptingSvc
This service handles scripting implemented using Python.
Definition: PythonScriptingSvc.h:15
PythonScriptingSvc::~PythonScriptingSvc
virtual ~PythonScriptingSvc()
Destructor.
Definition: PythonScriptingSvc.cpp:31
ana.file
list file
Definition: ana.py:160
PropertyWithValue::value
const TYPE & value() const
explicit conversion
Definition: Property.h:341
std::string::assign
T assign(T...args)
PropertyWithValue::assign
bool assign(const Property &source) override
get the value from another property
Definition: Property.h:269
std::string::c_str
T c_str(T...args)
PythonScriptingSvc::run
virtual StatusCode run()
Run the service by taking full control. [IRunable::run()].
Definition: PythonScriptingSvc.cpp:109
CommonMessaging
Definition: CommonMessaging.h:58
IProperty
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:21
ISvcLocator.h
PythonScriptingSvc::initialize
virtual StatusCode initialize()
Initialize the service. [IService::initialize()].
Definition: PythonScriptingSvc.cpp:35
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
std::ifstream
STL class.
std::string::reserve
T reserve(T...args)