KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_io_http_lib.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) 2023 Andre F. K. Iwers <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21
22#include <wx/log.h>
23#include <wx/tokenzr.h>
24
25#include <fmt.h>
26#include <lib_symbol.h>
27
30#include "sch_io_http_lib.h"
31#include <ki_exception.h>
32
33
35 SCH_IO( wxS( "HTTP library" ) ),
36 m_adapter( nullptr )
37{
38}
39
40
41void SCH_IO_HTTP_LIB::EnumerateSymbolLib( wxArrayString& aSymbolNameList, const wxString& aLibraryPath,
42 const std::map<std::string, UTF8>* aProperties )
43{
44 std::vector<LIB_SYMBOL*> symbols;
45 EnumerateSymbolLib( symbols, aLibraryPath, aProperties );
46
47 for( LIB_SYMBOL* symbol : symbols )
48 aSymbolNameList.Add( symbol->GetName() );
49}
50
51
52void SCH_IO_HTTP_LIB::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList, const wxString& aLibraryPath,
53 const std::map<std::string, UTF8>* aProperties )
54{
55 wxCHECK_RET( m_adapter, "HTTP plugin missing library manager adapter handle!" );
56 ensureSettings( aLibraryPath );
58
59 if( !m_conn )
61
62 bool powerSymbolsOnly = ( aProperties && aProperties->contains( SYMBOL_LIBRARY_ADAPTER::PropPowerSymsOnly ) );
63
64 for( const HTTP_LIB_CATEGORY& category : m_conn->getCategories() )
65 {
66 bool refresh_cache = true;
67
68 // Check if there is already a part in our cache, if not fetch it
69 if( m_cachedCategories.find( category.id ) != m_cachedCategories.end() )
70 {
71 // check if it's outdated, if so re-fetch
72 if( std::difftime( std::time( nullptr ), m_cachedCategories[category.id].lastCached )
73 < m_settings->m_Source.timeout_categories )
74 {
75 refresh_cache = false;
76 }
77 }
78
79 if( refresh_cache )
80 {
81 syncCache( category );
82 }
83
84 for( const HTTP_LIB_PART& part : m_cachedCategories[category.id].cachedParts )
85 {
86 wxString libIDString( part.name );
87
88 LIB_SYMBOL* symbol = loadSymbolFromPart( aLibraryPath, libIDString, category, part );
89
90 if( symbol && ( !powerSymbolsOnly || symbol->IsPower() ) )
91 aSymbolList.emplace_back( symbol );
92 }
93 }
94}
95
96
97LIB_SYMBOL* SCH_IO_HTTP_LIB::LoadSymbol( const wxString& aLibraryPath, const wxString& aAliasName,
98 const std::map<std::string, UTF8>* aProperties )
99{
100 wxCHECK_MSG( m_adapter, nullptr, "HTTP plugin missing library manager adapter handle!" );
101 ensureSettings( aLibraryPath );
103
104 if( !m_conn )
106
107 std::string part_id = "";
108
109 std::string partName( aAliasName.ToUTF8() );
110
111 const HTTP_LIB_CATEGORY* foundCategory = nullptr;
113
114 std::vector<HTTP_LIB_CATEGORY> categories = m_conn->getCategories();
115
116 if( m_conn->GetCachedParts().empty() )
117 syncCache();
118
119 std::tuple relations = m_conn->GetCachedParts()[partName];
120 std::string associatedCatID = std::get<1>( relations );
121
122 // get the matching category
123 for( const HTTP_LIB_CATEGORY& categoryIter : categories )
124 {
125 if( categoryIter.id == associatedCatID )
126 {
127 foundCategory = &categoryIter;
128 break;
129 }
130 }
131
132 // return Null if no category was found. This should never happen
133 if( foundCategory == nullptr )
134 {
135 wxLogTrace( traceHTTPLib, wxT( "loadSymbol: no category found for %s" ), partName );
136 return nullptr;
137 }
138
139 // get the matching query ID
140 for( const HTTP_LIB_PART& part : m_cachedCategories[foundCategory->id].cachedParts )
141 {
142 if( part.id == std::get<0>( relations ) )
143 {
144 part_id = part.id;
145 break;
146 }
147 }
148
149 if( m_conn->SelectOne( part_id, result ) )
150 {
151 wxLogTrace( traceHTTPLib, wxT( "LoadSymbol: SelectOne (%s) found in %s" ), part_id, foundCategory->name );
152 }
153 else
154 {
155 wxLogTrace( traceHTTPLib, wxT( "LoadSymbol: SelectOne (%s) failed for category %s" ), part_id,
156 foundCategory->name );
157
159 }
160
161 wxCHECK( foundCategory, nullptr );
162
163 return loadSymbolFromPart( aLibraryPath, aAliasName, *foundCategory, result );
164}
165
166
167void SCH_IO_HTTP_LIB::GetSubLibraryNames( std::vector<wxString>& aNames )
168{
169 ensureSettings( wxEmptyString );
170
171 aNames.clear();
172
173 std::set<wxString> categoryNames;
174
175 for( const HTTP_LIB_CATEGORY& categoryIter : m_conn->getCategories() )
176 {
177 if( categoryNames.count( categoryIter.name ) )
178 continue;
179
180 aNames.emplace_back( categoryIter.name );
181 categoryNames.insert( categoryIter.name );
182 }
183}
184
185
186wxString SCH_IO_HTTP_LIB::GetSubLibraryDescription( const wxString& aName )
187{
188 return m_conn->getCategoryDescription( std::string( aName.mb_str() ) );
189}
190
191
192void SCH_IO_HTTP_LIB::GetAvailableSymbolFields( std::vector<wxString>& aNames )
193{
194 // TODO: Implement this sometime; This is currently broken...
195 std::copy( m_customFields.begin(), m_customFields.end(), std::back_inserter( aNames ) );
196}
197
198
199void SCH_IO_HTTP_LIB::GetDefaultSymbolFields( std::vector<wxString>& aNames )
200{
201 std::copy( m_defaultShownFields.begin(), m_defaultShownFields.end(), std::back_inserter( aNames ) );
202}
203
204
205void SCH_IO_HTTP_LIB::ensureSettings( const wxString& aSettingsPath )
206{
207 auto tryLoad =
208 [&]()
209 {
210 if( !m_settings->LoadFromFile() )
211 {
212 THROW_IO_ERROR( wxString::Format( _( "HTTP library settings file %s missing or invalid." ),
213 aSettingsPath ) );
214 }
215
216 if( m_settings->m_Source.api_version.empty() )
217 {
218 THROW_IO_ERROR( wxString::Format( _( "HTTP library settings file %s is missing the API version "
219 "number." ),
220 aSettingsPath ) );
221 }
222
223 if( m_settings->getSupportedAPIVersion() != m_settings->m_Source.api_version )
224 {
225 THROW_IO_ERROR( wxString::Format( _( "HTTP library settings file %s uses API version %s, but "
226 "KiCad requires version %s." ),
227 aSettingsPath, m_settings->m_Source.api_version,
228 m_settings->getSupportedAPIVersion() ) );
229 }
230
231 if( m_settings->m_Source.root_url.empty() )
232 {
233 THROW_IO_ERROR( wxString::Format( _( "HTTP library settings file %s is missing the root URL." ),
234 aSettingsPath ) );
235 }
236
237 // map lib source type
238 m_settings->m_Source.type = m_settings->get_HTTP_LIB_SOURCE_TYPE();
239
240 if( m_settings->m_Source.type == HTTP_LIB_SOURCE_TYPE::INVALID )
241 {
242 THROW_IO_ERROR( wxString::Format( _( "HTTP library settings file %s has invalid library type." ),
243 aSettingsPath ) );
244 }
245
246 // make sure that the root url finishes with a forward slash
247 if( m_settings->m_Source.root_url.at( m_settings->m_Source.root_url.length() - 1 ) != '/' )
248 m_settings->m_Source.root_url += "/";
249
250 // Append api version to root URL
251 m_settings->m_Source.root_url += m_settings->m_Source.api_version + "/";
252 };
253
254 if( !m_settings && !aSettingsPath.IsEmpty() )
255 {
256 std::string path( aSettingsPath.ToUTF8() );
257 m_settings = std::make_unique<HTTP_LIB_SETTINGS>( path );
258
259 m_settings->SetReadOnly( true );
260
261 tryLoad();
262 }
263 else if( m_settings )
264 {
265 // If we have valid settings but no connection yet; reload settings in case user is editing
266 tryLoad();
267 }
268 else if( !m_settings )
269 {
270 wxLogTrace( traceHTTPLib, wxT( "ensureSettings: no settings available!" ) );
271 }
272}
273
274
276{
277 wxCHECK_RET( m_settings, "Call ensureSettings before ensureConnection!" );
278
279 connect();
280
281 if( !m_conn || !m_conn->IsValidEndpoint() )
282 {
283 THROW_IO_ERROR( wxString::Format( _( "Could not connect to %s. Errors: %s" ),
284 m_settings->m_Source.root_url,
285 m_lastError ) );
286 }
287}
288
289
291{
292 wxCHECK_RET( m_settings, "Call ensureSettings before connect()!" );
293
294 if( !m_conn )
295 {
296 m_conn = std::make_unique<HTTP_LIB_CONNECTION>( m_settings->m_Source, true );
297
298 if( !m_conn->IsValidEndpoint() )
299 {
300 m_lastError = m_conn->GetLastError();
301
302 // Make sure we release pointer so we are able to query API again next time
303 m_conn.reset();
304
305 return;
306 }
307 }
308}
309
310
312{
313 for( const HTTP_LIB_CATEGORY& category : m_conn->getCategories() )
314 syncCache( category );
315}
316
317
319{
320 std::vector<HTTP_LIB_PART> found_parts;
321
322 if( !m_conn->SelectAll( category, found_parts ) )
323 {
324 if( !m_conn->GetLastError().empty() )
325 {
326 THROW_IO_ERROR( wxString::Format( _( "Error retrieving data from HTTP library %s: %s" ),
327 category.name,
328 m_conn->GetLastError() ) );
329 }
330
331 return;
332 }
333
334 // remove cached parts
335 m_cachedCategories[category.id].cachedParts.clear();
336
337 // Copy newly cached data across
338 m_cachedCategories[category.id].cachedParts = found_parts;
339 m_cachedCategories[category.id].lastCached = std::time( nullptr );
340}
341
342
343LIB_SYMBOL* SCH_IO_HTTP_LIB::loadSymbolFromPart( const wxString& aLibraryPath,
344 const wxString& aSymbolName,
345 const HTTP_LIB_CATEGORY& aCategory,
346 const HTTP_LIB_PART& aPart )
347{
348 LIB_SYMBOL* symbol = nullptr;
349 LIB_SYMBOL* originalSymbol = nullptr;
350 LIB_ID symbolId;
351
352 std::string symbolIdStr = aPart.symbolIdStr;
353
354 // Extract library nickname from the library path (e.g., "/path/to/W5.kicad_httplib" -> "W5")
355 wxFileName libFileName( aLibraryPath );
356 wxString libNickname = libFileName.GetName();
357
358 // Get or Create the symbol using the found symbol
359 if( !symbolIdStr.empty() )
360 {
361 symbolId.Parse( symbolIdStr );
362
363 if( symbolId.IsValid() )
364 originalSymbol = m_adapter->LoadSymbol( symbolId );
365
366 if( originalSymbol )
367 {
368 wxLogTrace( traceHTTPLib, wxT( "loadSymbolFromPart: found original symbol '%s'" ), symbolIdStr );
369
370 symbol = originalSymbol->Duplicate();
371 symbol->SetSourceLibId( symbolId );
372 symbol->SetName( aSymbolName );
373
374 LIB_ID libId = symbol->GetLibId();
375 libId.SetLibNickname( libNickname );
376 libId.SetSubLibraryName( aCategory.name );
377 symbol->SetLibId( libId );
378 }
379 else if( !symbolId.IsValid() )
380 {
381 wxLogTrace( traceHTTPLib, wxT( "loadSymbolFromPart: source symbol id '%s' is invalid, "
382 "will create empty symbol" ), symbolIdStr );
383 }
384 else
385 {
386 wxLogTrace( traceHTTPLib, wxT( "loadSymbolFromPart: source symbol '%s' not found, "
387 "will create empty symbol" ), symbolIdStr );
388 }
389 }
390
391 if( !symbol )
392 {
393 // Actual symbol not found: return metadata only; error will be
394 // indicated in the symbol chooser
395 symbol = new LIB_SYMBOL( aSymbolName );
396
397 LIB_ID libId = symbol->GetLibId();
398 libId.SetLibNickname( libNickname );
399 libId.SetSubLibraryName( aCategory.name );
400 symbol->SetLibId( libId );
401 }
402
403 symbol->SetExcludedFromBOM( aPart.exclude_from_bom );
405 symbol->SetExcludedFromSim( aPart.exclude_from_sim );
406
407 wxArrayString fp_filters;
408
409 for( auto& [fieldName, fieldProperties] : aPart.fields )
410 {
411 wxString lowerFieldName = wxString( fieldName ).Lower();
412
413 if( lowerFieldName == footprint_field )
414 {
415 SCH_FIELD* field = &symbol->GetFootprintField();
416 wxStringTokenizer tokenizer( std::get<0>( fieldProperties ), ";\t\r\n", wxTOKEN_STRTOK );
417
418 while( tokenizer.HasMoreTokens() )
419 fp_filters.Add( tokenizer.GetNextToken() );
420
421 if( fp_filters.size() > 0 )
422 field->SetText( fp_filters[0] );
423
424 field->SetVisible( std::get<1>( fieldProperties ) );
425 }
426 else if( lowerFieldName == description_field )
427 {
428 SCH_FIELD* field = &symbol->GetDescriptionField();
429 field->SetText( std::get<0>( fieldProperties ) );
430 field->SetVisible( std::get<1>( fieldProperties ) );
431 }
432 else if( lowerFieldName == value_field )
433 {
434 SCH_FIELD* field = &symbol->GetValueField();
435 field->SetText( std::get<0>( fieldProperties ) );
436 field->SetVisible( std::get<1>( fieldProperties ) );
437 }
438 else if( lowerFieldName == datasheet_field )
439 {
440 SCH_FIELD* field = &symbol->GetDatasheetField();
441 field->SetText( std::get<0>( fieldProperties ) );
442 field->SetVisible( std::get<1>( fieldProperties ) );
443 }
444 else if( lowerFieldName == reference_field )
445 {
446 SCH_FIELD* field = &symbol->GetReferenceField();
447 field->SetText( std::get<0>( fieldProperties ) );
448 field->SetVisible( std::get<1>( fieldProperties ) );
449 }
450 else if( lowerFieldName == keywords_field )
451 {
452 symbol->SetKeyWords( std::get<0>( fieldProperties ) );
453 }
454 else
455 {
456 // Check if field exists, if so replace Text and adjust visiblity.
457 //
458 // This proves useful in situations where, for instance, an individual requires a particular value, such as
459 // the material type showcased at a specific position for a capacitor. Subsequently, this value could be defined
460 // in the symbol itself and then, potentially, be modified by the HTTP library as necessary.
461 SCH_FIELD* field = symbol->GetField( fieldName );
462
463 if( field != nullptr )
464 {
465 // adjust values accordingly
466 field->SetText( std::get<0>( fieldProperties ) );
467 field->SetVisible( std::get<1>( fieldProperties ) );
468 }
469 else
470 {
471 // Generic fields
472 field = new SCH_FIELD( symbol, FIELD_T::USER );
473 field->SetName( fieldName );
474
475 field->SetText( std::get<0>( fieldProperties ) );
476 field->SetVisible( std::get<1>( fieldProperties ) );
477 symbol->AddField( field );
478
479 m_customFields.insert( fieldName );
480 }
481 }
482 }
483
484 symbol->SetDescription( aPart.desc );
485 symbol->SetKeyWords( aPart.keywords );
486
487 for( const std::string& filter : aPart.fp_filters )
488 fp_filters.push_back( filter );
489
490 symbol->SetFPFilters( fp_filters );
491
492 return symbol;
493}
494
495void SCH_IO_HTTP_LIB::SaveSymbol( const wxString& aLibraryPath, const LIB_SYMBOL* aSymbol,
496 const std::map<std::string, UTF8>* aProperties )
497{
498 // TODO: Implement this sometime;
499}
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:400
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:52
bool IsValid() const
Check if this LID_ID is valid.
Definition lib_id.h:172
int SetLibNickname(const UTF8 &aLibNickname)
Override the logical library name portion of the LIB_ID to aLibNickname.
Definition lib_id.cpp:100
void SetSubLibraryName(const UTF8 &aName)
Definition lib_id.h:131
Define a library symbol object.
Definition lib_symbol.h:83
SCH_FIELD & GetDescriptionField()
Return reference to the description field.
Definition lib_symbol.h:348
const LIB_ID & GetLibId() const override
Definition lib_symbol.h:152
bool IsPower() const override
void SetSourceLibId(const LIB_ID &aLibId)
Definition lib_symbol.h:156
SCH_FIELD & GetDatasheetField()
Return reference to the datasheet field.
Definition lib_symbol.h:344
virtual LIB_SYMBOL * Duplicate() const
Create a copy of a LIB_SYMBOL and assigns unique KIIDs to the copy and its children.
Definition lib_symbol.h:97
SCH_FIELD * GetField(const wxString &aFieldName)
Find a field within this symbol matching aFieldName; return nullptr if not found.
SCH_FIELD & GetFootprintField()
Return reference to the footprint field.
Definition lib_symbol.h:340
void SetDescription(const wxString &aDescription)
Gets the Description field text value *‍/.
Definition lib_symbol.h:161
void SetKeyWords(const wxString &aKeyWords)
Definition lib_symbol.h:180
SCH_FIELD & GetValueField()
Return reference to the value field.
Definition lib_symbol.h:332
void SetFPFilters(const wxArrayString &aFilters)
Definition lib_symbol.h:212
void SetLibId(const LIB_ID &aLibId)
Definition lib_symbol.h:153
void AddField(SCH_FIELD *aField)
Add a field.
virtual void SetName(const wxString &aName)
SCH_FIELD & GetReferenceField()
Return reference to the reference designator field.
Definition lib_symbol.h:336
void SetName(const wxString &aName)
void SetText(const wxString &aText) override
std::unique_ptr< HTTP_LIB_CONNECTION > m_conn
Generally will be null if no valid connection is established.
LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aAliasName, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
void SaveSymbol(const wxString &aLibraryPath, const LIB_SYMBOL *aSymbol, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Write aSymbol to an existing library located at aLibraryPath.
std::unique_ptr< HTTP_LIB_SETTINGS > m_settings
wxString GetSubLibraryDescription(const wxString &aName) override
Gets a description of a sublibrary.
SYMBOL_LIBRARY_ADAPTER * m_adapter
void ensureSettings(const wxString &aSettingsPath)
std::set< wxString > m_customFields
void GetDefaultSymbolFields(std::vector< wxString > &aNames) override
Retrieves a list of (custom) field names that should be shown by default for this library in the symb...
void GetAvailableSymbolFields(std::vector< wxString > &aNames) override
Retrieves a list of (custom) field names that are present on symbols in this library.
std::set< wxString > m_defaultShownFields
LIB_SYMBOL * loadSymbolFromPart(const wxString &aLibraryPath, const wxString &aSymbolName, const HTTP_LIB_CATEGORY &aCategory, const HTTP_LIB_PART &aPart)
wxString description_field
std::map< std::string, HTTP_LIB_CATEGORY > m_cachedCategories
void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
void GetSubLibraryNames(std::vector< wxString > &aNames) override
Retrieves a list of sub-libraries in this library.
SCH_IO(const wxString &aName)
Definition sch_io.h:375
static const char * PropPowerSymsOnly
void SetExcludedFromBoard(bool aExclude, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
Set or clear exclude from board netlist flag.
Definition symbol.h:210
virtual void SetExcludedFromSim(bool aExcludeFromSim, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
Set or clear the exclude from simulation flag.
Definition symbol.h:180
virtual void SetExcludedFromBOM(bool aExcludeFromBOM, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
Set or clear the exclude from schematic bill of materials flag.
Definition symbol.h:195
#define _(s)
const char *const traceHTTPLib
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
std::string id
id of category
std::string name
name of category
std::string symbolIdStr
std::string keywords
std::vector< std::string > fp_filters
std::vector< std::pair< std::string, std::tuple< std::string, bool > > > fields
@ USER
The field ID hasn't been set yet; field is invalid.
std::string path
wxString result
Test unit parsing edge cases and error handling.