KiCad PCB EDA Suite
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  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * https://www.gnu.org/licenses/gpl-3.0.html
20  * or you may search the http://www.gnu.org website for the version 3 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include "bom_plugins.h"
26 
28  : m_file( aFile )
29 {
30  m_isOk = false;
31 
32  if( !wxFile::Exists( aFile ) )
33  {
34  m_info.Printf( _("Script file:\n%s\nnot found. Script not available."), aFile );
35  return;
36  }
37 
38  m_isOk = true;
39 
40  m_name = m_file.GetName();
41  wxString extension = m_file.GetExt().Lower();
42 
43  // Important note:
44  // On Windows the right command command to run a python script is:
45  // python <script_path>/script.py
46  // and *not* python <script_path>\script.py
47  // Otherwise the script does not find some auxiliary pythons scripts needed by this script
48  if( extension == "xsl" )
49  {
50  m_info = readHeader( "-->" );
51  m_cmd = wxString::Format( "xsltproc -o \"%%O\" \"%s\" \"%%I\"", m_file.GetFullPath() );
52  }
53  else if( extension == "py" )
54  {
55  m_info = readHeader( "\"\"\"" );
56 #ifdef __WINDOWS__
57  m_cmd = wxString::Format( "python \"%s/%s\" \"%%I\" \"%%O\"",
58  m_file.GetPath(), m_file.GetFullName() );
59 #else
60  m_cmd = wxString::Format( "python \"%s\" \"%%I\" \"%%O\"", m_file.GetFullPath() );
61 #endif
62  }
63 #ifdef __WINDOWS__
64  else if( extension == "pyw" )
65  {
66  m_info = readHeader( "\"\"\"" );
67  m_cmd = wxString::Format( "pythonw \"%s/%s\" \"%%I\" \"%%O\"",
68  m_file.GetPath(),m_file.GetFullName() );
69  }
70 #endif /* __WINDOWS__ */
71  else // fallback
72  {
73  m_cmd = m_file.GetFullPath();
74  }
75 }
76 
77 
78 bool BOM_GENERATOR_HANDLER::IsValidGenerator( const wxString& aFile )
79 {
80  wxFileName fn( aFile );
81  wxString ext = fn.GetExt().Lower();
82 
83  for( const auto& pluginExt : { "xsl", "py", "pyw" } )
84  {
85  if( pluginExt == ext )
86  return true;
87  }
88 
89  return false;
90 }
91 
92 
93 wxString BOM_GENERATOR_HANDLER::readHeader( const wxString& aEndSection )
94 {
95  if( aEndSection.IsEmpty() )
96  return wxEmptyString;
97 
98  wxFile fdata( m_file.GetFullPath() ); // dtor will close the file
99  wxString data;
100 
101  if( !fdata.ReadAll( &data ) )
102  return wxEmptyString;
103 
104  const wxString header( "@package" );
105 
106  // Extract substring between @package and endsection
107  int strstart = data.Find( header );
108 
109  if( strstart == wxNOT_FOUND )
110  return wxEmptyString;
111 
112  strstart += header.Length();
113  int strend = data.find( aEndSection, strstart );
114 
115  if( strend == wxNOT_FOUND)
116  return wxEmptyString;
117 
118  // Remove empty line if any
119  while( data[strstart] < ' ' )
120  strstart++;
121 
122  return data.SubString( strstart, strend - 1 );
123 }
wxString readHeader(const wxString &aEndSection)
Reads the plugin file header.
Definition: bom_plugins.cpp:93
wxString m_cmd
Command to execute the plugin
Definition: bom_plugins.h:138
static bool IsValidGenerator(const wxString &aFile)
Returns true if a file name matches a recognized plugin format.
Definition: bom_plugins.cpp:78
#define _(s)
BOM_GENERATOR_HANDLER(const wxString &aFile)
Constructor.
Definition: bom_plugins.cpp:27
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
const wxFileName m_file
Path to the plugin
Definition: bom_plugins.h:132
wxString m_name
User customisable name
Definition: bom_plugins.h:135
wxString m_info
Description of the plugin (normally from the plugin header)
Definition: bom_plugins.h:141
bool m_isOk
true if the plugin is working (i.e. if the plugin file exists and was read
Definition: bom_plugins.h:129