kicad/pcbnew/python/scripting/pcbnew_scripting.cpp

164 lines
4.6 KiB
C++
Raw Normal View History

2021-03-08 14:54:22 +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>
2021-07-19 23:56:05 +00:00
* Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
2021-03-08 14:54:22 +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
*/
2021-04-05 19:30:27 +00:00
#include <python_scripting.h>
2021-03-08 14:54:22 +00:00
#include <cstdlib>
#include <cstring>
#include <Python.h>
#include <sstream>
#include <eda_base_frame.h>
#include <gal/color4d.h>
#include <trace_helpers.h>
#include <string_utils.h>
2021-04-05 19:30:27 +00:00
#include <macros.h>
2021-03-08 14:54:22 +00:00
#include <paths.h>
#include <settings/settings_manager.h>
#include <kiplatform/environment.h>
#include <wx/app.h>
#include <config.h>
/**
* Run a python method from the pcbnew module.
*
2021-07-19 23:56:05 +00:00
* @param aMethodName is the name of the method (like "pcbnew.myfunction" ).
* @param aNames will contain the returned string.
2021-03-08 14:54:22 +00:00
*/
static void pcbnewRunPythonMethodWithReturnedString( const char* aMethodName, wxString& aNames )
{
aNames.Clear();
PyLOCK lock;
PyErr_Clear();
PyObject* builtins = PyImport_ImportModule( "pcbnew" );
wxASSERT( builtins );
if( !builtins ) // Something is wrong in pcbnew.py module (incorrect version?)
return;
PyObject* globals = PyDict_New();
PyDict_SetItemString( globals, "pcbnew", builtins );
Py_DECREF( builtins );
// Build the python code
char cmd[1024];
snprintf( cmd, sizeof(cmd), "result = %s()", aMethodName );
// Execute the python code and get the returned data
PyObject* localDict = PyDict_New();
PyObject* pobj = PyRun_String( cmd, Py_file_input, globals, localDict);
Py_DECREF( globals );
if( pobj )
{
PyObject* str = PyDict_GetItemString(localDict, "result" );
2021-07-19 23:56:05 +00:00
const char* str_res = nullptr;
2021-03-08 14:54:22 +00:00
if(str)
{
PyObject* temp_bytes = PyUnicode_AsEncodedString( str, "UTF-8", "strict" );
2021-07-19 23:56:05 +00:00
if( temp_bytes != nullptr )
2021-03-08 14:54:22 +00:00
{
str_res = PyBytes_AS_STRING( temp_bytes );
aNames = FROM_UTF8( str_res );
Py_DECREF( temp_bytes );
}
else
{
2023-01-22 14:41:33 +00:00
wxLogMessage( wxS( "cannot encode Unicode python string" ) );
2021-03-08 14:54:22 +00:00
}
}
else
{
aNames = wxString();
}
Py_DECREF( pobj );
}
Py_DECREF( localDict );
if( PyErr_Occurred() )
{
if( strcmp( aMethodName, "pcbnew.GetWizardsBackTrace" ) == 0 )
aNames = PyErrStringWithTraceback();
else
wxLogMessage( PyErrStringWithTraceback() );
}
2021-03-08 14:54:22 +00:00
}
void pcbnewGetUnloadableScriptNames( wxString& aNames )
{
pcbnewRunPythonMethodWithReturnedString( "pcbnew.GetUnLoadableWizards", aNames );
}
void pcbnewGetScriptsSearchPaths( wxString& aNames )
{
pcbnewRunPythonMethodWithReturnedString( "pcbnew.GetWizardsSearchPaths", aNames );
}
void pcbnewGetWizardsBackTrace( wxString& aTrace )
{
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 )
{
2022-02-05 02:06:25 +00:00
if( traces[ii].Contains( wxT( "Traceback" ) ) )
2021-03-08 14:54:22 +00:00
{
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
2022-02-05 02:06:25 +00:00
aTrace << wxT( "\n**********************************\n" );
2021-03-08 14:54:22 +00:00
}
else
2021-07-19 23:56:05 +00:00
{
2022-02-05 02:06:25 +00:00
aTrace += traces[ii] + wxT( "\n" );
2021-07-19 23:56:05 +00:00
}
2021-03-08 14:54:22 +00:00
}
}