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>
|
2019-05-23 20:55:27 +00:00
|
|
|
* Copyright (C) 1992-2019 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
|
|
|
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2012-03-11 19:07:10 +00:00
|
|
|
#include <python_scripting.h>
|
2018-08-13 20:14:06 +00:00
|
|
|
#include <sstream>
|
2012-08-02 17:24:53 +00:00
|
|
|
|
2018-01-29 16:34:12 +00:00
|
|
|
#include <eda_base_frame.h>
|
2017-02-20 16:57:41 +00:00
|
|
|
#include <gal/color4d.h>
|
2020-08-11 23:11:48 +00:00
|
|
|
#include <trace_helpers.h>
|
2020-12-15 22:46:52 +00:00
|
|
|
#include <kicad_string.h>
|
2012-03-11 19:07:10 +00:00
|
|
|
|
2021-01-23 02:23:37 +00:00
|
|
|
#include <paths.h>
|
2016-12-28 09:26:01 +00:00
|
|
|
#include <pgm_base.h>
|
2020-09-17 03:06:57 +00:00
|
|
|
#include <settings/settings_manager.h>
|
2016-12-28 09:26:01 +00:00
|
|
|
|
2021-01-23 02:23:37 +00:00
|
|
|
#include <kiplatform/environment.h>
|
|
|
|
|
2020-10-18 11:31:07 +00:00
|
|
|
#include <wx/app.h>
|
|
|
|
|
2021-01-12 16:19:59 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2012-03-11 19:07:10 +00:00
|
|
|
/* init functions defined by swig */
|
|
|
|
|
2018-08-03 13:23:32 +00:00
|
|
|
#if PY_MAJOR_VERSION >= 3
|
2018-08-04 10:03:37 +00:00
|
|
|
extern "C" PyObject* PyInit__pcbnew( void );
|
2018-08-03 13:23:32 +00:00
|
|
|
#else
|
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 );
|
2018-08-03 13:23:32 +00:00
|
|
|
#endif
|
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
|
|
|
|
2019-05-23 20:55:27 +00:00
|
|
|
/// True if the wxPython scripting layer was successfully loaded.
|
|
|
|
static bool wxPythonLoaded = false;
|
|
|
|
|
2015-07-09 16:44:23 +00:00
|
|
|
|
|
|
|
bool IsWxPythonLoaded()
|
|
|
|
{
|
|
|
|
return wxPythonLoaded;
|
|
|
|
}
|
|
|
|
|
2012-03-11 19:07:10 +00:00
|
|
|
|
2019-05-23 20:55:27 +00:00
|
|
|
/**
|
|
|
|
* Add a name + initfuction to our SwigImportInittab
|
|
|
|
*/
|
2012-03-11 19:07:10 +00:00
|
|
|
|
2018-08-03 13:23:32 +00:00
|
|
|
#if PY_MAJOR_VERSION >= 3
|
|
|
|
static void swigAddModule( const char* name, PyObject* (* initfunc)() )
|
|
|
|
#else
|
2012-06-27 21:19:19 +00:00
|
|
|
static void swigAddModule( const char* name, void (* initfunc)() )
|
2018-08-03 13:23:32 +00:00
|
|
|
#endif
|
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
|
|
|
|
2019-05-23 20:55:27 +00:00
|
|
|
/**
|
|
|
|
* Add the builtin python modules
|
|
|
|
*/
|
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(
|
2018-07-11 07:46:37 +00:00
|
|
|
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
|
|
|
|
2019-05-23 20:55:27 +00:00
|
|
|
/**
|
|
|
|
* Add the internal modules to the python scripting so they will be available to the scripts.
|
2012-06-27 21:19:19 +00:00
|
|
|
*/
|
2012-03-11 19:07:10 +00:00
|
|
|
static void swigAddModules()
|
|
|
|
{
|
2018-08-04 10:03:37 +00:00
|
|
|
#if PY_MAJOR_VERSION >= 3
|
|
|
|
swigAddModule( "_pcbnew", PyInit__pcbnew );
|
|
|
|
#else
|
2012-06-27 21:19:19 +00:00
|
|
|
swigAddModule( "_pcbnew", init_pcbnew );
|
2018-08-04 10:03:37 +00:00
|
|
|
#endif
|
2012-06-27 21:19:19 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2019-05-23 20:55:27 +00:00
|
|
|
/**
|
|
|
|
* Switch the python module table to the Pcbnew built one.
|
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
|
|
|
|
2013-03-11 08:09:48 +00:00
|
|
|
PyThreadState* g_PythonMainTState;
|
2012-08-02 07:47:30 +00:00
|
|
|
|
2019-05-23 20:55:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the python environment and publish the Pcbnew interface inside it.
|
|
|
|
*
|
|
|
|
* This initializes all the wxPython interface and returns the python thread control structure
|
|
|
|
*/
|
2021-01-29 04:45:38 +00:00
|
|
|
bool pcbnewInitPythonScripting( const char* aStockScriptingPath, const char* aUserScriptingPath )
|
2012-03-11 19:07:10 +00:00
|
|
|
{
|
2019-05-23 20:55:27 +00:00
|
|
|
int retv;
|
|
|
|
char cmd[1024];
|
|
|
|
|
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
|
|
|
|
2020-09-23 00:02:32 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
// Under vcpkg/msvc, we need to explicitly set the python home
|
|
|
|
// or else it'll start consuming system python registry keys and the like instead
|
|
|
|
// We are going to follow the "unix" layout for the msvc/vcpkg distributions so exes in /root/bin
|
2021-01-07 04:32:56 +00:00
|
|
|
// And the python lib in /root/lib/python3(/Lib,/DLLs)
|
2020-09-23 00:02:32 +00:00
|
|
|
wxFileName pyHome;
|
|
|
|
|
|
|
|
pyHome.Assign( Pgm().GetExecutablePath() );
|
|
|
|
|
|
|
|
pyHome.Normalize();
|
|
|
|
|
|
|
|
// MUST be called before Py_Initialize so it will to create valid default lib paths
|
2021-02-14 14:33:57 +00:00
|
|
|
if( !wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
|
|
|
|
{
|
|
|
|
Py_SetPythonHome( pyHome.GetFullPath().c_str() );
|
|
|
|
}
|
2020-09-23 00:02:32 +00:00
|
|
|
#endif
|
|
|
|
|
2012-08-01 11:54:20 +00:00
|
|
|
Py_Initialize();
|
2018-07-11 07:46:37 +00:00
|
|
|
PySys_SetArgv( Pgm().App().argc, Pgm().App().argv );
|
2012-08-02 17:24:53 +00:00
|
|
|
|
2012-11-14 07:15:59 +00:00
|
|
|
#ifdef KICAD_SCRIPTING_WXPYTHON
|
2020-12-27 18:18:49 +00:00
|
|
|
|
|
|
|
#if PY_VERSION_HEX < 0x03070000 // PyEval_InitThreads() is called by Py_Initialize() starting with version 3.7
|
2012-08-01 11:54:20 +00:00
|
|
|
PyEval_InitThreads();
|
2020-12-27 18:18:49 +00:00
|
|
|
#endif // if PY_VERSION_HEX < 0x03070000
|
2012-08-01 11:54:20 +00:00
|
|
|
|
2018-08-13 20:14:06 +00:00
|
|
|
#ifndef KICAD_SCRIPTING_WXPYTHON_PHOENIX
|
2019-05-23 20:55:27 +00:00
|
|
|
#ifndef __WINDOWS__ // import wxversion.py currently not working under winbuilder, and not useful.
|
2015-02-15 00:23:54 +00:00
|
|
|
// 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.
|
2018-07-11 07:46:37 +00:00
|
|
|
snprintf( cmd, sizeof( cmd ), "import wxversion; wxversion.select( '%s' )", WXPYTHON_VERSION );
|
2015-06-27 19:05:26 +00:00
|
|
|
|
2019-05-23 20:55:27 +00:00
|
|
|
retv = PyRun_SimpleString( cmd );
|
2015-06-27 19:05:26 +00:00
|
|
|
|
|
|
|
if( retv != 0 )
|
|
|
|
{
|
2019-05-23 20:55:27 +00:00
|
|
|
wxLogError( "Python error %d occurred running command:\n\n`%s`", retv, cmd );
|
2015-06-27 19:05:26 +00:00
|
|
|
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
|
|
|
{
|
2019-05-23 20:55:27 +00:00
|
|
|
wxLogError( "***** 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
|
|
|
}
|
2020-12-27 18:18:49 +00:00
|
|
|
#endif // ifndef KICAD_SCRIPTING_WXPYTHON_PHOENIX
|
2012-11-14 07:15:59 +00:00
|
|
|
|
2019-05-23 20:55:27 +00:00
|
|
|
#if defined( DEBUG )
|
|
|
|
RedirectStdio();
|
|
|
|
#endif
|
|
|
|
|
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.
|
2018-08-13 20:14:06 +00:00
|
|
|
g_PythonMainTState = PyEval_SaveThread();
|
2012-08-01 11:54:20 +00:00
|
|
|
|
2015-07-09 16:44:23 +00:00
|
|
|
#endif // ifdef KICAD_SCRIPTING_WXPYTHON
|
2012-03-11 19:07:10 +00:00
|
|
|
|
2020-09-17 03:06:57 +00:00
|
|
|
// Load pcbnew inside Python and load all the user plugins and package-based plugins
|
2013-03-11 08:09:48 +00:00
|
|
|
{
|
2013-03-15 16:35:24 +00:00
|
|
|
PyLOCK lock;
|
2019-05-23 20:55:27 +00:00
|
|
|
|
2020-08-09 18:49:26 +00:00
|
|
|
// Load os so that we can modify the environment variables through python
|
|
|
|
snprintf( cmd, sizeof( cmd ), "import sys, os, traceback\n"
|
2019-05-23 20:55:27 +00:00
|
|
|
"sys.path.append(\".\")\n"
|
|
|
|
"import pcbnew\n"
|
2021-01-29 04:45:38 +00:00
|
|
|
"pcbnew.LoadPlugins(\"%s\", \"%s\")",
|
|
|
|
aStockScriptingPath, aUserScriptingPath );
|
2019-05-23 20:55:27 +00:00
|
|
|
retv = PyRun_SimpleString( cmd );
|
|
|
|
|
|
|
|
if( retv != 0 )
|
|
|
|
wxLogError( "Python error %d occurred running command:\n\n`%s`", retv, 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
|
|
|
|
2017-01-07 12:39:47 +00:00
|
|
|
/**
|
2019-05-23 20:55:27 +00:00
|
|
|
* Run a python method from the pcbnew module.
|
|
|
|
*
|
2017-01-07 12:39:47 +00:00
|
|
|
* @param aMethodName is the name of the method (like "pcbnew.myfunction" )
|
2019-05-23 20:55:27 +00:00
|
|
|
* @param aNames will contain the returned string
|
2017-01-07 12:39:47 +00:00
|
|
|
*/
|
|
|
|
static void pcbnewRunPythonMethodWithReturnedString( const char* aMethodName, wxString& aNames )
|
2017-01-06 19:15:23 +00:00
|
|
|
{
|
2017-01-07 12:39:47 +00:00
|
|
|
aNames.Clear();
|
|
|
|
|
2017-01-06 19:15:23 +00:00
|
|
|
PyLOCK lock;
|
|
|
|
PyErr_Clear();
|
|
|
|
|
2017-01-07 15:25:11 +00:00
|
|
|
PyObject* builtins = PyImport_ImportModule( "pcbnew" );
|
|
|
|
wxASSERT( builtins );
|
|
|
|
|
|
|
|
if( !builtins ) // Something is wrong in pcbnew.py module (incorrect version?)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PyObject* globals = PyDict_New();
|
2017-01-06 19:15:23 +00:00
|
|
|
PyDict_SetItemString( globals, "pcbnew", builtins );
|
|
|
|
Py_DECREF( builtins );
|
|
|
|
|
2017-01-07 12:39:47 +00:00
|
|
|
// Build the python code
|
|
|
|
char cmd[1024];
|
|
|
|
snprintf( cmd, sizeof(cmd), "result = %s()", aMethodName );
|
|
|
|
|
|
|
|
// Execute the python code and get the returned data
|
2017-01-06 19:15:23 +00:00
|
|
|
PyObject* localDict = PyDict_New();
|
2017-01-07 12:39:47 +00:00
|
|
|
PyObject* pobj = PyRun_String( cmd, Py_file_input, globals, localDict);
|
2017-01-06 19:15:23 +00:00
|
|
|
Py_DECREF( globals );
|
|
|
|
|
|
|
|
if( pobj )
|
|
|
|
{
|
|
|
|
PyObject* str = PyDict_GetItemString(localDict, "result" );
|
2018-08-03 13:23:32 +00:00
|
|
|
#if PY_MAJOR_VERSION >= 3
|
2018-08-05 07:38:53 +00:00
|
|
|
const char* str_res = NULL;
|
2019-05-23 20:55:27 +00:00
|
|
|
|
2018-10-11 20:37:09 +00:00
|
|
|
if(str)
|
|
|
|
{
|
2018-08-05 07:38:53 +00:00
|
|
|
PyObject* temp_bytes = PyUnicode_AsEncodedString( str, "UTF-8", "strict" );
|
2019-05-23 20:55:27 +00:00
|
|
|
|
2018-10-17 08:35:27 +00:00
|
|
|
if( temp_bytes != NULL )
|
2018-10-11 20:37:09 +00:00
|
|
|
{
|
2018-08-05 07:38:53 +00:00
|
|
|
str_res = PyBytes_AS_STRING( temp_bytes );
|
|
|
|
str_res = strdup( str_res );
|
|
|
|
Py_DECREF( temp_bytes );
|
2018-10-11 20:37:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-05 07:38:53 +00:00
|
|
|
wxLogMessage( "cannot encode unicode python string" );
|
|
|
|
}
|
|
|
|
}
|
2018-08-03 13:23:32 +00:00
|
|
|
#else
|
2017-01-06 19:15:23 +00:00
|
|
|
const char* str_res = str ? PyString_AsString( str ) : 0;
|
2018-08-03 13:23:32 +00:00
|
|
|
#endif
|
2017-01-06 19:15:23 +00:00
|
|
|
aNames = FROM_UTF8( str_res );
|
|
|
|
Py_DECREF( pobj );
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_DECREF( localDict );
|
|
|
|
|
|
|
|
if( PyErr_Occurred() )
|
2019-05-23 20:55:27 +00:00
|
|
|
wxLogMessage( PyErrStringWithTraceback() );
|
2017-01-06 19:15:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-07 12:39:47 +00:00
|
|
|
void pcbnewGetUnloadableScriptNames( wxString& aNames )
|
|
|
|
{
|
|
|
|
pcbnewRunPythonMethodWithReturnedString( "pcbnew.GetUnLoadableWizards", aNames );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void pcbnewGetScriptsSearchPaths( wxString& aNames )
|
|
|
|
{
|
|
|
|
pcbnewRunPythonMethodWithReturnedString( "pcbnew.GetWizardsSearchPaths", aNames );
|
|
|
|
}
|
|
|
|
|
2019-05-23 20:55:27 +00:00
|
|
|
|
2020-12-15 22:46:52 +00:00
|
|
|
void pcbnewGetWizardsBackTrace( wxString& aTrace )
|
2017-01-07 19:43:39 +00:00
|
|
|
{
|
2020-12-15 22:46:52 +00:00
|
|
|
pcbnewRunPythonMethodWithReturnedString( "pcbnew.GetWizardsBackTrace", aTrace );
|
|
|
|
|
|
|
|
// Filter message before displaying them
|
|
|
|
// a trace starts by "Traceback" and is followed by 2 useless lines
|
|
|
|
// for our purpose
|
|
|
|
wxArrayString traces;
|
|
|
|
wxStringSplit( aTrace, traces, '\n' );
|
|
|
|
|
|
|
|
// Build the filtered message (remove useless lines)
|
|
|
|
aTrace.Clear();
|
|
|
|
|
|
|
|
for( unsigned ii = 0; ii < traces.Count(); ++ii )
|
|
|
|
{
|
|
|
|
if( traces[ii].Contains( "Traceback" ) )
|
|
|
|
{
|
|
|
|
ii += 2; // Skip this line and next lines which are related to pcbnew.py module
|
|
|
|
|
|
|
|
if( !aTrace.IsEmpty() ) // Add separator for the next trace block
|
|
|
|
aTrace << "\n**********************************\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
aTrace += traces[ii] + "\n";
|
|
|
|
}
|
2017-01-07 19:43:39 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 12:39:47 +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
|
2018-08-13 20:14:06 +00:00
|
|
|
PyEval_RestoreThread( 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
|
|
|
|
2020-08-10 23:50:09 +00:00
|
|
|
wxString PyEscapeString( const wxString& aSource )
|
|
|
|
{
|
|
|
|
wxString converted;
|
|
|
|
|
|
|
|
for( wxUniChar c: aSource )
|
|
|
|
{
|
|
|
|
if( c == '\\' )
|
|
|
|
converted += "\\\\";
|
|
|
|
else if( c == '\'' )
|
|
|
|
converted += "\\\'";
|
|
|
|
else if( c == '\"' )
|
|
|
|
converted += "\\\"";
|
|
|
|
else
|
|
|
|
converted += c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return converted;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-11 23:11:48 +00:00
|
|
|
void pcbnewUpdatePythonEnvVar( const wxString& aVar, const wxString& aValue )
|
2020-08-09 18:49:26 +00:00
|
|
|
{
|
|
|
|
char cmd[1024];
|
|
|
|
|
|
|
|
// Ensure the interpreter is initalized before we try to interact with it
|
|
|
|
if( !Py_IsInitialized() )
|
|
|
|
return;
|
|
|
|
|
2020-08-11 23:11:48 +00:00
|
|
|
wxLogTrace( traceEnvVars, "pcbnewUpdatePythonEnvVar: Updating Python variable %s = %s",
|
|
|
|
aVar, aValue );
|
|
|
|
|
2020-08-10 23:50:09 +00:00
|
|
|
wxString escapedVar = PyEscapeString( aVar );
|
|
|
|
wxString escapedVal = PyEscapeString( aValue );
|
|
|
|
|
2020-08-09 18:49:26 +00:00
|
|
|
snprintf( cmd, sizeof( cmd ),
|
|
|
|
"# coding=utf-8\n" // The values could potentially be UTF8
|
|
|
|
"os.environ[\"%s\"]=\"%s\"\n",
|
2020-08-10 23:50:09 +00:00
|
|
|
TO_UTF8( escapedVar ),
|
|
|
|
TO_UTF8( escapedVal ) );
|
2020-08-09 18:49:26 +00:00
|
|
|
|
|
|
|
PyLOCK lock;
|
|
|
|
|
|
|
|
int retv = PyRun_SimpleString( cmd );
|
|
|
|
|
|
|
|
if( retv != 0 )
|
|
|
|
wxLogError( "Python error %d occurred running command:\n\n`%s`", retv, cmd );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-15 00:23:54 +00:00
|
|
|
#if defined( KICAD_SCRIPTING_WXPYTHON )
|
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
|
|
|
|
2019-05-23 20:55:27 +00:00
|
|
|
int retv = PyRun_SimpleString( python_redirect );
|
|
|
|
|
|
|
|
if( retv != 0 )
|
|
|
|
wxLogError( "Python error %d occurred running command:\n\n`%s`", retv, python_redirect );
|
2012-08-02 17:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-21 14:55:31 +00:00
|
|
|
wxWindow* CreatePythonShellWindow( wxWindow* parent, const wxString& aFramenameId )
|
2012-08-02 17:24:53 +00:00
|
|
|
{
|
2018-08-13 20:14:06 +00:00
|
|
|
// parent is actually *PCB_EDIT_FRAME
|
|
|
|
const int parentId = parent->GetId();
|
|
|
|
{
|
|
|
|
wxWindow* parent2 = wxWindow::FindWindowById( parentId );
|
|
|
|
wxASSERT( parent2 == parent );
|
|
|
|
}
|
2013-03-15 16:35:24 +00:00
|
|
|
|
2018-08-13 20:14:06 +00:00
|
|
|
// passing window ids instead of pointers is because wxPython is not
|
|
|
|
// exposing the needed c++ apis to make that possible.
|
|
|
|
std::stringstream pcbnew_pyshell_one_step;
|
|
|
|
pcbnew_pyshell_one_step << "import kicad_pyshell\n";
|
|
|
|
pcbnew_pyshell_one_step << "import wx\n";
|
|
|
|
pcbnew_pyshell_one_step << "\n";
|
|
|
|
pcbnew_pyshell_one_step << "parent = wx.FindWindowById( " << parentId << " )\n";
|
|
|
|
pcbnew_pyshell_one_step << "newshell = kicad_pyshell.makePcbnewShellWindow( parent )\n";
|
|
|
|
pcbnew_pyshell_one_step << "newshell.SetName( \"" << aFramenameId << "\" )\n";
|
|
|
|
// return value goes into a "global". It's not actually global, but rather
|
|
|
|
// the dict that is passed to PyRun_String
|
|
|
|
pcbnew_pyshell_one_step << "retval = newshell.GetId()\n";
|
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();
|
2018-08-13 18:42:08 +00:00
|
|
|
#if PY_MAJOR_VERSION >= 3
|
|
|
|
PyObject* builtins = PyImport_ImportModule( "builtins" );
|
|
|
|
#else
|
2013-03-15 16:35:24 +00:00
|
|
|
PyObject* builtins = PyImport_ImportModule( "__builtin__" );
|
2018-08-13 18:42:08 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
wxASSERT( builtins );
|
2013-03-15 16:35:24 +00:00
|
|
|
|
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
|
|
|
|
2019-01-25 05:25:39 +00:00
|
|
|
#ifdef KICAD_SCRIPTING_WXPYTHON_PHOENIX
|
2019-01-25 03:36:05 +00:00
|
|
|
auto app_ptr = wxTheApp;
|
2019-01-25 05:25:39 +00:00
|
|
|
#endif
|
2012-08-02 17:24:53 +00:00
|
|
|
// Execute the code to make the makeWindow function we defined above
|
2019-05-23 20:55:27 +00:00
|
|
|
PyObject* result = PyRun_String( pcbnew_pyshell_one_step.str().c_str(), Py_file_input,
|
|
|
|
globals, globals );
|
2012-08-02 17:24:53 +00:00
|
|
|
|
2019-01-25 03:36:05 +00:00
|
|
|
#ifdef KICAD_SCRIPTING_WXPYTHON_PHOENIX
|
|
|
|
// This absolutely ugly hack is to work around the pyshell re-writing the global
|
|
|
|
// wxApp variable. Once we can initialize Phoenix to access the appropriate instance
|
|
|
|
// of wx, we can remove this line
|
|
|
|
wxApp::SetInstance( app_ptr );
|
|
|
|
#endif
|
2012-08-02 17:24:53 +00:00
|
|
|
// 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
|
|
|
|
2018-08-13 20:14:06 +00:00
|
|
|
result = PyDict_GetItemString( globals, "retval" );
|
2012-08-02 17:24:53 +00:00
|
|
|
|
2018-08-13 20:14:06 +00:00
|
|
|
#if PY_MAJOR_VERSION >= 3
|
|
|
|
if( !PyLong_Check( result ) )
|
|
|
|
#else
|
|
|
|
if( !PyInt_Check( result ) )
|
|
|
|
#endif
|
|
|
|
{
|
2019-05-23 20:55:27 +00:00
|
|
|
wxLogError( "creation of scripting window didn't return a number" );
|
2018-08-13 20:14:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-08-02 17:24:53 +00:00
|
|
|
|
2018-08-13 20:14:06 +00:00
|
|
|
#if PY_MAJOR_VERSION >= 3
|
|
|
|
const long windowId = PyLong_AsLong( result );
|
|
|
|
#else
|
|
|
|
const long windowId = PyInt_AsLong( result );
|
|
|
|
#endif
|
2012-11-14 07:15:59 +00:00
|
|
|
|
2018-08-13 20:14:06 +00:00
|
|
|
// It's important not to decref globals before extracting the window id.
|
|
|
|
// If you do it early, globals, and the retval int it contains, may/will be garbage collected.
|
|
|
|
// We do not need to decref result, because GetItemString returns a borrowed reference.
|
|
|
|
Py_DECREF( globals );
|
2012-11-14 07:15:59 +00:00
|
|
|
|
2018-08-13 20:14:06 +00:00
|
|
|
wxWindow* window = wxWindow::FindWindowById( windowId );
|
2012-08-02 17:24:53 +00:00
|
|
|
|
2018-08-13 20:14:06 +00:00
|
|
|
if( !window )
|
2012-08-02 17:24:53 +00:00
|
|
|
{
|
2019-05-23 20:55:27 +00:00
|
|
|
wxLogError( "unable to find pyshell window with id %d", windowId );
|
2018-08-13 20:14:06 +00:00
|
|
|
return NULL;
|
2012-08-02 17:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|
|
|
|
#endif
|
2013-03-17 01:14:46 +00:00
|
|
|
|
2019-05-23 20:55:27 +00:00
|
|
|
|
2018-10-12 19:36:50 +00:00
|
|
|
wxString PyStringToWx( PyObject* aString )
|
|
|
|
{
|
|
|
|
wxString ret;
|
|
|
|
|
|
|
|
if( !aString )
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
#if PY_MAJOR_VERSION >= 3
|
|
|
|
const char* str_res = NULL;
|
|
|
|
PyObject* temp_bytes = PyUnicode_AsEncodedString( aString, "UTF-8", "strict" );
|
2019-05-23 20:55:27 +00:00
|
|
|
|
2018-10-17 08:35:27 +00:00
|
|
|
if( temp_bytes != NULL )
|
2018-10-12 19:36:50 +00:00
|
|
|
{
|
|
|
|
str_res = PyBytes_AS_STRING( temp_bytes );
|
|
|
|
ret = FROM_UTF8( str_res );
|
|
|
|
Py_DECREF( temp_bytes );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxLogMessage( "cannot encode unicode python string" );
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
const char* str_res = PyString_AsString( aString );
|
|
|
|
ret = FROM_UTF8( str_res );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-17 01:14:46 +00:00
|
|
|
wxArrayString PyArrayStringToWx( PyObject* aArrayString )
|
|
|
|
{
|
|
|
|
wxArrayString ret;
|
|
|
|
|
2017-01-06 19:15:23 +00:00
|
|
|
if( !aArrayString )
|
|
|
|
return ret;
|
2013-03-17 01:14:46 +00:00
|
|
|
|
2017-01-06 19:15:23 +00:00
|
|
|
int list_size = PyList_Size( aArrayString );
|
|
|
|
|
|
|
|
for( int n = 0; n < list_size; n++ )
|
2013-03-17 01:14:46 +00:00
|
|
|
{
|
|
|
|
PyObject* element = PyList_GetItem( aArrayString, n );
|
|
|
|
|
2017-01-06 19:15:23 +00:00
|
|
|
if( element )
|
2018-08-03 13:23:32 +00:00
|
|
|
{
|
|
|
|
#if PY_MAJOR_VERSION >= 3
|
2018-10-18 06:39:36 +00:00
|
|
|
const char* str_res = NULL;
|
|
|
|
PyObject* temp_bytes = PyUnicode_AsEncodedString( element, "UTF-8", "strict" );
|
2019-05-23 20:55:27 +00:00
|
|
|
|
2018-10-18 06:39:36 +00:00
|
|
|
if( temp_bytes != NULL )
|
|
|
|
{
|
|
|
|
str_res = PyBytes_AS_STRING( temp_bytes );
|
|
|
|
ret.Add( FROM_UTF8( str_res ), 1 );
|
|
|
|
Py_DECREF( temp_bytes );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxLogMessage( "cannot encode unicode python string" );
|
|
|
|
}
|
2018-08-03 13:23:32 +00:00
|
|
|
#else
|
2018-10-18 06:39:36 +00:00
|
|
|
ret.Add( FROM_UTF8( PyString_AsString( element ) ), 1 );
|
2018-08-03 13:23:32 +00:00
|
|
|
#endif
|
|
|
|
}
|
2013-03-17 01:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString PyErrStringWithTraceback()
|
|
|
|
{
|
|
|
|
wxString err;
|
|
|
|
|
|
|
|
if( !PyErr_Occurred() )
|
|
|
|
return err;
|
|
|
|
|
|
|
|
PyObject* type;
|
|
|
|
PyObject* value;
|
|
|
|
PyObject* traceback;
|
|
|
|
|
|
|
|
PyErr_Fetch( &type, &value, &traceback );
|
|
|
|
|
2018-10-10 09:24:45 +00:00
|
|
|
PyErr_NormalizeException( &type, &value, &traceback );
|
2019-05-23 20:55:27 +00:00
|
|
|
|
2018-10-17 08:35:27 +00:00
|
|
|
if( traceback == NULL )
|
2018-10-11 20:37:09 +00:00
|
|
|
{
|
2018-08-05 09:28:57 +00:00
|
|
|
traceback = Py_None;
|
|
|
|
Py_INCREF( traceback );
|
|
|
|
}
|
|
|
|
|
2018-08-03 13:23:32 +00:00
|
|
|
#if PY_MAJOR_VERSION >= 3
|
2018-10-10 09:24:45 +00:00
|
|
|
PyException_SetTraceback( value, traceback );
|
2018-08-05 09:28:57 +00:00
|
|
|
|
2018-08-03 13:23:32 +00:00
|
|
|
PyObject* tracebackModuleString = PyUnicode_FromString( "traceback" );
|
|
|
|
#else
|
2017-01-06 19:15:23 +00:00
|
|
|
PyObject* tracebackModuleString = PyString_FromString( "traceback" );
|
2018-08-03 13:23:32 +00:00
|
|
|
#endif
|
2017-01-06 19:15:23 +00:00
|
|
|
PyObject* tracebackModule = PyImport_Import( tracebackModuleString );
|
|
|
|
Py_DECREF( tracebackModuleString );
|
|
|
|
|
|
|
|
PyObject* formatException = PyObject_GetAttrString( tracebackModule,
|
|
|
|
"format_exception" );
|
|
|
|
Py_DECREF( tracebackModule );
|
|
|
|
|
|
|
|
PyObject* args = Py_BuildValue( "(O,O,O)", type, value, traceback );
|
|
|
|
PyObject* result = PyObject_CallObject( formatException, args );
|
|
|
|
Py_XDECREF( formatException );
|
|
|
|
Py_XDECREF( args );
|
|
|
|
Py_XDECREF( type );
|
|
|
|
Py_XDECREF( value );
|
|
|
|
Py_XDECREF( traceback );
|
2013-03-17 01:14:46 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2016-12-28 09:26:01 +00:00
|
|
|
|
2019-05-23 20:55:27 +00:00
|
|
|
|
2016-12-28 09:26:01 +00:00
|
|
|
/**
|
2019-05-23 20:55:27 +00:00
|
|
|
* Find the Python scripting path.
|
2016-12-28 09:26:01 +00:00
|
|
|
*/
|
2020-09-17 03:06:57 +00:00
|
|
|
wxString PyScriptingPath( bool aUserPath )
|
2016-12-28 09:26:01 +00:00
|
|
|
{
|
|
|
|
wxString path;
|
|
|
|
|
2019-05-23 20:55:27 +00:00
|
|
|
//@todo This should this be a user configurable variable eg KISCRIPT?
|
2020-09-22 22:32:50 +00:00
|
|
|
if( aUserPath )
|
2020-09-17 03:06:57 +00:00
|
|
|
{
|
2021-01-23 02:23:37 +00:00
|
|
|
path = PATHS::GetUserScriptingPath();
|
2020-09-17 03:06:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-23 04:27:39 +00:00
|
|
|
path = PATHS::GetStockScriptingPath();
|
2020-09-17 03:06:57 +00:00
|
|
|
}
|
2016-12-28 09:26:01 +00:00
|
|
|
|
|
|
|
wxFileName scriptPath( path );
|
|
|
|
scriptPath.MakeAbsolute();
|
|
|
|
|
2018-04-27 12:03:33 +00:00
|
|
|
// Convert '\' to '/' in path, because later python script read \n or \r
|
2018-04-27 14:01:46 +00:00
|
|
|
// as escaped sequence, and create issues, when calling it by PyRun_SimpleString() method.
|
2018-04-27 12:03:33 +00:00
|
|
|
// It can happen on Windows.
|
|
|
|
path = scriptPath.GetFullPath();
|
|
|
|
path.Replace( '\\', '/' );
|
|
|
|
|
|
|
|
return path;
|
2016-12-28 09:26:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 20:55:27 +00:00
|
|
|
|
2020-09-17 03:06:57 +00:00
|
|
|
wxString PyPluginsPath( bool aUserPath )
|
2016-12-28 09:26:01 +00:00
|
|
|
{
|
2018-04-27 14:01:46 +00:00
|
|
|
// Note we are using unix path separator, because window separator sometimes
|
|
|
|
// creates issues when passing a command string to a python method by PyRun_SimpleString
|
2020-09-17 03:06:57 +00:00
|
|
|
return PyScriptingPath( aUserPath ) + '/' + "plugins";
|
2016-12-28 09:26:01 +00:00
|
|
|
}
|