KiCad PCB EDA Suite
BOM_GENERATOR_HANDLER Class Reference

Bill of material output generator. More...

#include <bom_plugins.h>

Public Types

typedef std::unique_ptr< BOM_GENERATOR_HANDLERPTR
 

Public Member Functions

 BOM_GENERATOR_HANDLER (const wxString &aFile)
 
bool IsOk ()
 Return true if the plugin is ready to work, i.e. More...
 
const wxString & GetInfo () const
 Return plugin description stored in the plugin header file (if available). More...
 
const wxFileName & GetFile () const
 Return the file name of the plugin. More...
 
wxString GetStoredPath () const
 
wxFileName FindFilePath () const
 Returns the calculated path to the plugin: if the path is already absolute and exists, just return it. More...
 
const wxString & GetName () const
 Return the customisable plugin name. More...
 
void SetName (const wxString &aName)
 Set the customisable plugin name. More...
 
const wxString & GetCommand () const
 Return the command to execute the plugin. More...
 
void SetCommand (const wxString &aCommand)
 Set the command to execute the plugin. More...
 
wxArrayString & Options ()
 Accessor to array of options. More...
 

Static Public Member Functions

static bool IsValidGenerator (const wxString &aFile)
 Return true if a file name matches a recognized plugin format. More...
 

Protected Member Functions

wxString readHeader (const wxString &aEndSection)
 Read the plugin file header. More...
 

Static Protected Member Functions

static wxString getOutputExtension (const wxString &aHeader)
 Extracts the output BOM file's extension, including the '. More...
 

Protected Attributes

bool m_isOk
 Path to the plugin. More...
 
wxFileName m_file
 Path to the plugin stored in config (can be absolute or just a filename) More...
 
const wxString m_storedPath
 User customisable name. More...
 
wxString m_name
 Command to execute the plugin. More...
 
wxString m_cmd
 Description of the plugin (normally from the plugin header) More...
 
wxString m_info
 Plugin specific options. More...
 
wxArrayString m_options
 

Detailed Description

Bill of material output generator.

A Material output generator is an external application called by Eeschema to create a BOM from our intermediate XML netlist. A generator can be a script or an executable that can read the intermediate XML netlist file and generates a output (the BOM file)

Definition at line 45 of file bom_plugins.h.

Member Typedef Documentation

◆ PTR

Definition at line 48 of file bom_plugins.h.

Constructor & Destructor Documentation

◆ BOM_GENERATOR_HANDLER()

BOM_GENERATOR_HANDLER::BOM_GENERATOR_HANDLER ( const wxString &  aFile)
Parameters
aFileis path to the plugin file.

Definition at line 34 of file bom_plugins.cpp.

