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 125 of file pcbnew_scripting.cpp.

126 {
127  pcbnewRunPythonMethodWithReturnedString( "pcbnew.GetWizardsSearchPaths", aNames );
128 }
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 119 of file pcbnew_scripting.cpp.

120 {
121  pcbnewRunPythonMethodWithReturnedString( "pcbnew.GetUnLoadableWizards", aNames );
122 }
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().

◆ 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 131 of file pcbnew_scripting.cpp.

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

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  wxLogMessage( PyErrStringWithTraceback() );
116 }
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().