← Back to team overview

kicad-developers team mailing list archive

[patch 1/1] kicad-wxpython-support.patch

 

7th patch: wxPython bind add

This patch adds wxPython binding, and some functions, it is now possible 
to use any wxWidget component
from python. (You will need wxPython installed on your system, please 
see www.wxPython.org )

next patch will be kicad project editor binding and feature add

---
common/pyhandler.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++++++-
include/pyhandler.h | 5 ++++-
2 files changed, 52 insertions(+), 2 deletions(-)

 --------------030907040306010407090308 Content-Type: text/x-patch;
name="kicad-wxpython-support.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="kicad-wxpython-support.patch"

7th patch: wxPython bind add

This patch adds wxPython binding, and some functions, it is now possible to use any wxWidget component
from python. (You will need wxPython installed on your system, please see www.wxPython.org )

next patch will be kicad project editor binding and feature add

---
common/pyhandler.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++++++-
include/pyhandler.h | 5 ++++-
2 files changed, 52 insertions(+), 2 deletions(-)


Index: kicad-dev/common/pyhandler.cpp
===================================================================
--- kicad-dev.orig/common/pyhandler.cpp	2007-04-25 23:25:07.000000000 +0200
+++ kicad-dev/common/pyhandler.cpp	2007-04-26 23:20:32.000000000 +0200
@@ -1,4 +1,5 @@
#include "wx/wxprec.h"
+#include <id.h>

#ifdef __BORLANDC__
#pragma hdrstop
@@ -26,6 +27,8 @@
/* Common Python Binding */
/*****************************************************************************/

+static int GetLastID( void ) { return ID_END_LIST; }
+
static object ChooseFile( str objTitle, str objMask, object objOpen )
{
wxString mask = PyHandler::MakeStr( objMask );
@@ -56,6 +59,7 @@
def ( "ChooseFile", &ChooseFile );
def ( "RegisterCallback", &RegisterCb );
def ( "UnRegisterCallback", &UnRegisterCb );
+ def ( "GetLastID", &GetLastID );

def ( "Print", &Print );
}
@@ -83,8 +87,24 @@
/* Init the Python env */
{
Py_Initialize();
+ PyEval_InitThreads();
m_ModulesLoaded = false;
m_current = 0;
+ if ( !wxPyCoreAPI_IMPORT() )
+ {
+ std::cerr << "Can't get wx Python binding\n" ;
+ PyErr_Print();
+ }
+// m_mainTState = wxPyBeginAllowThreads(); // I can't figure out why this make py crash ...
+ m_mainTState = NULL;
+
+ // Make the console appear in a window:
+ wxString initConsole;
+ initConsole += wxT( "import sys\n" );
+ initConsole += wxT( "import wx\n" );
+ initConsole += wxT( "output = wx.PyOnDemandOutputWindow()\n" );
+ initConsole += wxT( "sys.stdout = sys.stderr = output\n" );
+ RunSimpleString( initConsole );

AddToModule ( wxT( "common" ), &init_base_utils );
}
@@ -139,6 +159,7 @@
PyHandler::~PyHandler()
/* Closes the Python env */
{
+ wxPyEndAllowThreads(m_mainTState);
Py_Finalize();
}

@@ -197,6 +218,8 @@

FILE * file = fopen( name.fn_str(), "r" );

+ wxPyBlock_t blocked = wxPyBeginBlockThreads();
+
if ( !file )
{
// do something
@@ -206,19 +229,41 @@

try
{
- handle<> ignored( ( PyRun_File( file, name.fn_str(), Py_file_input, ns.ptr(), ns.ptr() ) ) );
+ handle<> ignored( PyRun_File( file, name.fn_str(), Py_file_input, ns.ptr(), ns.ptr() ) );
}
catch ( error_already_set )
{
PyErr_Print(); // should be printed into an error message ...
fclose( file );
+ wxPyEndBlockThreads(blocked);
return false;
}

+ wxPyEndBlockThreads(blocked);
fclose( file );
return true;
}

+bool PyHandler::RunSimpleString( const wxString & code )
+/* Run the code in 'code' */
+{
+ wxPyBlock_t blocked = wxPyBeginBlockThreads();
+	try
+	{
+ PyRun_SimpleString( code.fn_str() );
+	}
+	catch ( error_already_set )
+	{
+	PyErr_Print(); // should be printed into an error message ...
+ wxPyEndBlockThreads(blocked);
+	return false;
+	}
+
+ wxPyEndBlockThreads(blocked);
+	return true;
+}
+
+
void PyHandler::SetAppName( const wxString & name )
/* Set the application name in the python scope */
{
@@ -256,6 +301,7 @@
int i = GetEventIndex( key );
if ( -1 == i ) return;

+ wxPyBlock_t blocked = wxPyBeginBlockThreads();
for ( unsigned int j = 0; j < m_EventRegistry[i].functors.size(); j++ )
{
try
@@ -268,6 +314,7 @@
PyErr_Print();
}
}
+ wxPyEndBlockThreads(blocked);
}

void PyHandler::RegisterCallback( const wxString & key, const object & callback )
Index: kicad-dev/include/pyhandler.h
===================================================================
--- kicad-dev.orig/include/pyhandler.h	2007-04-25 23:23:51.000000000 +0200
+++ kicad-dev/include/pyhandler.h	2007-04-26 23:10:30.000000000 +0200
@@ -9,6 +9,7 @@
#include <Python.h>

#include <wx/string.h>
+#include <wx/wxPython/wxPython.h>
#include <vector>

class PyHandler
@@ -20,6 +21,7 @@
static PyHandler * m_instance;
bool m_ModulesLoaded;
int m_current;
+ PyThreadState* m_mainTState;

protected:
PyHandler();
@@ -60,9 +62,10 @@
void AddToModule( const wxString & name, initfunc_t initfunc );
int GetModuleIndex( const wxString & name ) const;

-	// Script
+	// Script and direct call
void RunScripts();
bool RunScript( const wxString & name );
+ bool RunSimpleString( const wxString & code );

// Common Informations
const char * GetVersion();
 --------------030907040306010407090308--