KiCad PCB EDA Suite
Loading...
Searching...
No Matches
bom_plugins.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2018 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 3
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#include "bom_plugins.h"
23#include <config.h>
24#include <paths.h>
25#include <pgm_base.h>
27#include <wx/ffile.h>
28#include <wx/log.h>
29
30
31const wxChar BOM_TRACE[] = wxT( "BOM_GENERATORS" );
32
33
35 : m_storedPath( aFile )
36{
37 m_isOk = false;
38 m_file = wxFileName( aFile );
39
40 if( !wxFile::Exists( m_file.GetFullPath() ) )
42
43 if( !wxFile::Exists( m_file.GetFullPath() ) )
44 {
45 m_info.Printf( _("Script file:\n%s\nnot found. Script not available."), aFile );
46 return;
47 }
48
49 m_isOk = true;
50
51 m_name = m_file.GetName();
52 wxString extension = m_file.GetExt().Lower();
53
54 wxString python = Pgm().GetCommonSettings()->m_Api.python_interpreter;
55
56 // Important note:
57 // On Windows the right command command to run a python script is:
58 // python <script_path>/script.py
59 // and *not* python <script_path>\script.py
60 // Otherwise the script does not find some auxiliary pythons scripts needed by this script
61 if( extension == wxS( "xsl" ) )
62 {
63 m_info = readHeader( wxS( "-->" ) );
64 m_cmd = wxString::Format( wxS( "xsltproc -o \"%%O%s\" \"%s\" \"%%I\"" ),
66 m_file.GetFullPath() );
67 }
68 else if( extension == wxS( "py" ) )
69 {
70 m_info = readHeader( wxS( "\"\"\"" ) );
71
72#ifdef __WINDOWS__
73 m_cmd = wxString::Format( "\"%s\" \"%s/%s\" \"%%I\" \"%%O%s\"",
74 python,
75 m_file.GetPath(),
76 m_file.GetFullName(),
78#else
79 wxString interpreter = wxString::FromUTF8Unchecked( python );
80
81 if( interpreter.IsEmpty() )
82 interpreter = wxT( "python" );
83
84 m_cmd = wxString::Format( "%s \"%s\" \"%%I\" \"%%O%s\"",
85 interpreter,
86 m_file.GetFullPath(),
88#endif
89 }
90#ifdef __WINDOWS__
91 else if( extension == wxS( "pyw" ) )
92 {
93 python.Replace( "python.exe", "pythonw.exe" );
94 m_info = readHeader( wxS( "\"\"\"" ) );
95 m_cmd = wxString::Format( wxS( "\"%s\" \"%s/%s\" \"%%I\" \"%%O%s\"" ),
96 python,
97 m_file.GetPath(),
98 m_file.GetFullName(),
100 }
101#endif /* __WINDOWS__ */
102 else // fallback
103 {
104 m_cmd = m_file.GetFullPath();
105 }
106
107 wxLogTrace( BOM_TRACE, wxS( "%s: extracted command line %s" ), m_name, m_cmd );
108}
109
110
111bool BOM_GENERATOR_HANDLER::IsValidGenerator( const wxString& aFile )
112{
113 wxFileName fn( aFile );
114 wxString ext = fn.GetExt().Lower();
115
116 for( const auto& pluginExt : { wxS( "xsl" ), wxS( "py" ), wxS( "pyw" ) } )
117 {
118 if( pluginExt == ext )
119 return true;
120 }
121
122 return false;
123}
124
125
126wxString BOM_GENERATOR_HANDLER::readHeader( const wxString& aEndSection )
127{
128 if( aEndSection.IsEmpty() )
129 return wxEmptyString;
130
131 wxFFile fdata( m_file.GetFullPath(), wxS( "rb" ) ); // dtor will close the file
132 wxString data;
133
134 if( !fdata.ReadAll( &data ) )
135 return wxEmptyString;
136
137 const wxString header( wxS( "@package" ) );
138
139 // Extract substring between @package and endsection
140 size_t strstart = data.find( header );
141
142 if( strstart == wxString::npos )
143 return wxEmptyString;
144
145 strstart += header.Length();
146 size_t strend = data.find( aEndSection, strstart );
147
148 if( strend == wxString::npos )
149 return wxEmptyString;
150
151 // Remove empty line if any
152 while( data[strstart] < ' ' )
153 strstart++;
154
155 return data.SubString( strstart, strend - 1 );
156}
157
158
159wxString BOM_GENERATOR_HANDLER::getOutputExtension( const wxString& aHeader )
160{
161 // search header for extension after %O (extension includes '.')
162 // looks for output argument of the form `"%O.extension"`
163 const wxString outputarg( wxS( "\"%O" ) );
164
165 size_t strstart = aHeader.find( outputarg );
166
167 if( strstart == wxString::npos )
168 return wxEmptyString;
169
170 strstart += outputarg.Length();
171 size_t strend = aHeader.find( wxS( "\"" ), strstart );
172
173 if( strend == wxString::npos )
174 return wxEmptyString;
175
176 return aHeader.SubString( strstart, strend - 1 );
177}
178
179
181{
182 if( m_file.IsAbsolute() && m_file.Exists( wxFILE_EXISTS_REGULAR ) )
183 {
184 wxLogTrace( BOM_TRACE, wxS( "%s found directly" ), m_file.GetFullPath() );
185 return m_file;
186 }
187
188 wxFileName test( PATHS::GetUserPluginsPath(), m_file.GetName(), m_file.GetExt() );
189
190 if( test.Exists( wxFILE_EXISTS_REGULAR ) )
191 {
192 wxLogTrace( BOM_TRACE, wxS( "%s found in user plugins path %s" ), m_file.GetFullName(),
194 return test;
195 }
196
197 test = wxFileName( PATHS::GetStockPluginsPath(), m_file.GetName(), m_file.GetExt() );
198
199 if( test.Exists( wxFILE_EXISTS_REGULAR ) )
200 {
201 wxLogTrace( BOM_TRACE, wxS( "%s found in stock plugins path %s" ), m_file.GetFullName(),
203 return test;
204 }
205
206 wxLogTrace( BOM_TRACE, wxS( "Could not find %s (checked %s, %s)" ), m_file.GetFullName(),
208
209 return m_file;
210}
const wxChar BOM_TRACE[]
const wxChar BOM_TRACE[]
wxString m_name
Command to execute the plugin.
static bool IsValidGenerator(const wxString &aFile)
Return true if a file name matches a recognized plugin format.
wxString readHeader(const wxString &aEndSection)
Read the plugin file header.
bool m_isOk
Path to the plugin.
BOM_GENERATOR_HANDLER(const wxString &aFile)
wxString m_info
Plugin specific options.
wxFileName m_file
Path to the plugin stored in config (can be absolute or just a filename)
const wxString m_storedPath
User customisable name.
wxString m_cmd
Description of the plugin (normally from the plugin header)
wxFileName FindFilePath() const
Returns the calculated path to the plugin: if the path is already absolute and exists,...
static wxString getOutputExtension(const wxString &aHeader)
Extracts the output BOM file's extension, including the '.
static wxString GetUserPluginsPath()
Gets the user path for plugins.
Definition paths.cpp:49
static wxString GetStockPluginsPath()
Gets the stock (install) plugins path.
Definition paths.cpp:377
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:546
#define _(s)
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE