[Hdrflow-svn] SF.net SVN: hdrflow: [184] trunk
Status: Pre-Alpha
Brought to you by:
glslang
|
From: <gl...@us...> - 2007-07-14 23:36:27
|
Revision: 184
http://hdrflow.svn.sourceforge.net/hdrflow/?rev=184&view=rev
Author: glslang
Date: 2007-07-14 16:36:02 -0700 (Sat, 14 Jul 2007)
Log Message:
-----------
+Python interpreter initial implementation
Modified Paths:
--------------
trunk/app/HDRFlow/English.lproj/ScriptEditor.nib/info.nib
trunk/app/HDRFlow/English.lproj/ScriptEditor.nib/keyedobjects.nib
trunk/app/HDRFlow/ScriptEditorController.h
trunk/app/HDRFlow/ScriptEditorController.mm
trunk/app/HDRFlow/ScriptEngine.cpp
trunk/app/HDRFlow/ScriptEngine.hpp
trunk/lib/openlibraries/src/umbrella_framework/openlibraries.hpp
Modified: trunk/app/HDRFlow/English.lproj/ScriptEditor.nib/info.nib
===================================================================
--- trunk/app/HDRFlow/English.lproj/ScriptEditor.nib/info.nib 2007-07-12 22:11:47 UTC (rev 183)
+++ trunk/app/HDRFlow/English.lproj/ScriptEditor.nib/info.nib 2007-07-14 23:36:02 UTC (rev 184)
@@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>IBDocumentLocation</key>
- <string>69 49 356 240 0 0 1280 778 </string>
+ <string>81 58 356 240 0 0 1440 878 </string>
<key>IBFramework Version</key>
<string>446.1</string>
<key>IBOpenObjects</key>
Modified: trunk/app/HDRFlow/English.lproj/ScriptEditor.nib/keyedobjects.nib
===================================================================
(Binary files differ)
Modified: trunk/app/HDRFlow/ScriptEditorController.h
===================================================================
--- trunk/app/HDRFlow/ScriptEditorController.h 2007-07-12 22:11:47 UTC (rev 183)
+++ trunk/app/HDRFlow/ScriptEditorController.h 2007-07-14 23:36:02 UTC (rev 184)
@@ -7,13 +7,21 @@
#import <Cocoa/Cocoa.h>
+// forward declaration
+namespace hdrflow { class ScriptEngine; }
+
@interface ScriptEditorController : NSWindowController
{
IBOutlet NSTextView* scriptEdit_;
IBOutlet NSTextView* scriptOutput_;
IBOutlet NSTextField* scriptStatus_;
+
+ hdrflow::ScriptEngine* engine_;
}
+- ( void ) executeScript;
+- ( void ) clearScript;
+
- ( IBAction ) executeScript: ( id ) sender;
- ( IBAction ) clearScript: ( id ) sender;
Modified: trunk/app/HDRFlow/ScriptEditorController.mm
===================================================================
--- trunk/app/HDRFlow/ScriptEditorController.mm 2007-07-12 22:11:47 UTC (rev 183)
+++ trunk/app/HDRFlow/ScriptEditorController.mm 2007-07-14 23:36:02 UTC (rev 184)
@@ -7,16 +7,64 @@
#import "ScriptEditorController.h"
+#import "ScriptEngine.hpp"
+
@implementation ScriptEditorController
+#pragma mark ---- IB actions ----
+
- ( IBAction ) executeScript: ( id ) sender
{
+ [ self executeScript ];
}
- ( IBAction ) clearScript: ( id ) sender
{
- [ scriptEdit_ setString: @""];
- [ scriptEdit_ setNeedsDisplay: YES];
+ [ self clearScript ];
}
+- ( id ) initWithWindowNibName: ( NSString* ) windowNibName
+{
+ engine_ = new hdrflow::ScriptEngine( );
+ return [ super initWithWindowNibName: windowNibName ];
+}
+
+- ( void ) dealloc
+{
+ delete engine_;
+ [ super dealloc ];
+}
+
+- ( void ) executeScript
+{
+ NSString* script = [ scriptEdit_ string ];
+ NSString* output = [ scriptOutput_ string ];
+
+ if( engine_->eval( [ script UTF8String ] ) )
+ {
+ output = [ output stringByAppendingString: script ];
+ output = [ output stringByAppendingString: @"\n" ];
+ [ scriptStatus_ setStringValue: @"Script successful." ];
+ }
+ else
+ {
+ output = [ output stringByAppendingString: @"Error in script:\n\t" ];
+ [ scriptStatus_ setStringValue: @"Script failed." ];
+ }
+
+ NSString* out = [ [ [ NSString alloc ] initWithUTF8String: engine_->result( ).c_str( ) ] autorelease ];
+ output = [ output stringByAppendingString: out ];
+
+ [ scriptOutput_ setString: output ];
+ [ scriptOutput_ setNeedsDisplay: YES ];
+
+ [ self clearScript ];
+}
+
+- ( void ) clearScript
+{
+ [ scriptEdit_ setString: @"" ];
+ [ scriptEdit_ setNeedsDisplay: YES ];
+}
+
@end
Modified: trunk/app/HDRFlow/ScriptEngine.cpp
===================================================================
--- trunk/app/HDRFlow/ScriptEngine.cpp 2007-07-12 22:11:47 UTC (rev 183)
+++ trunk/app/HDRFlow/ScriptEngine.cpp 2007-07-14 23:36:02 UTC (rev 184)
@@ -8,14 +8,27 @@
#include "ScriptEngine.hpp"
namespace py = boost::python;
+namespace pl = olib::openpluginlib;
namespace hdrflow {
+namespace
+{
+ pl::string error_to_result( PyObject* obj )
+ {
+ if( obj )
+ {
+ py::extract<std::string> extract( obj );
+ if( extract.check( ) ) return extract( );
+ }
+
+ return pl::string( );
+ }
+}
+
ScriptEngine::ScriptEngine( )
{
Py_Initialize( );
-
- main_module_ = py::object( py::handle<>( py::borrowed( PyImport_AddModule( "__main__" ) ) ) );
}
ScriptEngine::~ScriptEngine( )
@@ -23,9 +36,49 @@
Py_Finalize( );
}
-bool ScriptEngine::eval( )
+bool ScriptEngine::eval( const pl::string& str )
{
- return false;
+ py::object main_module( py::handle<>( py::borrowed( PyImport_AddModule( "__main__" ) ) ) );
+ py::object main_namespace = main_module.attr( "__dict__" );
+
+ result_.clear( );
+
+ try
+ {
+ py::handle<> handle;
+ if( !( handle = py::handle<>(
+ py::allow_null( PyRun_String( str.c_str( ), Py_eval_input, main_namespace.ptr( ), main_namespace.ptr( ) ) ) ) ) )
+ {
+ PyErr_Clear( );
+ handle = py::handle<>( PyRun_String( str.c_str( ), Py_file_input, main_namespace.ptr( ), main_namespace.ptr( ) ) );
+ }
+
+ py::object strobj( py::handle<>( PyObject_Str( py::object( handle ).ptr( ) ) ) );
+ if( strobj )
+ result_ = PyString_AsString( strobj.ptr( ) ) + pl::string( "\n" );
+ }
+ catch( py::error_already_set )
+ {
+ PyObject* error_type;
+ PyObject* error_data;
+ PyObject* error_traceback;
+
+ PyErr_Fetch( &error_type, &error_data, &error_traceback );
+
+ result_ += error_to_result( error_type );
+ result_ += error_to_result( error_data );
+ result_ += error_to_result( error_traceback );
+ result_ += "\n";
+
+ return false;
+ }
+
+ return true;
}
+pl::string ScriptEngine::result( ) const
+{
+ return result_;
}
+
+}
Modified: trunk/app/HDRFlow/ScriptEngine.hpp
===================================================================
--- trunk/app/HDRFlow/ScriptEngine.hpp 2007-07-12 22:11:47 UTC (rev 183)
+++ trunk/app/HDRFlow/ScriptEngine.hpp 2007-07-14 23:36:02 UTC (rev 184)
@@ -18,10 +18,11 @@
explicit ScriptEngine( );
~ScriptEngine( );
- bool eval( );
+ bool eval( const olib::openpluginlib::string& str );
+ olib::openpluginlib::string result( ) const;
private:
- boost::python::object main_module_;
+ olib::openpluginlib::string result_;
};
}
Modified: trunk/lib/openlibraries/src/umbrella_framework/openlibraries.hpp
===================================================================
--- trunk/lib/openlibraries/src/umbrella_framework/openlibraries.hpp 2007-07-12 22:11:47 UTC (rev 183)
+++ trunk/lib/openlibraries/src/umbrella_framework/openlibraries.hpp 2007-07-14 23:36:02 UTC (rev 184)
@@ -8,7 +8,9 @@
#ifndef OPENLIBRARIES_INC_
#define OPENLIBRARIES_INC_
+#ifndef __OBJC__
#include <boost/python.hpp>
+#endif
#include <openpluginlib/pl/openpluginlib.hpp>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|