Gaudi Framework, version v23r4

Home   Generated: Mon Sep 17 2012

PythonScriptingSvc.cpp

Go to the documentation of this file.
00001 // $Id: PythonScriptingSvc.cpp,v 1.18 2008/10/27 21:12:08 marcocle Exp $
00002 
00003 #include "Python.h"
00004 
00005 // Include Files
00006 #include "GaudiKernel/MsgStream.h"
00007 #include "GaudiKernel/ISvcLocator.h"
00008 #include "GaudiKernel/SvcFactory.h"
00009 #include "GaudiKernel/SmartIF.h"
00010 
00011 #include "PythonScriptingSvc.h"
00012 
00013 #include <fstream>
00014 #include <sstream>
00015 
00016 // Special for Unixes
00017 #if defined(linux)
00018   #include "dlfcn.h"
00019 #endif
00020 
00021 // Instantiation of a static factory class used by clients to create
00022 //  instances of this service
00023 DECLARE_SERVICE_FACTORY(PythonScriptingSvc)
00024 
00025 //----------------------------------------------------------------------------------
00026 PythonScriptingSvc::PythonScriptingSvc( const std::string& name, ISvcLocator* svc )
00027 //----------------------------------------------------------------------------------
00028 : base_class(name, svc) {
00029   // Declare the startup script Property
00030   declareProperty( "StartupScript", m_startupScript = "" );
00031 }
00032 
00033 //----------------------------------------------------------------------------------
00034 PythonScriptingSvc::~PythonScriptingSvc() { }
00035 //----------------------------------------------------------------------------------
00036 
00037 //----------------------------------------------------------------------------------
00038 StatusCode PythonScriptingSvc::initialize()
00039 //----------------------------------------------------------------------------------
00040 {
00041   // initialize the Service Base class
00042   StatusCode sc = Service::initialize();
00043   if ( sc.isFailure() ) return sc;
00044 
00045   MsgStream log( msgSvc(), name() );
00046 
00047   // Setup startup script. If none is explicitly specified, then
00048   // use the ApplicationMgr JobOptionsPath property as long as
00049   // the JobOptionsType property is set to "NONE".
00050   if( m_startupScript == "" ) {
00051     SmartIF<IProperty> prpMgr(serviceLocator());
00052     if ( prpMgr.isValid() )   {
00053       StringProperty tmp;
00054       tmp.assign(prpMgr->getProperty("JobOptionsType"));
00055       if ( tmp.value( ) == "NONE" ) {
00056         tmp.assign(prpMgr->getProperty("JobOptionsPath"));
00057         m_startupScript = tmp;
00058       }
00059     }
00060   }
00061 
00062   char* progName[] = { const_cast<char*>("GaudiPython") };
00063 
00064   // Initialize the Python interpreter.  Required.
00065   Py_Initialize();
00066   // Set argv for Tkinter (needs program name)
00067   PySys_SetArgv( 1, progName );
00068   // Get the Python version
00069   std::string fullversion = Py_GetVersion();
00070   std::string version( fullversion, 0, fullversion.find_first_of(' '));
00071   std::string vers(version, 0, version.find_first_of('.',version.find_first_of('.')+1));
00072   log << MSG::INFO << "Python version: [" << vers << "]" << endmsg;
00073 
00074 #if defined(linux)
00075   // This is hack to make global the python symbols
00076   // which are needed by the other python modules
00077   // (eg. readline, math, etc,) libraries.
00078   std::string libname = "libpython" + vers + ".so";
00079   dlopen(libname.c_str(), RTLD_GLOBAL | RTLD_LAZY);
00080 #endif
00081 
00082 
00083   // Startup commands
00084   PyRun_SimpleString( "from gaudimodule import *" );
00085   PyRun_SimpleString( "g      = AppMgr()" );
00086   // backward compatibility with SIPython
00087   PyRun_SimpleString( "theApp = g" );
00088   PyRun_SimpleString( "def Service(n): return g.service(n)" );
00089   PyRun_SimpleString( "def Algorithm(n): return g.algorithm(n)" );
00090   PyRun_SimpleString( "def Property(n): return g.service(n)" );
00091   // For command-line completion (unix only)
00092 #if !defined( _WIN32 )
00093   PyRun_SimpleString( "import rlcompleter");
00094   PyRun_SimpleString( "rlcompleter.readline.parse_and_bind('tab: complete')");
00095 #endif
00096   return StatusCode::SUCCESS;
00097 }
00098 
00099 //----------------------------------------------------------------------------------
00100 StatusCode PythonScriptingSvc::finalize()
00101 //----------------------------------------------------------------------------------
00102 {
00103   // Finalize this specific service
00104   StatusCode sc = Service::finalize();
00105   if ( sc.isFailure() ) {
00106       return sc;
00107   }
00108 
00109   // Shutdown the Python interpreter
00110   Py_Finalize();
00111   return StatusCode::SUCCESS;
00112 }
00113 
00114 //----------------------------------------------------------------------------------
00115 StatusCode PythonScriptingSvc::run()
00116 //----------------------------------------------------------------------------------
00117 {
00118   MsgStream log( msgSvc(), name() );
00119   if ( m_startupScript != "" ) {
00120     std::ifstream file(m_startupScript.c_str());
00121     std::stringstream stream;
00122     if( file ) {
00123       char ch;
00124       while( file.get(ch) ) stream.put(ch);
00125       PyRun_SimpleString( const_cast<char*>(stream.str().c_str()) );
00126       file.close();
00127     }
00128     else {
00129       log << MSG::WARNING << "Python startup file " << m_startupScript << " not found" << endmsg;
00130     }
00131   }
00132   PyRun_InteractiveLoop(stdin, "\0");
00133   return StatusCode::SUCCESS;
00134 }
00135 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated at Mon Sep 17 2012 13:49:36 for Gaudi Framework, version v23r4 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004