KiCad PCB EDA Suite
pcbnew_scripting.cpp File Reference
#include <python_scripting.h>
#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>
#include <macros.h>
#include <paths.h>
#include <settings/settings_manager.h>
#include <kiplatform/environment.h>
#include <wx/app.h>
#include <config.h>

Go to the source code of this file.

Functions

static void pcbnewRunPythonMethodWithReturnedString (const char *aMethodName, wxString &aNames)
 Run a python method from the pcbnew module. More...
 
void pcbnewGetUnloadableScriptNames (wxString &aNames)
 Collect the list of python scripts which could not be loaded. More...
 
void pcbnewGetScriptsSearchPaths (wxString &aNames)
 Collect the list of paths where python scripts are searched. More...
 
void pcbnewGetWizardsBackTrace (wxString &aTrace)
 Return the backtrace of errors (if any) when wizard python scripts are loaded. More...
 

Function Documentation

◆ pcbnewGetScriptsSearchPaths()

void pcbnewGetScriptsSearchPaths ( wxString &  aNames)

Collect the list of paths where python scripts are searched.

Parameters
aNamesis a wxString which will contain the paths (separated by '
')

Definition at line 130 of file pcbnew_scripting.cpp.

131{
132 pcbnewRunPythonMethodWithReturnedString( "pcbnew.GetWizardsSearchPaths", aNames );
133}
static void pcbnewRunPythonMethodWithReturnedString(const char *aMethodName, wxString &aNames)
Run a python method from the pcbnew module.

References pcbnewRunPythonMethodWithReturnedString().

Referenced by DIALOG_FOOTPRINT_WIZARD_LIST::initLists().

◆ pcbnewGetUnloadableScriptNames()

void pcbnewGetUnloadableScriptNames ( wxString &  aNames)

Collect the list of python scripts which could not be loaded.

Parameters
aNamesis a wxString which will contain the filenames (separated by '
')

Definition at line 124 of file pcbnew_scripting.cpp.

125{
126 pcbnewRunPythonMethodWithReturnedString( "pcbnew.GetUnLoadableWizards", aNames );
127}

References pcbnewRunPythonMethodWithReturnedString().

Referenced by DIALOG_FOOTPRINT_WIZARD_LIST::initLists().

◆ pcbnewGetWizardsBackTrace()

void pcbnewGetWizardsBackTrace ( wxString &  aNames)

Return the backtrace of errors (if any) when wizard python scripts are loaded.

Parameters
aNamesis a wxString which will contain the trace

Definition at line 136 of file pcbnew_scripting.cpp.

137{
138 pcbnewRunPythonMethodWithReturnedString( "pcbnew.GetWizardsBackTrace", aTrace );
139
140 // Filter message before displaying them
141 // a trace starts by "Traceback" and is followed by 2 useless lines
142 // for our purpose
143 wxArrayString traces;
144 wxStringSplit( aTrace, traces, '\n' );
145
146 // Build the filtered message (remove useless lines)
147 aTrace.Clear();
148
149 for( unsigned ii = 0; ii < traces.Count(); ++ii )
150 {
151 if( traces[ii].Contains( wxT( "Traceback" ) ) )
152 {
153 ii += 2; // Skip this line and next lines which are related to pcbnew.py module
154
155 if( !aTrace.IsEmpty() ) // Add separator for the next trace block
156 aTrace << wxT( "\n**********************************\n" );
157 }
158 else
159 {
160 aTrace += traces[ii] + wxT( "\n" );
161 }
162 }
163}
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.

References pcbnewRunPythonMethodWithReturnedString(), and wxStringSplit().

Referenced by PANEL_PCBNEW_ACTION_PLUGINS::OnShowErrorsButtonClick(), DIALOG_FOOTPRINT_WIZARD_LIST::onShowTrace(), and PANEL_PCBNEW_ACTION_PLUGINS::TransferDataToWindow().

◆ pcbnewRunPythonMethodWithReturnedString()

static void pcbnewRunPythonMethodWithReturnedString ( const char *  aMethodName,
wxString &  aNames 
)
static

Run a python method from the pcbnew module.

Parameters
aMethodNameis the name of the method (like "pcbnew.myfunction" ).
aNameswill contain the returned string.

Definition at line 58 of file pcbnew_scripting.cpp.

59{
60 aNames.Clear();
61
62 PyLOCK lock;
63 PyErr_Clear();
64
65 PyObject* builtins = PyImport_ImportModule( "pcbnew" );
66 wxASSERT( builtins );
67
68 if( !builtins ) // Something is wrong in pcbnew.py module (incorrect version?)
69 return;
70
71 PyObject* globals = PyDict_New();
72 PyDict_SetItemString( globals, "pcbnew", builtins );
73 Py_DECREF( builtins );
74
75 // Build the python code
76 char cmd[1024];
77 snprintf( cmd, sizeof(cmd), "result = %s()", aMethodName );
78
79 // Execute the python code and get the returned data
80 PyObject* localDict = PyDict_New();
81 PyObject* pobj = PyRun_String( cmd, Py_file_input, globals, localDict);
82 Py_DECREF( globals );
83
84 if( pobj )
85 {
86 PyObject* str = PyDict_GetItemString(localDict, "result" );
87 const char* str_res = nullptr;
88
89 if(str)
90 {
91 PyObject* temp_bytes = PyUnicode_AsEncodedString( str, "UTF-8", "strict" );
92
93 if( temp_bytes != nullptr )
94 {
95 str_res = PyBytes_AS_STRING( temp_bytes );
96 aNames = FROM_UTF8( str_res );
97 Py_DECREF( temp_bytes );
98 }
99 else
100 {
101 wxLogMessage( "cannot encode Unicode python string" );
102 }
103 }
104 else
105 {
106 aNames = wxString();
107 }
108
109 Py_DECREF( pobj );
110 }
111
112 Py_DECREF( localDict );
113
114 if( PyErr_Occurred() )
115 {
116 if( strcmp( aMethodName, "pcbnew.GetWizardsBackTrace" ) == 0 )
117 aNames = PyErrStringWithTraceback();
118 else
119 wxLogMessage( PyErrStringWithTraceback() );
120 }
121}
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110

References FROM_UTF8().

Referenced by pcbnewGetScriptsSearchPaths(), pcbnewGetUnloadableScriptNames(), and pcbnewGetWizardsBackTrace().