| # Gaudi Framework, version v22r4 |

Home |  | Generated: Fri Sep 2 2011

Public Member Functions | Protected Member Functions | Private Attributes

# PythonScriptingSvc Class Reference

This service handles scripting implemented using Python. More...

`#include <GaudiPython/PythonScriptingSvc.h>`

Inheritance diagram for PythonScriptingSvc:
[legend]
Collaboration diagram for PythonScriptingSvc:
[legend]

List of all members.

## Public Member Functions
| PythonScriptingSvc (const std::string &name, ISvcLocator *svc)
| Standard Constructor.
virtual StatusCode | initialize ()
| Initialize the service. [IService::initialize()].
virtual StatusCode | finalize ()
| Finalize the service. [IService::finalize()].
virtual StatusCode | run ()
| Run the service by taking full control. [IRunable::run()].
## Protected Member Functions
virtual | ~PythonScriptingSvc ()
| Destructor.
## Private Attributes
std::string | m_startupScript
| Startup script.

## Detailed Description

This service handles scripting implemented using Python.

Author:
  Pere Mato
  David Quarrie

Date:
  2001

Definition at line 16 of file PythonScriptingSvc.h.

## Constructor & Destructor Documentation

PythonScriptingSvc::PythonScriptingSvc | ( | const std::string & | name,
|  | ISvcLocator * | svc
| ) |  |

Standard Constructor.

Definition at line 27 of file PythonScriptingSvc.cpp.

    : base_class(name, svc) {
      // Declare the startup script Property
      declareProperty( "StartupScript", m_startupScript = "" );
    }

PythonScriptingSvc::~PythonScriptingSvc | ( | ) | `[protected, virtual]`

Destructor.

Definition at line 35 of file PythonScriptingSvc.cpp.

    { }

## Member Function Documentation

StatusCode PythonScriptingSvc::finalize | ( | void | ) | `[virtual]`

Finalize the service. [IService::finalize()].

Reimplemented from Service.

Definition at line 101 of file PythonScriptingSvc.cpp.

    {
      // Finalize this specific service
      StatusCode sc = Service::finalize();
      if ( sc.isFailure() ) {
          return sc;
      }

      // Shutdown the Python interpreter
      Py_Finalize();
      return StatusCode::SUCCESS;
    }

StatusCode PythonScriptingSvc::initialize | ( | void | ) | `[virtual]`

Initialize the service. [IService::initialize()].

Reimplemented from Service.

Definition at line 39 of file PythonScriptingSvc.cpp.

    {
      // initialize the Service Base class
      StatusCode sc = Service::initialize();
      if ( sc.isFailure() ) return sc;

      MsgStream log( msgSvc(), name() );

      // Setup startup script. If none is explicitly specified, then
      // use the ApplicationMgr JobOptionsPath property as long as
      // the JobOptionsType property is set to "NONE".
      if( m_startupScript == "" ) {
        SmartIF<IProperty> prpMgr(serviceLocator());
        if ( prpMgr.isValid() )   {
          StringProperty tmp;
          tmp.assign(prpMgr->getProperty("JobOptionsType"));
          if ( tmp.value( ) == "NONE" ) {
            tmp.assign(prpMgr->getProperty("JobOptionsPath"));
            m_startupScript = tmp;
          }
        }
      }

      char* progName[] = { const_cast<char*>("GaudiPython") };

      // Initialize the Python interpreter.  Required.
      Py_Initialize();
      // Set argv for Tkinter (needs program name)
      PySys_SetArgv( 1, progName );
      // Get the Python version
      std::string fullversion = Py_GetVersion();
      std::string version( fullversion, 0, fullversion.find_first_of(' '));
      std::string vers(version, 0, version.find_first_of('.',version.find_first_of('.')+1));
      log << MSG::INFO << "Python version: [" << vers << "]" << endmsg;

    #if defined(linux)
      // This is hack to make global the python symbols
      // which are needed by the other python modules
      // (eg. readline, math, etc,) libraries.
      std::string libname = "libpython" + vers + ".so";
      dlopen(libname.c_str(), RTLD_GLOBAL | RTLD_LAZY);
    #endif

      // Startup commands
      PyRun_SimpleString( "from gaudimodule import *" );
      PyRun_SimpleString( "g      = AppMgr()" );
      // backward compatibility with SIPython
      PyRun_SimpleString( "theApp = g" );
      PyRun_SimpleString( "def Service(n): return g.service(n)" );
      PyRun_SimpleString( "def Algorithm(n): return g.algorithm(n)" );
      PyRun_SimpleString( "def Property(n): return g.service(n)" );
      // For command-line completion (unix only)
    #if !defined( _WIN32 )
      PyRun_SimpleString( "import rlcompleter");
      PyRun_SimpleString( "rlcompleter.readline.parse_and_bind('tab: complete')");
    #endif
      return StatusCode::SUCCESS;
    }

StatusCode PythonScriptingSvc::run | ( | ) | `[virtual]`

Run the service by taking full control. [IRunable::run()].

Implements IRunable.

Definition at line 116 of file PythonScriptingSvc.cpp.

    {
      MsgStream log( msgSvc(), name() );
      if ( m_startupScript != "" ) {
        std::ifstream file(m_startupScript.c_str());
        std::stringstream stream;
        if( file ) {
          char ch;
          while( file.get(ch) ) stream.put(ch);
          PyRun_SimpleString( const_cast<char*>(stream.str().c_str()) );
          file.close();
        }
        else {
          log << MSG::WARNING << "Python startup file " << m_startupScript << " not found" << endmsg;
        }
      }
      PyRun_InteractiveLoop(stdin, "\0");
      return StatusCode::SUCCESS;
    }

## Member Data Documentation

std::string PythonScriptingSvc::m_startupScript`[private]`

Startup script.

Definition at line 36 of file PythonScriptingSvc.h.

The documentation for this class was generated from the following files:

- /afs/cern.ch/sw/Gaudi/releases/GAUDI/GAUDI_v22r4/GaudiPython/src/Services/PythonScriptingSvc.h
- /afs/cern.ch/sw/Gaudi/releases/GAUDI/GAUDI_v22r4/GaudiPython/src/Services/PythonScriptingSvc.cpp

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines