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, you may find one here:
20 * https://www.gnu.org/licenses/gpl-3.0.html
21 * or you may search the http://www.gnu.org website for the version 3 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include "bom_plugins.h"
27#include <config.h>
28#include <paths.h>
29#include <wx/ffile.h>
30#include <wx/log.h>
31
32
33const wxChar BOM_TRACE[] = wxT( "BOM_GENERATORS" );
34
35
37 : m_storedPath( aFile )
38{
39 m_isOk = false;
40 m_file = wxFileName( aFile );
41
42 if( !wxFile::Exists( m_file.GetFullPath() ) )
44
45 if( !wxFile::Exists( m_file.GetFullPath() ) )
46 {
47 m_info.Printf( _("Script file:\n%s\nnot found. Script not available."), aFile );
48 return;
49 }
50
51 m_isOk = true;
52
53 m_name = m_file.GetName();
54 wxString extension = m_file.GetExt().Lower();
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#ifdef __WINDOWS__
72 m_cmd = wxString::Format( "python \"%s/%s\" \"%%I\" \"%%O%s\"",
73 m_file.GetPath(),
74 m_file.GetFullName(),
76#else
77 wxString interpreter = wxString::FromUTF8Unchecked( PYTHON_EXECUTABLE );
78
79 if( interpreter.IsEmpty() )
80 interpreter = wxT( "python" );
81
82 m_cmd = wxString::Format( "%s \"%s\" \"%%I\" \"%%O%s\"",
83 interpreter,
84 m_file.GetFullPath(),
86#endif
87 }
88#ifdef __WINDOWS__
89 else if( extension == wxS( "pyw" ) )
90 {
91 m_info = readHeader( wxS( "\"\"\"" ) );
92 m_cmd = wxString::Format( wxS( "pythonw \"%s/%s\" \"%%I\" \"%%O%s\"" ),
93 m_file.GetPath(),
94 m_file.GetFullName(),
96 }
97#endif /* __WINDOWS__ */
98 else // fallback
99 {
100 m_cmd = m_file.GetFullPath();
101 }
102
103 wxLogTrace( BOM_TRACE, wxS( "%s: extracted command line %s" ), m_name, m_cmd );
104}
105
106
107bool BOM_GENERATOR_HANDLER::IsValidGenerator( const wxString& aFile )
108{
109 wxFileName fn( aFile );
110 wxString ext = fn.GetExt().Lower();
111
112 for( const auto& pluginExt : { wxS( "xsl" ), wxS( "py" ), wxS( "pyw" ) } )
113 {
114 if( pluginExt == ext )
115 return true;
116 }
117
118 return false;
119}
120
121
122wxString BOM_GENERATOR_HANDLER::readHeader( const wxString& aEndSection )
123{
124 if( aEndSection.IsEmpty() )
125 return wxEmptyString;
126
127 wxFFile fdata( m_file.GetFullPath(), wxS( "rb" ) ); // dtor will close the file
128 wxString data;
129
130 if( !fdata.ReadAll( &data ) )
131 return wxEmptyString;
132
133 const wxString header( wxS( "@package" ) );
134
135 // Extract substring between @package and endsection
136 size_t strstart = data.find( header );
137
138 if( strstart == wxString::npos )
139 return wxEmptyString;
140
141 strstart += header.Length();
142 size_t strend = data.find( aEndSection, strstart );
143
144 if( strend == wxString::npos )
145 return wxEmptyString;
146
147 // Remove empty line if any
148 while( data[strstart] < ' ' )
149 strstart++;
150
151 return data.SubString( strstart, strend - 1 );
152}
153
154
155wxString BOM_GENERATOR_HANDLER::getOutputExtension( const wxString& aHeader )
156{
157 // search header for extension after %O (extension includes '.')
158 // looks for output argument of the form `"%O.extension"`
159 const wxString outputarg( wxS( "\"%O" ) );
160
161 size_t strstart = aHeader.find( outputarg );
162
163 if( strstart == wxString::npos )
164 return wxEmptyString;
165
166 strstart += outputarg.Length();
167 size_t strend = aHeader.find( wxS( "\"" ), strstart );
168
169 if( strend == wxString::npos )
170 return wxEmptyString;
171
172 return aHeader.SubString( strstart, strend - 1 );
173}
174
175
177{
178 if( m_file.IsAbsolute() && m_file.Exists( wxFILE_EXISTS_REGULAR ) )
179 {
180 wxLogTrace( BOM_TRACE, wxS( "%s found directly" ), m_file.GetFullPath() );
181 return m_file;
182 }
183
184 wxFileName test( PATHS::GetUserPluginsPath(), m_file.GetName(), m_file.GetExt() );
185
186 if( test.Exists( wxFILE_EXISTS_REGULAR ) )
187 {
188 wxLogTrace( BOM_TRACE, wxS( "%s found in user plugins path %s" ), m_file.GetFullName(),
190 return test;
191 }
192
193 test = wxFileName( PATHS::GetStockPluginsPath(), m_file.GetName(), m_file.GetExt() );
194
195 if( test.Exists( wxFILE_EXISTS_REGULAR ) )
196 {
197 wxLogTrace( BOM_TRACE, wxS( "%s found in stock plugins path %s" ), m_file.GetFullName(),
199 return test;
200 }
201
202 wxLogTrace( BOM_TRACE, wxS( "Could not find %s (checked %s, %s)" ), m_file.GetFullName(),
204
205 return m_file;
206}
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
#define _(s)
std::vector< std::string > header