2012-03-17 17:30:03 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 NBEE Embedded Systems, Miguel Angel Ajo <miguelangel@nbee.es>
|
2015-02-15 00:23:54 +00:00
|
|
|
* Copyright (C) 1992-2015 KiCad Developers, see AUTHORS.txt for contributors.
|
2012-03-17 17:30:03 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file python_scripting.cpp
|
|
|
|
* @brief methods to add scripting capabilities inside pcbnew
|
|
|
|
*/
|
2012-03-11 19:07:10 +00:00
|
|
|
|
|
|
|
#include <python_scripting.h>
|
2012-06-27 21:19:19 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-08-02 17:24:53 +00:00
|
|
|
|
|
|
|
#include <fctsys.h>
|
|
|
|
#include <wxstruct.h>
|
|
|
|
#include <common.h>
|
|
|
|
#include <colors.h>
|
2013-03-17 01:14:46 +00:00
|
|
|
#include <macros.h>
|
2012-03-11 19:07:10 +00:00
|
|
|
|
|
|
|
/* init functions defined by swig */
|
|
|
|
|
2012-06-27 21:19:19 +00:00
|
|
|
extern "C" void init_kicad( void );
|
2012-03-11 19:07:10 +00:00
|
|
|
|
2012-06-27 21:19:19 +00:00
|
|
|
extern "C" void init_pcbnew( void );
|
2012-03-11 19:07:10 +00:00
|
|
|
|
2013-03-11 08:09:48 +00:00
|
|
|
#define EXTRA_PYTHON_MODULES 10 // this is the number of python
|
|
|
|
// modules that we want to add into the list
|
2012-06-27 21:19:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* python inittab that links module names to module init functions
|
2012-03-11 19:07:10 +00:00
|
|
|
* we will rebuild it to include the original python modules plus
|
2012-06-27 21:19:19 +00:00
|
|
|
* our own ones
|
2012-03-11 19:07:10 +00:00
|
|
|
*/
|
|
|
|
|
2013-03-15 16:35:24 +00:00
|
|
|
struct _inittab* SwigImportInittab;
|
|
|
|
static int SwigNumModules = 0;
|
2012-03-11 19:07:10 +00:00
|
|
|
|
2015-07-09 16:44:23 +00:00
|
|
|
static bool wxPythonLoaded = false; // true if the wxPython scripting layer was successfully loaded
|
|
|
|
|
|
|
|
bool IsWxPythonLoaded()
|
|
|
|
{
|
|
|
|
return wxPythonLoaded;
|
|
|
|
}
|
|
|
|
|
2012-03-11 19:07:10 +00:00
|
|
|
|
|
|
|
/* Add a name + initfuction to our SwigImportInittab */
|
|
|
|
|
2012-06-27 21:19:19 +00:00
|
|
|
static void swigAddModule( const char* name, void (* initfunc)() )
|
2012-03-17 15:17:13 +00:00
|
|
|
{
|
2013-03-17 01:14:46 +00:00
|
|
|
SwigImportInittab[SwigNumModules].name = (char*) name;
|
|
|
|
SwigImportInittab[SwigNumModules].initfunc = initfunc;
|
2012-06-27 21:19:19 +00:00
|
|
|
SwigNumModules++;
|
2013-03-17 01:14:46 +00:00
|
|
|
SwigImportInittab[SwigNumModules].name = (char*) 0;
|
|
|
|
SwigImportInittab[SwigNumModules].initfunc = 0;
|
2012-03-11 19:07:10 +00:00
|
|
|
}
|
|
|
|
|
2012-06-27 21:19:19 +00:00
|
|
|
|
2015-02-15 00:23:54 +00:00
|
|
|
/* Add the builtin python modules */
|
2012-03-11 19:07:10 +00:00
|
|
|
|
2012-06-27 21:19:19 +00:00
|
|
|
static void swigAddBuiltin()
|
2012-03-17 15:17:13 +00:00
|
|
|
{
|
2012-06-27 21:19:19 +00:00
|
|
|
int i = 0;
|
2012-03-11 19:07:10 +00:00
|
|
|
|
2012-07-15 20:01:05 +00:00
|
|
|
/* discover the length of the pyimport inittab */
|
2012-11-14 07:15:59 +00:00
|
|
|
while( PyImport_Inittab[i].name )
|
|
|
|
i++;
|
2012-07-15 20:01:05 +00:00
|
|
|
|
|
|
|
/* allocate memory for the python module table */
|
2013-03-15 16:35:24 +00:00
|
|
|
SwigImportInittab = (struct _inittab*) malloc(
|
|
|
|
sizeof(struct _inittab) * (i + EXTRA_PYTHON_MODULES) );
|
2012-11-14 07:15:59 +00:00
|
|
|
|
2012-07-15 20:01:05 +00:00
|
|
|
/* copy all pre-existing python modules into our newly created table */
|
2013-03-15 16:35:24 +00:00
|
|
|
i = 0;
|
|
|
|
|
2012-06-27 21:19:19 +00:00
|
|
|
while( PyImport_Inittab[i].name )
|
|
|
|
{
|
|
|
|
swigAddModule( PyImport_Inittab[i].name, PyImport_Inittab[i].initfunc );
|
|
|
|
i++;
|
|
|
|
}
|
2012-03-11 19:07:10 +00:00
|
|
|
}
|
2012-06-27 21:19:19 +00:00
|
|
|
|
2012-11-14 07:15:59 +00:00
|
|
|
|
|
|
|
/* Function swigAddModules
|
2012-06-27 21:19:19 +00:00
|
|
|
* adds the internal modules we offer to the python scripting, so they will be
|
|
|
|
* available to the scripts we run.
|
2012-11-14 07:15:59 +00:00
|
|
|
*
|
2012-06-27 21:19:19 +00:00
|
|
|
*/
|
|
|
|
|
2012-03-11 19:07:10 +00:00
|
|
|
static void swigAddModules()
|
|
|
|
{
|
2012-06-27 21:19:19 +00:00
|
|
|
swigAddModule( "_pcbnew", init_pcbnew );
|
|
|
|
|
|
|
|
// finally it seems better to include all in just one module
|
|
|
|
// but in case we needed to include any other modules,
|
|
|
|
// it must be done like this:
|
2013-03-15 16:35:24 +00:00
|
|
|
// swigAddModule( "_kicad", init_kicad );
|
2012-03-11 19:07:10 +00:00
|
|
|
}
|
|
|
|
|
2013-03-15 16:35:24 +00:00
|
|
|
|
2012-06-27 21:19:19 +00:00
|
|
|
/* Function swigSwitchPythonBuiltin
|
|
|
|
* switches python module table to our built one .
|
2012-11-14 07:15:59 +00:00
|
|
|
*
|
2012-06-27 21:19:19 +00:00
|
|
|
*/
|
|
|
|
|
2012-03-11 19:07:10 +00:00
|
|
|
static void swigSwitchPythonBuiltin()
|
|
|
|
{
|
2012-06-27 21:19:19 +00:00
|
|
|
PyImport_Inittab = SwigImportInittab;
|
2012-03-11 19:07:10 +00:00
|
|
|
}
|
|
|
|
|
2013-03-15 16:35:24 +00:00
|
|
|
|
2012-06-27 21:19:19 +00:00
|
|
|
/* Function pcbnewInitPythonScripting
|
2012-11-14 07:15:59 +00:00
|
|
|
* Initializes all the python environment and publish our interface inside it
|
2012-08-01 11:54:20 +00:00
|
|
|
* initializes all the wxpython interface, and returns the python thread control structure
|
2012-11-14 07:15:59 +00:00
|
|
|
*
|
2012-06-27 21:19:19 +00:00
|
|
|
*/
|
2012-08-02 07:47:30 +00:00
|
|
|
|
2013-03-11 08:09:48 +00:00
|
|
|
PyThreadState* g_PythonMainTState;
|
2012-08-02 07:47:30 +00:00
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
bool pcbnewInitPythonScripting( const char * aUserPluginsPath )
|
2012-03-11 19:07:10 +00:00
|
|
|
{
|
2012-06-27 21:19:19 +00:00
|
|
|
swigAddBuiltin(); // add builtin functions
|
|
|
|
swigAddModules(); // add our own modules
|
|
|
|
swigSwitchPythonBuiltin(); // switch the python builtin modules to our new list
|
2012-03-11 19:07:10 +00:00
|
|
|
|
2012-08-01 11:54:20 +00:00
|
|
|
Py_Initialize();
|
2012-08-02 17:24:53 +00:00
|
|
|
|
2012-11-14 07:15:59 +00:00
|
|
|
#ifdef KICAD_SCRIPTING_WXPYTHON
|
2012-08-01 11:54:20 +00:00
|
|
|
PyEval_InitThreads();
|
|
|
|
|
2015-07-09 16:44:23 +00:00
|
|
|
#ifndef __WINDOWS__ // import wxversion.py currently not working under winbuilder, and not useful.
|
2015-02-15 00:23:54 +00:00
|
|
|
char cmd[1024];
|
|
|
|
// Make sure that that the correct version of wxPython is loaded. In systems where there
|
|
|
|
// are different versions of wxPython installed this can lead to select wrong wxPython
|
|
|
|
// version being selected.
|
2015-07-09 16:44:23 +00:00
|
|
|
snprintf( cmd, sizeof(cmd), "import wxversion; wxversion.select('%s')", WXPYTHON_VERSION );
|
2015-06-27 19:05:26 +00:00
|
|
|
|
|
|
|
int retv = PyRun_SimpleString( cmd );
|
|
|
|
|
|
|
|
if( retv != 0 )
|
|
|
|
{
|
|
|
|
wxLogError( wxT( "Python error %d occurred running string `%s`" ), retv, cmd );
|
|
|
|
PyErr_Print();
|
|
|
|
Py_Finalize();
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-09 16:44:23 +00:00
|
|
|
#endif // ifndef __WINDOWS__
|
2015-02-15 00:23:54 +00:00
|
|
|
|
2012-08-01 11:54:20 +00:00
|
|
|
// Load the wxPython core API. Imports the wx._core_ module and sets a
|
|
|
|
// local pointer to a function table located there. The pointer is used
|
|
|
|
// internally by the rest of the API functions.
|
2013-03-15 16:35:24 +00:00
|
|
|
if( !wxPyCoreAPI_IMPORT() )
|
2012-11-14 07:15:59 +00:00
|
|
|
{
|
2013-03-15 16:35:24 +00:00
|
|
|
wxLogError( wxT( "***** Error importing the wxPython API! *****" ) );
|
2012-08-01 11:54:20 +00:00
|
|
|
PyErr_Print();
|
|
|
|
Py_Finalize();
|
|
|
|
return false;
|
2012-11-14 07:15:59 +00:00
|
|
|
}
|
|
|
|
|
2015-07-09 16:51:08 +00:00
|
|
|
wxPythonLoaded = true;
|
|
|
|
|
2012-08-01 11:54:20 +00:00
|
|
|
// Save the current Python thread state and release the
|
|
|
|
// Global Interpreter Lock.
|
|
|
|
|
2012-08-02 07:47:30 +00:00
|
|
|
g_PythonMainTState = wxPyBeginAllowThreads();
|
2015-07-09 16:44:23 +00:00
|
|
|
#endif // ifdef KICAD_SCRIPTING_WXPYTHON
|
2012-03-11 19:07:10 +00:00
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
// load pcbnew inside python, and load all the user plugins, TODO: add system wide plugins
|
2013-03-11 08:09:48 +00:00
|
|
|
{
|
2013-07-19 18:27:22 +00:00
|
|
|
char cmd[1024];
|
2013-03-15 16:35:24 +00:00
|
|
|
PyLOCK lock;
|
2015-02-22 23:56:38 +00:00
|
|
|
snprintf( cmd, sizeof(cmd), "import sys, traceback\n"
|
2013-07-19 18:27:22 +00:00
|
|
|
"sys.path.append(\".\")\n"
|
|
|
|
"import pcbnew\n"
|
|
|
|
"pcbnew.LoadPlugins(\"%s\")", aUserPluginsPath );
|
|
|
|
PyRun_SimpleString( cmd );
|
2013-03-11 08:09:48 +00:00
|
|
|
}
|
2012-08-01 11:54:20 +00:00
|
|
|
|
|
|
|
return true;
|
2012-03-11 19:07:10 +00:00
|
|
|
}
|
2012-08-02 07:47:30 +00:00
|
|
|
|
2013-03-15 16:35:24 +00:00
|
|
|
|
2014-03-28 12:27:58 +00:00
|
|
|
void pcbnewFinishPythonScripting()
|
2012-08-02 07:47:30 +00:00
|
|
|
{
|
2012-08-02 17:24:53 +00:00
|
|
|
#ifdef KICAD_SCRIPTING_WXPYTHON
|
2013-03-15 16:35:24 +00:00
|
|
|
wxPyEndAllowThreads( g_PythonMainTState );
|
2012-08-02 17:24:53 +00:00
|
|
|
#endif
|
2012-08-02 07:47:30 +00:00
|
|
|
Py_Finalize();
|
|
|
|
}
|
2012-08-02 17:24:53 +00:00
|
|
|
|
2012-11-14 07:15:59 +00:00
|
|
|
|
2015-02-15 00:23:54 +00:00
|
|
|
#if defined( KICAD_SCRIPTING_WXPYTHON )
|
2012-08-02 17:24:53 +00:00
|
|
|
|
2012-11-14 07:15:59 +00:00
|
|
|
void RedirectStdio()
|
2012-08-02 17:24:53 +00:00
|
|
|
{
|
|
|
|
// This is a helpful little tidbit to help debugging and such. It
|
|
|
|
// redirects Python's stdout and stderr to a window that will popup
|
|
|
|
// only on demand when something is printed, like a traceback.
|
2012-11-14 07:15:59 +00:00
|
|
|
const char* python_redirect =
|
2013-03-15 16:35:24 +00:00
|
|
|
"import sys\n"
|
|
|
|
"import wx\n"
|
|
|
|
"output = wx.PyOnDemandOutputWindow()\n"
|
|
|
|
"sys.stderr = output\n";
|
|
|
|
|
2013-03-17 01:14:46 +00:00
|
|
|
PyLOCK lock;
|
2012-11-14 07:15:59 +00:00
|
|
|
|
2012-08-02 17:24:53 +00:00
|
|
|
PyRun_SimpleString( python_redirect );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-15 16:35:24 +00:00
|
|
|
wxWindow* CreatePythonShellWindow( wxWindow* parent )
|
2012-08-02 17:24:53 +00:00
|
|
|
{
|
2013-03-15 16:35:24 +00:00
|
|
|
const char* pycrust_panel =
|
|
|
|
"import wx\n"
|
|
|
|
"from wx.py import shell, version\n"
|
|
|
|
"\n"
|
2015-08-24 18:32:56 +00:00
|
|
|
"intro = \"PyCrust %s - KiCAD Python Shell\" % version.VERSION\n"
|
2013-03-15 16:35:24 +00:00
|
|
|
"\n"
|
|
|
|
"def makeWindow(parent):\n"
|
2015-08-24 18:32:56 +00:00
|
|
|
" pycrust = shell.Shell(parent, -1, introText=intro)\n"
|
|
|
|
" return pycrust\n"
|
2013-03-15 16:35:24 +00:00
|
|
|
"\n";
|
|
|
|
|
|
|
|
|
|
|
|
wxWindow* window = NULL;
|
|
|
|
PyObject* result;
|
2012-08-02 17:24:53 +00:00
|
|
|
|
|
|
|
// As always, first grab the GIL
|
2013-03-15 16:35:24 +00:00
|
|
|
PyLOCK lock;
|
2012-08-02 17:24:53 +00:00
|
|
|
|
|
|
|
// Now make a dictionary to serve as the global namespace when the code is
|
2012-11-14 07:15:59 +00:00
|
|
|
// executed. Put a reference to the builtins module in it.
|
2012-08-02 17:24:53 +00:00
|
|
|
|
2013-03-15 16:35:24 +00:00
|
|
|
PyObject* globals = PyDict_New();
|
|
|
|
PyObject* builtins = PyImport_ImportModule( "__builtin__" );
|
|
|
|
|
2012-08-02 17:24:53 +00:00
|
|
|
PyDict_SetItemString( globals, "__builtins__", builtins );
|
2013-03-15 16:35:24 +00:00
|
|
|
Py_DECREF( builtins );
|
2012-08-02 17:24:53 +00:00
|
|
|
|
|
|
|
// Execute the code to make the makeWindow function we defined above
|
|
|
|
result = PyRun_String( pycrust_panel, Py_file_input, globals, globals );
|
|
|
|
|
|
|
|
// Was there an exception?
|
2012-11-14 07:15:59 +00:00
|
|
|
if( !result )
|
2012-08-02 17:24:53 +00:00
|
|
|
{
|
|
|
|
PyErr_Print();
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-03-15 16:35:24 +00:00
|
|
|
|
|
|
|
Py_DECREF( result );
|
2012-08-02 17:24:53 +00:00
|
|
|
|
|
|
|
// Now there should be an object named 'makeWindow' in the dictionary that
|
|
|
|
// we can grab a pointer to:
|
|
|
|
PyObject* func = PyDict_GetItemString( globals, "makeWindow" );
|
|
|
|
wxASSERT( PyCallable_Check( func ) );
|
|
|
|
|
|
|
|
// Now build an argument tuple and call the Python function. Notice the
|
|
|
|
// use of another wxPython API to take a wxWindows object and build a
|
|
|
|
// wxPython object that wraps it.
|
|
|
|
|
2013-03-15 16:35:24 +00:00
|
|
|
PyObject* arg = wxPyMake_wxObject( parent, false );
|
2012-08-02 17:24:53 +00:00
|
|
|
wxASSERT( arg != NULL );
|
2012-11-14 07:15:59 +00:00
|
|
|
|
2013-03-15 16:35:24 +00:00
|
|
|
PyObject* tuple = PyTuple_New( 1 );
|
2012-08-02 17:24:53 +00:00
|
|
|
PyTuple_SET_ITEM( tuple, 0, arg );
|
2012-11-14 07:15:59 +00:00
|
|
|
|
2012-08-02 17:24:53 +00:00
|
|
|
result = PyEval_CallObject( func, tuple );
|
|
|
|
|
|
|
|
// Was there an exception?
|
2012-11-14 07:15:59 +00:00
|
|
|
if( !result )
|
2012-08-02 17:24:53 +00:00
|
|
|
PyErr_Print();
|
2012-11-14 07:15:59 +00:00
|
|
|
else
|
2012-08-02 17:24:53 +00:00
|
|
|
{
|
|
|
|
// Otherwise, get the returned window out of Python-land and
|
|
|
|
// into C++-ville...
|
2013-03-15 16:35:24 +00:00
|
|
|
bool success = wxPyConvertSwigPtr( result, (void**) &window, _T( "wxWindow" ) );
|
|
|
|
(void) success;
|
2012-11-14 07:15:59 +00:00
|
|
|
|
2013-03-15 16:35:24 +00:00
|
|
|
wxASSERT_MSG( success, _T( "Returned object was not a wxWindow!" ) );
|
|
|
|
Py_DECREF( result );
|
2012-08-02 17:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release the python objects we still have
|
|
|
|
Py_DECREF( globals );
|
|
|
|
Py_DECREF( tuple );
|
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|
2013-03-15 16:35:24 +00:00
|
|
|
|
|
|
|
|
2012-08-02 17:24:53 +00:00
|
|
|
#endif
|
2013-03-17 01:14:46 +00:00
|
|
|
|
|
|
|
wxArrayString PyArrayStringToWx( PyObject* aArrayString )
|
|
|
|
{
|
|
|
|
wxArrayString ret;
|
|
|
|
|
|
|
|
int list_size = PyList_Size( aArrayString );
|
|
|
|
|
|
|
|
for( int n = 0; n<list_size; n++ )
|
|
|
|
{
|
|
|
|
PyObject* element = PyList_GetItem( aArrayString, n );
|
|
|
|
|
|
|
|
ret.Add( FROM_UTF8( PyString_AsString( element ) ), 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString PyErrStringWithTraceback()
|
|
|
|
{
|
|
|
|
wxString err;
|
|
|
|
|
|
|
|
if( !PyErr_Occurred() )
|
|
|
|
return err;
|
|
|
|
|
|
|
|
PyObject* type;
|
|
|
|
PyObject* value;
|
|
|
|
PyObject* traceback;
|
|
|
|
|
|
|
|
PyErr_Fetch( &type, &value, &traceback );
|
|
|
|
|
|
|
|
PyObject* tracebackModuleString = PyString_FromString( (char*) "traceback" );
|
|
|
|
PyObject* tracebackModule = PyImport_Import( tracebackModuleString );
|
|
|
|
|
|
|
|
|
|
|
|
PyObject* formatException = PyObject_GetAttrString( tracebackModule,
|
|
|
|
(char*) "format_exception" );
|
|
|
|
PyObject* args = Py_BuildValue( "(O,O,O)", type, value, traceback );
|
|
|
|
|
|
|
|
PyObject* result = PyObject_CallObject( formatException, args );
|
|
|
|
|
|
|
|
Py_DECREF( args );
|
|
|
|
|
|
|
|
wxArrayString res = PyArrayStringToWx( result );
|
|
|
|
|
2013-03-17 19:04:42 +00:00
|
|
|
for( unsigned i = 0; i<res.Count(); i++ )
|
2013-03-17 01:14:46 +00:00
|
|
|
{
|
|
|
|
err += res[i] + wxT( "\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
PyErr_Clear();
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|