35  : m_storedPath( aFile )
36 {
37  m_isOk = false;
38  m_file = wxFileName( aFile );
39 
40  if( !wxFile::Exists( m_file.GetFullPath() ) )
41  m_file = FindFilePath();
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  // Important note:
55  // On Windows the right command command to run a python script is:
56  // python <script_path>/script.py
57  // and *not* python <script_path>\script.py
58  // Otherwise the script does not find some auxiliary pythons scripts needed by this script
59  if( extension == "xsl" )
60  {
61  m_info = readHeader( "-->" );
62  m_cmd = wxString::Format( "xsltproc -o \"%%O%s\" \"%s\" \"%%I\"",
63  getOutputExtension( m_info ), m_file.GetFullPath() );
64  }
65  else if( extension == "py" )
66  {
67  m_info = readHeader( "\"\"\"" );
68 #ifdef __WINDOWS__
69  m_cmd = wxString::Format( "python \"%s/%s\" \"%%I\" \"%%O%s\"",
70  m_file.GetPath(), m_file.GetFullName(),
72 #else
73  m_cmd = wxString::Format( "python \"%s\" \"%%I\" \"%%O%s\"", m_file.GetFullPath(),
75 #endif
76  }
77 #ifdef __WINDOWS__
78  else if( extension == "pyw" )
79  {
80  m_info = readHeader( "\"\"\"" );
81  m_cmd = wxString::Format( "pythonw \"%s/%s\" \"%%I\" \"%%O%s\"",
82  m_file.GetPath(), m_file.GetFullName(),
84  }
85 #endif /* __WINDOWS__ */
86  else // fallback
87  {
88  m_cmd = m_file.GetFullPath();
89  }
90 
91  wxLogTrace( BOM_TRACE, "%s: extracted command line %s", m_name, m_cmd );
92 }
wxString readHeader(const wxString &aEndSection)
Read the plugin file header.
wxFileName m_file
Path to the plugin stored in config (can be absolute or just a filename)
Definition: bom_plugins.h:157
wxString m_cmd
Description of the plugin (normally from the plugin header)
Definition: bom_plugins.h:166
const wxChar BOM_TRACE[]
Definition: bom_plugins.cpp:31
wxFileName FindFilePath() const
Returns the calculated path to the plugin: if the path is already absolute and exists,...
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
static wxString getOutputExtension(const wxString &aHeader)
Extracts the output BOM file's extension, including the '.
#define _(s)
Definition: 3d_actions.cpp:33
wxString m_name
Command to execute the plugin.
Definition: bom_plugins.h:163
const wxString m_storedPath
User customisable name.
Definition: bom_plugins.h:160
wxString m_info
Plugin specific options.
Definition: bom_plugins.h:169
bool m_isOk
Path to the plugin.
Definition: bom_plugins.h:154

References _, BOM_TRACE, FindFilePath(), Format(), getOutputExtension(), m_cmd, m_file, m_info, m_isOk, m_name, and readHeader().

Member Function Documentation

◆ FindFilePath()

wxFileName BOM_GENERATOR_HANDLER::FindFilePath ( ) const

Returns the calculated path to the plugin: if the path is already absolute and exists, just return it.

Otherwise if the path is just a filename, look for that file in the user and system plugin directories and return the first one found. If neither is found, just return m_file.

Returns
the full path to the plugin

Definition at line 164 of file bom_plugins.cpp.

165 {
166  if( m_file.IsAbsolute() && m_file.Exists( wxFILE_EXISTS_REGULAR ) )
167  {
168  wxLogTrace( BOM_TRACE, "%s found directly", m_file.GetFullPath() );
169  return m_file;
170  }
171 
172  wxFileName test( PATHS::GetUserPluginsPath(), m_file.GetName(), m_file.GetExt() );
173 
174  if( test.Exists( wxFILE_EXISTS_REGULAR ) )
175  {
176  wxLogTrace( BOM_TRACE, "%s found in user plugins path %s", m_file.GetFullName(),
178  return test;
179  }
180 
181  test = wxFileName( PATHS::GetStockPluginsPath(), m_file.GetName(), m_file.GetExt() );
182 
183  if( test.Exists( wxFILE_EXISTS_REGULAR ) )
184  {
185  wxLogTrace( BOM_TRACE, "%s found in stock plugins path %s", m_file.GetFullName(),
187  return test;
188  }
189 
190  wxLogTrace( BOM_TRACE, "Could not find %s (checked %s, %s)", m_file.GetFullName(),
192 
193  return m_file;
194 }
wxFileName m_file
Path to the plugin stored in config (can be absolute or just a filename)
Definition: bom_plugins.h:157
const wxChar BOM_TRACE[]
Definition: bom_plugins.cpp:31
static wxString GetUserPluginsPath()
Gets the user path for plugins.
Definition: paths.cpp:53
static wxString GetStockPluginsPath()
Gets the stock (install) plugins path.
Definition: paths.cpp:175

References BOM_TRACE, PATHS::GetStockPluginsPath(), PATHS::GetUserPluginsPath(), and m_file.

Referenced by BOM_GENERATOR_HANDLER(), and DIALOG_BOM::pluginInit().

◆ GetCommand()

const wxString& BOM_GENERATOR_HANDLER::GetCommand ( ) const
inline

Return the command to execute the plugin.

Definition at line 116 of file bom_plugins.h.

117  {
118  return m_cmd;
119  }
wxString m_cmd
Description of the plugin (normally from the plugin header)
Definition: bom_plugins.h:166

References m_cmd.

Referenced by DIALOG_BOM::pluginInit().

◆ GetFile()

const wxFileName& BOM_GENERATOR_HANDLER::GetFile ( ) const
inline

Return the file name of the plugin.

Definition at line 78 of file bom_plugins.h.

79  {
80  return m_file;
81  }
wxFileName m_file
Path to the plugin stored in config (can be absolute or just a filename)
Definition: bom_plugins.h:157

References m_file.

Referenced by DIALOG_BOM::pluginInit().

◆ GetInfo()

const wxString& BOM_GENERATOR_HANDLER::GetInfo ( ) const
inline

Return plugin description stored in the plugin header file (if available).

Definition at line 70 of file bom_plugins.h.

71  {
72  return m_info;
73  }
wxString m_info
Plugin specific options.
Definition: bom_plugins.h:169

References m_info.

Referenced by DIALOG_BOM::pluginInit().

◆ GetName()

const wxString& BOM_GENERATOR_HANDLER::GetName ( void  ) const
inline

Return the customisable plugin name.

Definition at line 98 of file bom_plugins.h.

99  {
100  return m_name;
101  }
wxString m_name
Command to execute the plugin.
Definition: bom_plugins.h:163

References m_name.

Referenced by DIALOG_BOM::pluginInit().

◆ getOutputExtension()

wxString BOM_GENERATOR_HANDLER::getOutputExtension ( const wxString &  aHeader)
staticprotected

Extracts the output BOM file's extension, including the '.

', from the plugin file header. If the output extension cannot be determined from the plugin header, returns wxEmptyString.

Parameters
aHeaderis the plugin file's header, as returned by readHeader()true if the plugin is working (i.e. if the plugin file exists and was read

Definition at line 143 of file bom_plugins.cpp.

144 {
145  // search header for extension after %O (extension includes '.')
146  // looks for output argument of the form `"%O.extension"`
147  const wxString outputarg( "\"%O" );
148 
149  int strstart = aHeader.Find( outputarg );
150 
151  if( strstart == wxNOT_FOUND )
152  return wxEmptyString;
153 
154  strstart += outputarg.Length();
155  int strend = aHeader.find( "\"", strstart );
156 
157  if( strend == wxNOT_FOUND )
158  return wxEmptyString;
159 
160  return aHeader.SubString( strstart, strend - 1 );
161 }

Referenced by BOM_GENERATOR_HANDLER().

◆ GetStoredPath()

wxString BOM_GENERATOR_HANDLER::GetStoredPath ( ) const
inline

Definition at line 83 of file bom_plugins.h.

83 { return m_storedPath; }
const wxString m_storedPath
User customisable name.
Definition: bom_plugins.h:160

References m_storedPath.

◆ IsOk()

bool BOM_GENERATOR_HANDLER::IsOk ( )
inline

Return true if the plugin is ready to work, i.e.

if the plugin file is found and readable.

Definition at line 58 of file bom_plugins.h.

58 { return m_isOk; }
bool m_isOk
Path to the plugin.
Definition: bom_plugins.h:154

References m_isOk.

Referenced by DIALOG_BOM::pluginInit().

◆ IsValidGenerator()

bool BOM_GENERATOR_HANDLER::IsValidGenerator ( const wxString &  aFile)
static

Return true if a file name matches a recognized plugin format.

Parameters
aFileis path to the plugin file.

Definition at line 95 of file bom_plugins.cpp.

96 {
97  wxFileName fn( aFile );
98  wxString ext = fn.GetExt().Lower();
99 
100  for( const auto& pluginExt : { "xsl", "py", "pyw" } )
101  {
102  if( pluginExt == ext )
103  return true;
104  }
105 
106  return false;
107 }

◆ Options()

wxArrayString& BOM_GENERATOR_HANDLER::Options ( )
inline

Accessor to array of options.

Definition at line 132 of file bom_plugins.h.

133  {
134  return m_options;
135  }
wxArrayString m_options
Definition: bom_plugins.h:172

References m_options.

Referenced by DIALOG_BOM::pluginInit().

◆ readHeader()

wxString BOM_GENERATOR_HANDLER::readHeader ( const wxString &  aEndSection)
protected

Read the plugin file header.

Parameters
aEndSectionis a string marking end of the header.

Definition at line 110 of file bom_plugins.cpp.

111 {
112  if( aEndSection.IsEmpty() )
113  return wxEmptyString;
114 
115  wxFFile fdata( m_file.GetFullPath(), "rb" ); // dtor will close the file
116  wxString data;
117 
118  if( !fdata.ReadAll( &data ) )
119  return wxEmptyString;
120 
121  const wxString header( "@package" );
122 
123  // Extract substring between @package and endsection
124  int strstart = data.Find( header );
125 
126  if( strstart == wxNOT_FOUND )
127  return wxEmptyString;
128 
129  strstart += header.Length();
130  int strend = data.find( aEndSection, strstart );
131 
132  if( strend == wxNOT_FOUND )
133  return wxEmptyString;
134 
135  // Remove empty line if any
136  while( data[strstart] < ' ' )
137  strstart++;
138 
139  return data.SubString( strstart, strend - 1 );
140 }
wxFileName m_file
Path to the plugin stored in config (can be absolute or just a filename)
Definition: bom_plugins.h:157

References m_file.

Referenced by BOM_GENERATOR_HANDLER().

◆ SetCommand()

void BOM_GENERATOR_HANDLER::SetCommand ( const wxString &  aCommand)
inline

Set the command to execute the plugin.

Definition at line 124 of file bom_plugins.h.

125  {
126  m_cmd = aCommand;
127  }
wxString m_cmd
Description of the plugin (normally from the plugin header)
Definition: bom_plugins.h:166

References m_cmd.

◆ SetName()

void BOM_GENERATOR_HANDLER::SetName ( const wxString &  aName)
inline

Set the customisable plugin name.

Parameters
aNameis the new name.

Definition at line 108 of file bom_plugins.h.

109  {
110  m_name = aName;
111  }
wxString m_name
Command to execute the plugin.
Definition: bom_plugins.h:163

References m_name.

Member Data Documentation

◆ m_cmd

wxString BOM_GENERATOR_HANDLER::m_cmd
protected

Description of the plugin (normally from the plugin header)

Definition at line 166 of file bom_plugins.h.

Referenced by BOM_GENERATOR_HANDLER(), GetCommand(), and SetCommand().

◆ m_file

wxFileName BOM_GENERATOR_HANDLER::m_file
protected

Path to the plugin stored in config (can be absolute or just a filename)

Definition at line 157 of file bom_plugins.h.

Referenced by BOM_GENERATOR_HANDLER(), FindFilePath(), GetFile(), and readHeader().

◆ m_info

wxString BOM_GENERATOR_HANDLER::m_info
protected

Plugin specific options.

Definition at line 169 of file bom_plugins.h.

Referenced by BOM_GENERATOR_HANDLER(), and GetInfo().

◆ m_isOk

bool BOM_GENERATOR_HANDLER::m_isOk
protected

Path to the plugin.

Definition at line 154 of file bom_plugins.h.

Referenced by BOM_GENERATOR_HANDLER(), and IsOk().

◆ m_name

wxString BOM_GENERATOR_HANDLER::m_name
protected

Command to execute the plugin.

Definition at line 163 of file bom_plugins.h.

Referenced by BOM_GENERATOR_HANDLER(), GetName(), and SetName().

◆ m_options

wxArrayString BOM_GENERATOR_HANDLER::m_options
protected

Definition at line 172 of file bom_plugins.h.

Referenced by Options().

◆ m_storedPath

const wxString BOM_GENERATOR_HANDLER::m_storedPath
protected

User customisable name.

Definition at line 160 of file bom_plugins.h.

Referenced by GetStoredPath().


The documentation for this class was generated from the following files: