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 (C) 2024 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
24#include <fmt.h>
25#include <lib_symbol.h>
26#include <symbol_lib_table.h>
27
29#include "sch_io_http_lib.h"
30
31
32SCH_IO_HTTP_LIB::SCH_IO_HTTP_LIB() : SCH_IO( wxS( "HTTP library" ) ), m_libTable( nullptr )
33{
34}
35
36
38{
39}
40
41
42void SCH_IO_HTTP_LIB::EnumerateSymbolLib( wxArrayString& aSymbolNameList,
43 const wxString& aLibraryPath,
44 const STRING_UTF8_MAP* aProperties )
45{
46 std::vector<LIB_SYMBOL*> symbols;
47 EnumerateSymbolLib( symbols, aLibraryPath, aProperties );
48
49 for( LIB_SYMBOL* symbol : symbols )
50 aSymbolNameList.Add( symbol->GetName() );
51}
52
53
54void SCH_IO_HTTP_LIB::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
55 const wxString& aLibraryPath,
56 const STRING_UTF8_MAP* aProperties )
57{
58 wxCHECK_RET( m_libTable, _( "httplib plugin missing library table handle!" ) );
59 ensureSettings( aLibraryPath );
61
62 if( !m_conn )
63 {
65 return;
66 }
67
68 bool powerSymbolsOnly =
69 ( aProperties
70 && aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->end() );
71
72 for( const HTTP_LIB_CATEGORY& category : m_conn->getCategories() )
73 {
74 bool refresh_cache = true;
75
76 // Check if there is already a part in our cache, if not fetch it
77 if( m_cachedCategories.find( category.id ) != m_cachedCategories.end() )
78 {
79 // check if it's outdated, if so re-fetch
80 if( std::difftime( std::time( nullptr ), m_cachedCategories[category.id].lastCached )
81 < m_settings->m_Source.timeout_categories )
82 {
83 refresh_cache = false;
84 }
85 }
86
87 if( refresh_cache )
88 {
89 syncCache( category );
90 }
91
92 for( const HTTP_LIB_PART& part : m_cachedCategories[category.id].cachedParts )
93 {
94 wxString libIDString( part.name );
95
96 LIB_SYMBOL* symbol = loadSymbolFromPart( libIDString, category, part );
97
98 if( symbol && ( !powerSymbolsOnly || symbol->IsPower() ) )
99 aSymbolList.emplace_back( symbol );
100 }
101 }
102}
103
104
105LIB_SYMBOL* SCH_IO_HTTP_LIB::LoadSymbol( const wxString& aLibraryPath, const wxString& aAliasName,
106 const STRING_UTF8_MAP* aProperties )
107{
108 wxCHECK( m_libTable, nullptr );
109 ensureSettings( aLibraryPath );
111
112 if( !m_conn )
114
115 std::string part_id = "";
116
117 std::string partName( aAliasName.ToUTF8() );
118
119 const HTTP_LIB_CATEGORY* foundCategory = nullptr;
120 HTTP_LIB_PART result;
121
122 std::vector<HTTP_LIB_CATEGORY> categories = m_conn->getCategories();
123
124 if( m_conn->getCachedParts().empty() )
125 {
126 syncCache();
127 }
128
129 std::tuple relations = m_conn->getCachedParts()[partName];
130 std::string associatedCatID = std::get<1>( relations );
131
132 // get the matching category
133 for( const HTTP_LIB_CATEGORY& categoryIter : categories )
134 {
135 if( categoryIter.id == associatedCatID )
136 {
137 foundCategory = &categoryIter;
138 break;
139 }
140 }
141
142 // return Null if no category was found. This should never happen
143 if( foundCategory == nullptr )
144 {
145 wxLogTrace( traceHTTPLib, wxT( "loadSymbol: no category found for %s" ), partName );
146 return nullptr;
147 }
148
149 // get the matching query ID
150 for( const HTTP_LIB_PART& part : m_cachedCategories[foundCategory->id].cachedParts )
151 {
152 if( part.id == std::get<0>( relations ) )
153 {
154 part_id = part.id;
155 break;
156 }
157 }
158
159 if( m_conn->SelectOne( part_id, result ) )
160 {
161 wxLogTrace( traceHTTPLib, wxT( "loadSymbol: SelectOne (%s) found in %s" ), part_id,
162 foundCategory->name );
163 }
164 else
165 {
166 wxLogTrace( traceHTTPLib, wxT( "loadSymbol: SelectOne (%s) failed for category %s" ),
167 part_id, foundCategory->name );
168
170 }
171
172 wxCHECK( foundCategory, nullptr );
173
174 return loadSymbolFromPart( aAliasName, *foundCategory, result );
175}
176
177
178void SCH_IO_HTTP_LIB::GetSubLibraryNames( std::vector<wxString>& aNames )
179{
180 ensureSettings( wxEmptyString );
181
182 aNames.clear();
183
184 std::set<wxString> categoryNames;
185
186 for( const HTTP_LIB_CATEGORY& categoryIter : m_conn->getCategories() )
187 {
188 if( categoryNames.count( categoryIter.name ) )
189 continue;
190
191 aNames.emplace_back( categoryIter.name );
192 categoryNames.insert( categoryIter.name );
193 }
194}
195
196
197void SCH_IO_HTTP_LIB::GetAvailableSymbolFields( std::vector<wxString>& aNames )
198{
199 // TODO: Implement this sometime; This is currently broken...
200 std::copy( m_customFields.begin(), m_customFields.end(), std::back_inserter( aNames ) );
201}
202
203
204void SCH_IO_HTTP_LIB::GetDefaultSymbolFields( std::vector<wxString>& aNames )
205{
206 std::copy( m_defaultShownFields.begin(), m_defaultShownFields.end(),
207 std::back_inserter( aNames ) );
208}
209
210
211void SCH_IO_HTTP_LIB::ensureSettings( const wxString& aSettingsPath )
212{
213 auto tryLoad = [&]()
214 {
215 if( !m_settings->LoadFromFile() )
216 {
217 wxString msg = wxString::Format(
218 _( "HTTP library settings file %s missing or invalid" ), aSettingsPath );
219
220 THROW_IO_ERROR( msg );
221 }
222
223 if( m_settings->m_Source.api_version.empty() )
224 {
225 wxString msg = wxString::Format(
226 _( "HTTP library settings file %s is missing the API version number!" ),
227 aSettingsPath );
228
229 THROW_IO_ERROR( msg );
230 }
231
232 if( m_settings->getSupportedAPIVersion() != m_settings->m_Source.api_version )
233 {
234 wxString msg = wxString::Format( _( "HTTP library settings file %s uses API version "
235 "%s, but KiCad requires version %s" ),
236 aSettingsPath, m_settings->m_Source.api_version,
237 m_settings->getSupportedAPIVersion() );
238
239 THROW_IO_ERROR( msg );
240 }
241
242 if( m_settings->m_Source.root_url.empty() )
243 {
244 wxString msg = wxString::Format(
245 _( "HTTP library settings file %s is missing the root URL!" ), aSettingsPath );
246
247 THROW_IO_ERROR( msg );
248 }
249
250 // map lib source type
251 m_settings->m_Source.type = m_settings->get_HTTP_LIB_SOURCE_TYPE();
252
253 if( m_settings->m_Source.type == HTTP_LIB_SOURCE_TYPE::INVALID )
254 {
255 wxString msg = wxString::Format(
256 _( "HTTP library settings file %s has an invalid library type" ),
257 aSettingsPath );
258
259 THROW_IO_ERROR( msg );
260 }
261
262 // make sure that the root url finishes with a forward slash
263 if( m_settings->m_Source.root_url.at( m_settings->m_Source.root_url.length() - 1 ) != '/' )
264 {
265 m_settings->m_Source.root_url += "/";
266 }
267
268 // Append api version to root URL
269 m_settings->m_Source.root_url += m_settings->m_Source.api_version + "/";
270 };
271
272 if( !m_settings && !aSettingsPath.IsEmpty() )
273 {
274 std::string path( aSettingsPath.ToUTF8() );
275 m_settings = std::make_unique<HTTP_LIB_SETTINGS>( path );
276
277 m_settings->SetReadOnly( true );
278
279 tryLoad();
280 }
281 else if( m_settings )
282 {
283 // If we have valid settings but no connection yet; reload settings in case user is editing
284 tryLoad();
285 }
286 else if( !m_settings )
287 {
288 wxLogTrace( traceHTTPLib, wxT( "ensureSettings: no settings available!" ) );
289 }
290}
291
292
294{
295 wxCHECK_RET( m_settings, "Call ensureSettings before ensureConnection!" );
296
297 connect();
298
299 if( !m_conn || !m_conn->IsValidEndpoint() )
300 {
301 wxString msg = wxString::Format( _( "Could not connect to %s. Errors: %s" ),
302 m_settings->m_Source.root_url, m_lastError );
303
304 THROW_IO_ERROR( msg );
305 }
306}
307
308
310{
311 wxCHECK_RET( m_settings, "Call ensureSettings before connect()!" );
312
313 if( !m_conn )
314 {
315 m_conn = std::make_unique<HTTP_LIB_CONNECTION>( m_settings->m_Source, true );
316
317 if( !m_conn->IsValidEndpoint() )
318 {
319 m_lastError = m_conn->GetLastError();
320
321 // Make sure we release pointer so we are able to query API again next time
322 m_conn.reset();
323
324 return;
325 }
326 }
327}
328
330{
331 for( const HTTP_LIB_CATEGORY& category : m_conn->getCategories() )
332 {
333 syncCache( category );
334 }
335}
336
338{
339 std::vector<HTTP_LIB_PART> found_parts;
340
341 if( !m_conn->SelectAll( category, found_parts ) )
342 {
343 if( !m_conn->GetLastError().empty() )
344 {
345 wxString msg = wxString::Format( _( "Error retriving data from HTTP library %s: %s" ),
346 category.name, m_conn->GetLastError() );
347 THROW_IO_ERROR( msg );
348 }
349
350 return;
351 }
352
353 // remove cached parts
354 m_cachedCategories[category.id].cachedParts.clear();
355
356 // Copy newly cached data across
357 m_cachedCategories[category.id].cachedParts = found_parts;
358 m_cachedCategories[category.id].lastCached = std::time( nullptr );
359}
360
361
363 const HTTP_LIB_CATEGORY& aCategory,
364 const HTTP_LIB_PART& aPart )
365{
366 LIB_SYMBOL* symbol = nullptr;
367 LIB_SYMBOL* originalSymbol = nullptr;
368 LIB_ID symbolId;
369
370 std::string symbolIdStr = aPart.symbolIdStr;
371
372 // Get or Create the symbol using the found symbol
373 if( !symbolIdStr.empty() )
374 {
375 symbolId.Parse( symbolIdStr );
376
377 if( symbolId.IsValid() )
378 {
379 originalSymbol = m_libTable->LoadSymbol( symbolId );
380 }
381
382 if( originalSymbol )
383 {
384 wxLogTrace( traceHTTPLib, wxT( "loadSymbolFromPart: found original symbol '%s'" ),
385 symbolIdStr );
386
387 symbol = originalSymbol->Duplicate();
388 symbol->SetSourceLibId( symbolId );
389
390 LIB_ID libId = symbol->GetLibId();
391 libId.SetSubLibraryName( aCategory.name );
392 symbol->SetLibId( libId );
393 }
394 else if( !symbolId.IsValid() )
395 {
396 wxLogTrace( traceHTTPLib,
397 wxT( "loadSymbolFromPart: source symbol id '%s' is invalid, "
398 "will create empty symbol" ),
399 symbolIdStr );
400 }
401 else
402 {
403 wxLogTrace( traceHTTPLib,
404 wxT( "loadSymbolFromPart: source symbol '%s' not found, "
405 "will create empty symbol" ),
406 symbolIdStr );
407 }
408 }
409
410 if( !symbol )
411 {
412 // Actual symbol not found: return metadata only; error will be
413 // indicated in the symbol chooser
414 symbol = new LIB_SYMBOL( aSymbolName );
415
416 LIB_ID libId = symbol->GetLibId();
417 libId.SetSubLibraryName( aCategory.name );
418 symbol->SetLibId( libId );
419 }
420
421 symbol->SetExcludedFromBOM( aPart.exclude_from_bom );
423 symbol->SetExcludedFromSim( aPart.exclude_from_sim );
424
425 SCH_FIELD* field;
426
427 for( auto& _field : aPart.fields )
428 {
429 wxString fieldName = wxString( _field.first );
430 std::tuple fieldProperties = _field.second;
431
432 if( fieldName.Lower() == footprint_field )
433 {
434 field = &symbol->GetFootprintField();
435 field->SetText( std::get<0>( fieldProperties ) );
436 field->SetVisible( std::get<1>( fieldProperties ) );
437 }
438 else if( fieldName.Lower() == description_field )
439 {
440 field = &symbol->GetDescriptionField();
441 field->SetText( std::get<0>( fieldProperties ) );
442 field->SetVisible( std::get<1>( fieldProperties ) );
443 }
444 else if( fieldName.Lower() == value_field )
445 {
446 field = &symbol->GetValueField();
447 field->SetText( std::get<0>( fieldProperties ) );
448 field->SetVisible( std::get<1>( fieldProperties ) );
449 }
450 else if( fieldName.Lower() == datasheet_field )
451 {
452 field = &symbol->GetDatasheetField();
453 field->SetText( std::get<0>( fieldProperties ) );
454 field->SetVisible( std::get<1>( fieldProperties ) );
455 }
456 else if( fieldName.Lower() == reference_field )
457 {
458 field = &symbol->GetReferenceField();
459 field->SetText( std::get<0>( fieldProperties ) );
460 field->SetVisible( std::get<1>( fieldProperties ) );
461 }
462 else if( fieldName.Lower() == keywords_field )
463 {
464 symbol->SetKeyWords( std::get<0>( fieldProperties ) );
465 }
466 else
467 {
468 // Check if field exists, if so replace Text and adjust visiblity.
469 //
470 // This proves useful in situations where, for instance, an individual requires a particular value, such as
471 // the material type showcased at a specific position for a capacitor. Subsequently, this value could be defined
472 // in the symbol itself and then, potentially, be modified by the HTTP library as necessary.
473 field = symbol->FindField( fieldName );
474
475 if( field != nullptr )
476 {
477 // adjust values accordingly
478 field->SetText( std::get<0>( fieldProperties ) );
479 field->SetVisible( std::get<1>( fieldProperties ) );
480 }
481 else
482 {
483 // Generic fields
484 field = new SCH_FIELD( nullptr, symbol->GetNextAvailableFieldId() );
485 field->SetName( fieldName );
486
487 field->SetText( std::get<0>( fieldProperties ) );
488 field->SetVisible( std::get<1>( fieldProperties ) );
489 symbol->AddField( field );
490
491 m_customFields.insert( fieldName );
492 }
493 }
494 }
495
496 return symbol;
497}
498
499void SCH_IO_HTTP_LIB::SaveSymbol( const wxString& aLibraryPath, const LIB_SYMBOL* aSymbol,
500 const STRING_UTF8_MAP* aProperties )
501{
502 // TODO: Implement this sometime;
503}
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:243
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:51
bool IsValid() const
Check if this LID_ID is valid.
Definition: lib_id.h:172
void SetSubLibraryName(const UTF8 &aName)
Definition: lib_id.h:131
Define a library symbol object.
Definition: lib_symbol.h:77
const LIB_ID & GetLibId() const override
Definition: lib_symbol.h:142
bool IsPower() const override
Definition: lib_symbol.cpp:663
void SetSourceLibId(const LIB_ID &aLibId)
Definition: lib_symbol.h:146
int GetNextAvailableFieldId() const
SCH_FIELD & GetValueField() const
Return reference to the value field.
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:93
SCH_FIELD & GetDatasheetField() const
Return reference to the datasheet field.
SCH_FIELD & GetFootprintField() const
Return reference to the footprint field.
void SetKeyWords(const wxString &aKeyWords)
Definition: lib_symbol.h:168
SCH_FIELD & GetReferenceField() const
Return reference to the reference designator field.
SCH_FIELD & GetDescriptionField() const
Return reference to the description field.
SCH_FIELD * FindField(const wxString &aFieldName, bool aCaseInsensitive=false)
Find a field within this symbol matching aFieldName and returns it or NULL if not found.
void SetLibId(const LIB_ID &aLibId)
Definition: lib_symbol.h:143
void AddField(SCH_FIELD *aField)
Add a field.
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:51
void SetName(const wxString &aName)
Definition: sch_field.cpp:1097
void SetText(const wxString &aText) override
Definition: sch_field.cpp:1107
LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aAliasName, const STRING_UTF8_MAP *aProperties=nullptr) override
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
virtual ~SCH_IO_HTTP_LIB()
std::unique_ptr< HTTP_LIB_CONNECTION > m_conn
Generally will be null if no valid connection is established.
wxString reference_field
wxString footprint_field
std::unique_ptr< HTTP_LIB_SETTINGS > m_settings
wxString datasheet_field
void SaveSymbol(const wxString &aLibraryPath, const LIB_SYMBOL *aSymbol, const STRING_UTF8_MAP *aProperties=nullptr) override
Write aSymbol to an existing library located at aLibraryPath.
void ensureSettings(const wxString &aSettingsPath)
SYMBOL_LIB_TABLE * m_libTable
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.
void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr) override
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
std::set< wxString > m_defaultShownFields
wxString description_field
LIB_SYMBOL * loadSymbolFromPart(const wxString &aSymbolName, const HTTP_LIB_CATEGORY &aCategory, const HTTP_LIB_PART &aPart)
std::map< std::string, HTTP_LIB_CATEGORY > m_cachedCategories
void GetSubLibraryNames(std::vector< wxString > &aNames) override
Retrieves a list of sub-libraries in this library.
wxString keywords_field
Base class that schematic file and library loading and saving plugins should derive from.
Definition: sch_io.h:57
A name/value tuple with unique names and optional values.
static const char * PropPowerSymsOnly
LIB_SYMBOL * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_SYMBOL having aName from the library given by aNickname.
void SetExcludedFromSim(bool aExcludeFromSim) override
Set or clear the exclude from simulation flag.
Definition: symbol.h:135
void SetExcludedFromBOM(bool aExcludeFromBOM)
Set or clear the exclude from schematic bill of materials flag.
Definition: symbol.h:141
void SetExcludedFromBoard(bool aExcludeFromBoard)
Set or clear exclude from board netlist flag.
Definition: symbol.h:147
#define _(s)
const char *const traceHTTPLib
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
std::string id
id of category
std::string name
name of category
std::string symbolIdStr
std::vector< std::pair< std::string, std::tuple< std::string, bool > > > fields
additional generic fields