Unify scripting dirs and open user-based

This consolidates python scripting in two locations: the system-wide
package-based location and the user configuration location.  It also
updates the "Show Scripting Folder" action to open the user
configuration location instead of the package-based one.

Fixes https://gitlab.com/kicad/code/kicad/issues/5652

Fixes https://gitlab.com/kicad/code/kicad/issues/5115
This commit is contained in:
Seth Hillbrand 2020-09-16 20:06:57 -07:00
parent f3586ccd45
commit 45ca1709bb
5 changed files with 31 additions and 29 deletions

View File

@ -1475,11 +1475,11 @@ void PCB_EDIT_FRAME::PythonPluginsShowFolder()
wxString msg; wxString msg;
// Quote in case there are spaces in the path. // Quote in case there are spaces in the path.
msg.Printf( "open \"%s\"", PyPluginsPath() ); msg.Printf( "open \"%s\"", PyPluginsPath( true ) );
system( msg.c_str() ); system( msg.c_str() );
#else #else
wxString pypath( PyPluginsPath() ); wxString pypath( PyPluginsPath( true ) );
// Quote in case there are spaces in the path. // Quote in case there are spaces in the path.
AddDelimiterString( pypath ); AddDelimiterString( pypath );

View File

@ -41,6 +41,7 @@
#include <pcb_edit_frame.h> #include <pcb_edit_frame.h>
#include <eda_dde.h> #include <eda_dde.h>
#include <wx/file.h> #include <wx/file.h>
#include <wx/log.h>
#include <wx/snglinst.h> #include <wx/snglinst.h>
#include <gestfich.h> #include <gestfich.h>
#include <pcbnew.h> #include <pcbnew.h>
@ -280,6 +281,16 @@ static bool scriptingSetup()
#endif #endif
wxFileName path( PyPluginsPath( true ) + wxT("/") );
// Ensure the user plugin path exists, and create it if not.
if( !path.DirExists() && !path.Mkdir() )
{
wxLogDebug( "Warning: could not create user scripting path %s",
path.GetPath() );
return false;
}
if( !pcbnewInitPythonScripting( TO_UTF8( PyScriptingPath() ) ) ) if( !pcbnewInitPythonScripting( TO_UTF8( PyScriptingPath() ) ) )
{ {
wxLogError( "pcbnewInitPythonScripting() failed." ); wxLogError( "pcbnewInitPythonScripting() failed." );

View File

@ -40,6 +40,7 @@
#include <trace_helpers.h> #include <trace_helpers.h>
#include <pgm_base.h> #include <pgm_base.h>
#include <settings/settings_manager.h>
/* init functions defined by swig */ /* init functions defined by swig */
@ -207,7 +208,7 @@ bool pcbnewInitPythonScripting( const char * aUserScriptingPath )
#endif // ifdef KICAD_SCRIPTING_WXPYTHON #endif // ifdef KICAD_SCRIPTING_WXPYTHON
// Load pcbnew inside Python and load all the user plugins, TODO: add system wide plugins // Load pcbnew inside Python and load all the user plugins and package-based plugins
{ {
PyLOCK lock; PyLOCK lock;
@ -616,16 +617,23 @@ wxString PyErrStringWithTraceback()
/** /**
* Find the Python scripting path. * Find the Python scripting path.
*/ */
wxString PyScriptingPath() wxString PyScriptingPath( bool aUserPath )
{ {
wxString path; wxString path;
//@todo This should this be a user configurable variable eg KISCRIPT? //@todo This should this be a user configurable variable eg KISCRIPT?
if( aUserPath)
{
path = SETTINGS_MANAGER::GetUserSettingsPath() + wxT( "/scripting" );
}
else
{
#if defined( __WXMAC__ ) #if defined( __WXMAC__ )
path = GetOSXKicadDataDir() + wxT( "/scripting" ); path = GetOSXKicadDataDir() + wxT( "/scripting" );
#else #else
path = Pgm().GetExecutablePath() + wxT( "../share/kicad/scripting" ); path = Pgm().GetExecutablePath() + wxT( "../share/kicad/scripting" );
#endif #endif
}
wxFileName scriptPath( path ); wxFileName scriptPath( path );
scriptPath.MakeAbsolute(); scriptPath.MakeAbsolute();
@ -640,9 +648,9 @@ wxString PyScriptingPath()
} }
wxString PyPluginsPath() wxString PyPluginsPath( bool aUserPath )
{ {
// Note we are using unix path separator, because window separator sometimes // 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 // creates issues when passing a command string to a python method by PyRun_SimpleString
return PyScriptingPath() + '/' + "plugins"; return PyScriptingPath( aUserPath ) + '/' + "plugins";
} }

View File

@ -121,7 +121,7 @@ wxString PyStringToWx( PyObject* str );
wxArrayString PyArrayStringToWx( PyObject* arr ); wxArrayString PyArrayStringToWx( PyObject* arr );
wxString PyErrStringWithTraceback(); wxString PyErrStringWithTraceback();
wxString PyScriptingPath(); wxString PyScriptingPath( bool aUserPath = false );
wxString PyPluginsPath(); wxString PyPluginsPath( bool aUserPath = false );
#endif // __PYTHON_SCRIPTING_H #endif // __PYTHON_SCRIPTING_H

View File

@ -159,18 +159,10 @@ def LoadPlugins(bundlepath=None):
<bundlepath>/ <bundlepath>/
<bundlepath>/plugins/ <bundlepath>/plugins/
The Scripts relative to the KiCad search path environment variable:
[KICAD_PATH]/scripting/
[KICAD_PATH]/scripting/plugins/
The Scripts relative to the KiCad Users configuration: The Scripts relative to the KiCad Users configuration:
<kicad_config_path>/scripting/ <userpath>/
<kicad_config_path>/scripting/plugins/ <userpath>/plugins/
And on Linux ONLY, extra paths relative to the users home directory:
~/.kicad_plugins/
~/.kicad/scripting/
~/.kicad/scripting/plugins/
""" """
import os import os
import sys import sys
@ -181,7 +173,6 @@ def LoadPlugins(bundlepath=None):
import importlib import importlib
importlib.invalidate_caches() importlib.invalidate_caches()
kicad_path = os.environ.get('KICAD_PATH')
config_path = pcbnew.SETTINGS_MANAGER.GetUserSettingsPath() config_path = pcbnew.SETTINGS_MANAGER.GetUserSettingsPath()
plugin_directories=[] plugin_directories=[]
@ -196,18 +187,10 @@ def LoadPlugins(bundlepath=None):
plugin_directories.append(bundlepath) plugin_directories.append(bundlepath)
plugin_directories.append(os.path.join(bundlepath, 'plugins')) plugin_directories.append(os.path.join(bundlepath, 'plugins'))
if kicad_path:
plugin_directories.append(os.path.join(kicad_path, 'scripting'))
plugin_directories.append(os.path.join(kicad_path, 'scripting', 'plugins'))
if config_path: if config_path:
plugin_directories.append(os.path.join(config_path, 'scripting')) plugin_directories.append(os.path.join(config_path, 'scripting'))
plugin_directories.append(os.path.join(config_path, 'scripting', 'plugins')) plugin_directories.append(os.path.join(config_path, 'scripting', 'plugins'))
if sys.platform.startswith('linux'):
plugin_directories.append(os.path.join(os.environ['HOME'],'.kicad_plugins'))
plugin_directories.append(os.path.join(os.environ['HOME'],'.kicad','scripting'))
plugin_directories.append(os.path.join(os.environ['HOME'],'.kicad','scripting','plugins'))
global PLUGIN_DIRECTORIES_SEARCH global PLUGIN_DIRECTORIES_SEARCH
PLUGIN_DIRECTORIES_SEARCH="" PLUGIN_DIRECTORIES_SEARCH=""