Gaudi Framework, version v23r6

Home   Generated: Wed Jan 30 2013
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PythonScriptingSvc.cpp
Go to the documentation of this file.
1 // $Id: PythonScriptingSvc.cpp,v 1.18 2008/10/27 21:12:08 marcocle Exp $
2 
3 #include "Python.h"
4 
5 // Include Files
9 #include "GaudiKernel/SmartIF.h"
10 
11 #include "PythonScriptingSvc.h"
12 
13 #include <fstream>
14 #include <sstream>
15 
16 // Special for Unixes
17 #if defined(linux)
18  #include "dlfcn.h"
19 #endif
20 
21 // Instantiation of a static factory class used by clients to create
22 // instances of this service
24 
25 //----------------------------------------------------------------------------------
27 //----------------------------------------------------------------------------------
28 : base_class(name, svc) {
29  // Declare the startup script Property
30  declareProperty( "StartupScript", m_startupScript = "" );
31 }
32 
33 //----------------------------------------------------------------------------------
35 //----------------------------------------------------------------------------------
36 
37 //----------------------------------------------------------------------------------
39 //----------------------------------------------------------------------------------
40 {
41  // initialize the Service Base class
43  if ( sc.isFailure() ) return sc;
44 
45  MsgStream log( msgSvc(), name() );
46 
47  // Setup startup script. If none is explicitly specified, then
48  // use the ApplicationMgr JobOptionsPath property as long as
49  // the JobOptionsType property is set to "NONE".
50  if( m_startupScript == "" ) {
52  if ( prpMgr.isValid() ) {
53  StringProperty tmp;
54  tmp.assign(prpMgr->getProperty("JobOptionsType"));
55  if ( tmp.value( ) == "NONE" ) {
56  tmp.assign(prpMgr->getProperty("JobOptionsPath"));
57  m_startupScript = tmp;
58  }
59  }
60  }
61 
62  char* progName[] = { const_cast<char*>("GaudiPython") };
63 
64  // Initialize the Python interpreter. Required.
65  Py_Initialize();
66  // Set argv for Tkinter (needs program name)
67  PySys_SetArgv( 1, progName );
68  // Get the Python version
69  std::string fullversion = Py_GetVersion();
70  std::string version( fullversion, 0, fullversion.find_first_of(' '));
71  std::string vers(version, 0, version.find_first_of('.',version.find_first_of('.')+1));
72  log << MSG::INFO << "Python version: [" << vers << "]" << endmsg;
73 
74 #if defined(linux)
75  // This is hack to make global the python symbols
76  // which are needed by the other python modules
77  // (eg. readline, math, etc,) libraries.
78  std::string libname = "libpython" + vers + ".so";
79  dlopen(libname.c_str(), RTLD_GLOBAL | RTLD_LAZY);
80 #endif
81 
82 
83  // Startup commands
84  PyRun_SimpleString( "from gaudimodule import *" );
85  PyRun_SimpleString( "g = AppMgr()" );
86  // backward compatibility with SIPython
87  PyRun_SimpleString( "theApp = g" );
88  PyRun_SimpleString( "def Service(n): return g.service(n)" );
89  PyRun_SimpleString( "def Algorithm(n): return g.algorithm(n)" );
90  PyRun_SimpleString( "def Property(n): return g.service(n)" );
91  // For command-line completion (unix only)
92 #if !defined( _WIN32 )
93  PyRun_SimpleString( "import rlcompleter");
94  PyRun_SimpleString( "rlcompleter.readline.parse_and_bind('tab: complete')");
95 #endif
96  return StatusCode::SUCCESS;
97 }
98 
99 //----------------------------------------------------------------------------------
101 //----------------------------------------------------------------------------------
102 {
103  // Finalize this specific service
105  if ( sc.isFailure() ) {
106  return sc;
107  }
108 
109  // Shutdown the Python interpreter
110  Py_Finalize();
111  return StatusCode::SUCCESS;
112 }
113 
114 //----------------------------------------------------------------------------------
116 //----------------------------------------------------------------------------------
117 {
118  MsgStream log( msgSvc(), name() );
119  if ( m_startupScript != "" ) {
121  std::stringstream stream;
122  if( file ) {
123  char ch;
124  while( file.get(ch) ) stream.put(ch);
125  PyRun_SimpleString( const_cast<char*>(stream.str().c_str()) );
126  file.close();
127  }
128  else {
129  log << MSG::WARNING << "Python startup file " << m_startupScript << " not found" << endmsg;
130  }
131  }
132  PyRun_InteractiveLoop(stdin, "\0");
133  return StatusCode::SUCCESS;
134 }
135 

Generated at Wed Jan 30 2013 17:13:41 for Gaudi Framework, version v23r6 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004