Commit 253dbb8c authored by Andre Reichenbach's avatar Andre Reichenbach
Browse files

[ADD] added first version of a python script interpreter and a very simple nogui

parent 6afb0515
......@@ -148,6 +148,12 @@ IF( OW_GUI_QT4 )
ADD_SUBDIRECTORY( qt4gui )
ENDIF()
OPTION( OW_GUI_SCRIPT "Enable this to build the script-based OpenWalnut NOGUI." ON )
IF( OW_GUI_SCRIPT )
# build
ADD_SUBDIRECTORY( scriptGui )
ENDIF()
# -----------------------------------------------------------------------------------------------------------------------------------------------
# Modules
......
......@@ -39,12 +39,17 @@ ADD_DEFINITIONS( '-DW_LIB_SUFFIX="${CMAKE_SHARED_LIBRARY_SUFFIX}"' )
# Add sources as target
# ---------------------------------------------------------------------------------------------------------------------------------------------------
FIND_PACKAGE( PythonLibs REQUIRED )
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS} )
# Collect the compile-files for this target
COLLECT_COMPILE_FILES( "${CMAKE_CURRENT_SOURCE_DIR}" TARGET_CPP_FILES TARGET_H_FILES TARGET_TEST_FILES )
# Setup the target
ADD_LIBRARY( ${LibName} SHARED ${TARGET_CPP_FILES} ${TARGET_H_FILES} ${OW_VERSION_HEADER} )
TARGET_LINK_LIBRARIES( ${LibName} ${Boost_LIBRARIES} ${CMAKE_DL_LIBS} ${OPENGL_gl_LIBRARY} ${OPENSCENEGRAPH_LIBRARIES}
${PYTHON_LIBRARIES}
)
# Tell CMake that someone creates this file for us. See doc of SETUP_VERSION_HEADER for details why this is needed.
......@@ -144,4 +149,3 @@ SETUP_SHADERS( "${TARGET_GLSL_FILES}" "${OW_SHARE_DIR_RELATIVE}/shaders" "CORE"
SETUP_STYLECHECKER( "${LibName}"
"${TARGET_CPP_FILES};${TARGET_H_FILES};${TARGET_TEST_FILES};${TARGET_GLSL_FILES}" # add all these files to the stylechecker
"" ) # exlude some ugly files
......@@ -146,10 +146,16 @@ void WKernel::threadMain()
WLogger::getLogger()->addLogMessage( "Starting Kernel", "Kernel", LL_INFO );
// wait for GUI to be initialized properly
m_gui->isInitialized().wait();
if( m_gui )
{
m_gui->isInitialized().wait();
}
// start GE
m_graphicsEngine->run();
if( m_graphicsEngine )
{
m_graphicsEngine->run();
}
// actually there is nothing more to do here
waitForStop();
......
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include "WScriptInterpreter.h"
WScriptInterpreter::~WScriptInterpreter()
{
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WSCRIPTINTERPRETER_H
#define WSCRIPTINTERPRETER_H
#include <string>
/**
* \class WScriptInterpreter
*
* An abstract base class for a script interpreter.
*/
class WScriptInterpreter
{
public:
/**
* Destructor.
*/
virtual ~WScriptInterpreter();
/**
* Initialize OpenWalnut-bindings. These allow OW-classes to be used via the script interpreter.
*/
virtual void initBindings() = 0;
/**
* Execute some code.
*
* \param line The code to interpret.
*/
virtual void execute( std::string const& line ) = 0;
};
#endif // WSCRIPTINTERPRETER_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <string>
#include "python/WScriptInterpreterPython.h"
#include "WScriptInterpreterFactory.h"
boost::shared_ptr< WScriptInterpreter > WScriptInterpreterFactory::constructByFileExtension( std::string const& ext )
{
boost::shared_ptr< WScriptInterpreter > interp;
if( ext == ".py" )
{
interp = boost::shared_ptr< WScriptInterpreter >( new WScriptInterpreterPython() );
}
return interp;
}
boost::shared_ptr< WScriptInterpreter > WScriptInterpreterFactory::constructByName( std::string const& name )
{
boost::shared_ptr< WScriptInterpreter > interp;
if( name == "python" )
{
interp = boost::shared_ptr< WScriptInterpreter >( new WScriptInterpreterPython() );
}
return interp;
}
std::string WScriptInterpreterFactory::getSupportedInterpreterList()
{
return "\tpython (.py)";
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WSCRIPTINTERPRETERFACTORY_H
#define WSCRIPTINTERPRETERFACTORY_H
#include <string>
#include <boost/shared_ptr.hpp>
#include "WScriptInterpreter.h"
/**
* A factory class that creates instances of WScriptInterpreter subclasses.
*/
class WScriptInterpreterFactory
{
public:
/**
* Create a script interpreter that can execute scripts from file with the given extension (for example
* .py files get executed by a python interpreter).
*
* \param ext The extension of the file.
* \return A matching script interpreter instance or NULL if no such instance could be created.
*/
static boost::shared_ptr< WScriptInterpreter > constructByFileExtension( std::string const& ext );
/**
* Create a script interpreter via language name (for example "python").
*
* \param ext The filename.
* \return A matching script interpreter instance or NULL if no such instance could be created.
*/
static boost::shared_ptr< WScriptInterpreter > constructByName( std::string const& name );
/**
* Returns a formatted string listing the interpreter types that can be instantiated.
*
* \return A string listing possible interpreters.
*/
static std::string getSupportedInterpreterList();
};
#endif // WSCRIPTINTERPRETERFACTORY_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <string>
#include "../../kernel/WKernel.h"
#include "../wrappers/WModuleWrapper.h"
#include "../wrappers/WPropertyGroupWrapper.h"
#include "../wrappers/WPropertyWrapper.h"
#include "WScriptInterpreterPython.h"
WScriptInterpreterPython::WScriptInterpreterPython()
{
try
{
Py_Initialize();
m_pyModule = pb::import( "__main__" );
m_pyMainNamespace = m_pyModule.attr( "__dict__" );
}
catch( pb::error_already_set const& )
{
PyErr_Print();
}
}
WScriptInterpreterPython::~WScriptInterpreterPython()
{
Py_Finalize();
}
void WScriptInterpreterPython::initBindings()
{
// bind WPropertyWrapper class to "WProperty" in the python namespace
// no constructor in python for now
m_pyMainNamespace[ "WProperty" ] = pb::class_< WPropertyWrapper >( "WProperty", pb::no_init )
.def( "getBool", &WPropertyWrapper::getBool )
.def( "getInt", &WPropertyWrapper::getInt )
.def( "getString", &WPropertyWrapper::getString )
.def( "getDouble", &WPropertyWrapper::getDouble )
.def( "setBool", &WPropertyWrapper::setBool )
.def( "setInt", &WPropertyWrapper::setInt )
.def( "setString", &WPropertyWrapper::setString )
.def( "setDouble", &WPropertyWrapper::setDouble )
.def( "getName", &WPropertyWrapper::getName )
.def( "getDescription", &WPropertyWrapper::getDescription );
m_pyMainNamespace[ "WPropertyGroup" ] = pb::class_< WPropertyGroupWrapper >( "WPropertyGroup", pb::no_init )
.def( "getProperty", &WPropertyGroupWrapper::getProperty );
m_pyMainNamespace[ "WModuleContainer" ] = pb::class_< WModuleContainerWrapper >( "WModuleContainer", pb::no_init )
.def( "create", &WModuleContainerWrapper::create )
.def( "remove", &WModuleContainerWrapper::remove )
.def( "createDataModule", &WModuleContainerWrapper::createDataModule );
m_pyMainNamespace[ "WModule" ] = pb::class_< WModuleWrapper >( "WModule", pb::no_init )
.def( "getName", &WModuleWrapper::getName )
.def( "getDescription", &WModuleWrapper::getDescription )
.def( "connect", &WModuleWrapper::connect )
.def( "getProperties", &WModuleWrapper::getProperties );
// bind the kernel's root container to the "rootContainer" variable in the python namespace
// this allows access to the modules via this variable
m_rootContainer = WModuleContainerWrapper( WKernel::getRunningKernel()->getRootContainer() );
m_pyMainNamespace[ "rootContainer" ] = &m_rootContainer;
}
void WScriptInterpreterPython::execute( std::string const& line )
{
try
{
pb::exec( line.c_str(), m_pyMainNamespace );
}
catch( pb::error_already_set const& )
{
PyErr_Print();
}
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WSCRIPTINTERPRETERPYTHON_H
#define WSCRIPTINTERPRETERPYTHON_H
#include <string>
#include <boost/python.hpp>
#include "../wrappers/WModuleContainerWrapper.h"
#include "../WScriptInterpreter.h"
namespace pb = boost::python;
/**
* \class WScriptInterpreterPython
*
* An interpreter for python scripts.
*/
class WScriptInterpreterPython : public WScriptInterpreter
{
public:
/**
* Constructor. Creates the interpreter.
*/
WScriptInterpreterPython();
/**
* Destructor. Destroys the interpreter.
*/
virtual ~WScriptInterpreterPython();
/**
* Initializes bindings for OpenWalnut classes. Call this after starting the kernel.
*/
virtual void initBindings();
/**
* Execute some python code.
*
* \param line The code to execute.
*/
virtual void execute( std::string const& line );
private:
//! The python module.
pb::object m_pyModule;
//! The namespace where we will be working. Bindings are declared in this workspace.
pb::object m_pyMainNamespace;
//! A pointer to the kernel's root container. We can use this to manipulate modules.
WModuleContainerWrapper m_rootContainer;
};
#endif // WSCRIPTINTERPRETERPYTHON_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <string>
#include "../../kernel/WModuleFactory.h"
#include "WModuleContainerWrapper.h"
WModuleContainerWrapper::WModuleContainerWrapper()
: m_mc()
{
}
WModuleContainerWrapper::WModuleContainerWrapper( boost::shared_ptr< WModuleContainer > mc )
: m_mc( mc )
{
}
WModuleContainerWrapper::~WModuleContainerWrapper()
{
}
WModuleWrapper WModuleContainerWrapper::create( std::string const& name )
{
WModuleWrapper mw( m_mc->createAndAdd( name ) );
return mw;
}
WModuleWrapper WModuleContainerWrapper::createDataModule( std::string const& filename )
{
boost::shared_ptr< WDataModule > dataModule = boost::shared_static_cast< WDataModule >(
WModuleFactory::getModuleFactory()->create(
WModuleFactory::getModuleFactory()->getPrototypeByName( "Data Module" ) ) );
dataModule->setSuppressColormaps( true );
dataModule->setFilename( filename );
m_mc->add( dataModule );
dataModule->isReadyOrCrashed().wait();
return WModuleWrapper( dataModule );
}
void WModuleContainerWrapper::remove( WModuleWrapper module )
{
module.getModulePtr()->wait( true );
m_mc->remove( module.getModulePtr() );
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WMODULECONTAINERWRAPPER_H
#define WMODULECONTAINERWRAPPER_H
#include <string>
#include <boost/shared_ptr.hpp>
#include "../../kernel/WModuleContainer.h"
#include "WModuleWrapper.h"
/**
* \class WModuleContainerWrapper
*
* Encapsulates a module container. This class is exposed to scripts.
*/
class WModuleContainerWrapper
{
public:
/**
* Constructor. Creates an empty wrapper.
*/
WModuleContainerWrapper();
/**
* Constructor.
*
* \param mc The module container.
*/
explicit WModuleContainerWrapper( boost::shared_ptr< WModuleContainer > mc );
/**
* Destructor.
*/
~WModuleContainerWrapper();
/**
* Creates a module from the prototype with the given name.
*
* \param name The name of the module prototype.
* \return The module.
*/
WModuleWrapper create( std::string const& name );
/**
* Creates a data module and load the file given via filename.
*
* \param filename The name of the file to load.
*/
WModuleWrapper createDataModule( std::string const& filename );
/**
* Remove a module from the container.
*
* \param The module to remove.
*/
void remove( WModuleWrapper module );
private:
//! The module container.
boost::shared_ptr< WModuleContainer > m_mc;
};
#endif // WMODULECONTAINERWRAPPER_H