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 
27 BOM_PLUGIN::BOM_PLUGIN( const wxString& aFile )
28  : m_file( aFile )
29 {
30  if( !wxFile::Exists( aFile ) )
31  throw std::runtime_error( ( _( "Cannot open plugin " ) + aFile ).ToStdString() );
32 
33  m_name = m_file.GetName();
34  wxString extension = m_file.GetExt().Lower();
35 
36  // Important note:
37  // On Windows the right command command to run a python script is:
38  // python <script_path>/script.py
39  // and *not* python <script_path>\script.py
40  // Otherwise the script does not find some auxiliary pythons scripts needed by this script
41  if( extension == "xsl" )
42  {
43  m_info = readHeader( "-->" );
44  m_cmd = wxString::Format( "xsltproc -o \"%%O\" \"%s\" \"%%I\"", m_file.GetFullPath() );
45  }
46  else if( extension == "py" )
47  {
48  m_info = readHeader( "\"\"\"" );
49 #ifdef __WINDOWS__
50  m_cmd = wxString::Format( "python \"%s/%s\" \"%%I\" \"%%O\"",
51  m_file.GetPath(), m_file.GetFullName() );
52 #else
53  m_cmd = wxString::Format( "python \"%s\" \"%%I\" \"%%O\"", m_file.GetFullPath() );
54 #endif
55  }
56 #ifdef __WINDOWS__
57  else if( extension == "pyw" )
58  {
59  m_info = readHeader( "\"\"\"" );
60  m_cmd = wxString::Format( "pythonw \"%s/%s\" \"%%I\" \"%%O\"",
61  m_file.GetPath(),m_file.GetFullName() );
62  }
63 #endif /* __WINDOWS__ */
64  else // fallback
65  {
66  m_cmd = m_file.GetFullPath();
67  }
68 }
69 
70 
71 bool BOM_PLUGIN::IsPlugin( const wxString& aFile )
72 {
73  wxFileName fn( aFile );
74  wxString ext = fn.GetExt().Lower();
75 
76  for( const auto& pluginExt : { "xsl", "py", "pyw" } )
77  {
78  if( pluginExt == ext )
79  return true;
80  }
81 
82  return false;
83 }
84 
85 
86 wxString BOM_PLUGIN::readHeader( const wxString& aEndSection )
87 {
88  if( aEndSection.IsEmpty() )
89  return wxEmptyString;
90 
91  wxFile fdata( m_file.GetFullPath() ); // dtor will close the file
92  wxString data;
93 
94  if( !fdata.ReadAll( &data ) )
95  return wxEmptyString;
96 
97  const wxString header( "@package" );
98 
99  // Extract substring between @package and endsection
100  int strstart = data.Find( header );
101 
102  if( strstart == wxNOT_FOUND )
103  return wxEmptyString;
104 
105  strstart += header.Length();
106  int strend = data.find( aEndSection, strstart );
107 
108  if( strend == wxNOT_FOUND)
109  return wxEmptyString;
110 
111  // Remove empty line if any
112  while( data[strstart] < ' ' )
113  strstart++;
114 
115  return data.SubString( strstart, strend - 1 );
116 }
wxString m_info
Description of the plugin (normally from the plugin header)
Definition: bom_plugins.h:128
wxString readHeader(const wxString &aEndSection)
Reads the plugin file header.
Definition: bom_plugins.cpp:86
wxString m_cmd
Command to execute the plugin
Definition: bom_plugins.h:125
const wxFileName m_file
Path to the plugin
Definition: bom_plugins.h:119
wxString m_name
User customisable name
Definition: bom_plugins.h:122
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
static bool IsPlugin(const wxString &aFile)
Returns true if a file name matches a recognized plugin format.
Definition: bom_plugins.cpp:71
BOM_PLUGIN(const wxString &aFile)
Constructor.
Definition: bom_plugins.cpp:27