23#include <wx/tokenzr.h>
35 SCH_IO( wxS(
"HTTP library" ) ),
42 const std::map<std::string, UTF8>* aProperties )
44 std::vector<LIB_SYMBOL*> symbols;
48 aSymbolNameList.Add( symbol->GetName() );
53 const std::map<std::string, UTF8>* aProperties )
55 wxCHECK_RET(
m_adapter,
"HTTP plugin missing library manager adapter handle!" );
66 bool refresh_cache =
true;
75 refresh_cache =
false;
86 wxString libIDString( part.name );
90 if( symbol && ( !powerSymbolsOnly || symbol->
IsPower() ) )
91 aSymbolList.emplace_back( symbol );
98 const std::map<std::string, UTF8>* aProperties )
100 wxCHECK_MSG(
m_adapter,
nullptr,
"HTTP plugin missing library manager adapter handle!" );
107 std::string part_id =
"";
109 std::string partName( aAliasName.ToUTF8() );
114 std::vector<HTTP_LIB_CATEGORY> categories =
m_conn->getCategories();
116 if(
m_conn->GetCachedParts().empty() )
119 std::tuple relations =
m_conn->GetCachedParts()[partName];
120 std::string associatedCatID = std::get<1>( relations );
125 if( categoryIter.id == associatedCatID )
127 foundCategory = &categoryIter;
133 if( foundCategory ==
nullptr )
135 wxLogTrace(
traceHTTPLib, wxT(
"loadSymbol: no category found for %s" ), partName );
142 if( part.id == std::get<0>( relations ) )
151 wxLogTrace(
traceHTTPLib, wxT(
"LoadSymbol: SelectOne (%s) found in %s" ), part_id, foundCategory->
name );
155 wxLogTrace(
traceHTTPLib, wxT(
"LoadSymbol: SelectOne (%s) failed for category %s" ), part_id,
156 foundCategory->
name );
161 wxCHECK( foundCategory,
nullptr );
173 std::set<wxString> categoryNames;
177 if( categoryNames.count( categoryIter.name ) )
180 aNames.emplace_back( categoryIter.name );
181 categoryNames.insert( categoryIter.name );
188 return m_conn->getCategoryDescription( std::string( aName.mb_str() ) );
212 THROW_IO_ERROR( wxString::Format(
_(
"HTTP library settings file %s missing or invalid." ),
216 if(
m_settings->m_Source.api_version.empty() )
218 THROW_IO_ERROR( wxString::Format(
_(
"HTTP library settings file %s is missing the API version "
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,
233 THROW_IO_ERROR( wxString::Format(
_(
"HTTP library settings file %s is missing the root URL." ),
242 THROW_IO_ERROR( wxString::Format(
_(
"HTTP library settings file %s has invalid library type." ),
256 std::string
path( aSettingsPath.ToUTF8() );
270 wxLogTrace(
traceHTTPLib, wxT(
"ensureSettings: no settings available!" ) );
277 wxCHECK_RET(
m_settings,
"Call ensureSettings before ensureConnection!" );
283 THROW_IO_ERROR( wxString::Format(
_(
"Could not connect to %s. Errors: %s" ),
292 wxCHECK_RET(
m_settings,
"Call ensureSettings before connect()!" );
296 m_conn = std::make_unique<HTTP_LIB_CONNECTION>(
m_settings->m_Source,
true );
298 if( !
m_conn->IsValidEndpoint() )
320 std::vector<HTTP_LIB_PART> found_parts;
322 if( !
m_conn->SelectAll( category, found_parts ) )
324 if( !
m_conn->GetLastError().empty() )
326 THROW_IO_ERROR( wxString::Format(
_(
"Error retrieving data from HTTP library %s: %s" ),
328 m_conn->GetLastError() ) );
344 const wxString& aSymbolName,
355 wxFileName libFileName( aLibraryPath );
356 wxString libNickname = libFileName.GetName();
359 if( !symbolIdStr.empty() )
361 symbolId.
Parse( symbolIdStr );
364 originalSymbol =
m_adapter->LoadSymbol( symbolId );
368 wxLogTrace(
traceHTTPLib, wxT(
"loadSymbolFromPart: found original symbol '%s'" ), symbolIdStr );
372 symbol->
SetName( aSymbolName );
381 wxLogTrace(
traceHTTPLib, wxT(
"loadSymbolFromPart: source symbol id '%s' is invalid, "
382 "will create empty symbol" ), symbolIdStr );
386 wxLogTrace(
traceHTTPLib, wxT(
"loadSymbolFromPart: source symbol '%s' not found, "
387 "will create empty symbol" ), symbolIdStr );
407 wxArrayString fp_filters;
409 for(
auto& [fieldName, fieldProperties] : aPart.
fields )
411 wxString lowerFieldName = wxString( fieldName ).Lower();
416 wxStringTokenizer tokenizer( std::get<0>( fieldProperties ),
";\t\r\n", wxTOKEN_STRTOK );
418 while( tokenizer.HasMoreTokens() )
419 fp_filters.Add( tokenizer.GetNextToken() );
421 if( fp_filters.size() > 0 )
422 field->
SetText( fp_filters[0] );
424 field->
SetVisible( std::get<1>( fieldProperties ) );
429 field->
SetText( std::get<0>( fieldProperties ) );
430 field->
SetVisible( std::get<1>( fieldProperties ) );
435 field->
SetText( std::get<0>( fieldProperties ) );
436 field->
SetVisible( std::get<1>( fieldProperties ) );
441 field->
SetText( std::get<0>( fieldProperties ) );
442 field->
SetVisible( std::get<1>( fieldProperties ) );
447 field->
SetText( std::get<0>( fieldProperties ) );
448 field->
SetVisible( std::get<1>( fieldProperties ) );
452 symbol->
SetKeyWords( std::get<0>( fieldProperties ) );
463 if( field !=
nullptr )
466 field->
SetText( std::get<0>( fieldProperties ) );
467 field->
SetVisible( std::get<1>( fieldProperties ) );
475 field->
SetText( std::get<0>( fieldProperties ) );
476 field->
SetVisible( std::get<1>( fieldProperties ) );
488 fp_filters.push_back(
filter );
496 const std::map<std::string, UTF8>* aProperties )
virtual void SetVisible(bool aVisible)
A logical library item identifier and consists of various portions much like a URI.
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
bool IsValid() const
Check if this LID_ID is valid.
int SetLibNickname(const UTF8 &aLibNickname)
Override the logical library name portion of the LIB_ID to aLibNickname.
void SetSubLibraryName(const UTF8 &aName)
Define a library symbol object.
SCH_FIELD & GetDescriptionField()
Return reference to the description field.
const LIB_ID & GetLibId() const override
bool IsPower() const override
void SetSourceLibId(const LIB_ID &aLibId)
SCH_FIELD & GetDatasheetField()
Return reference to the datasheet field.
virtual LIB_SYMBOL * Duplicate() const
Create a copy of a LIB_SYMBOL and assigns unique KIIDs to the copy and its children.
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.
void SetDescription(const wxString &aDescription)
Gets the Description field text value */.
void SetKeyWords(const wxString &aKeyWords)
SCH_FIELD & GetValueField()
Return reference to the value field.
void SetFPFilters(const wxArrayString &aFilters)
void SetLibId(const LIB_ID &aLibId)
void AddField(SCH_FIELD *aField)
Add a field.
virtual void SetName(const wxString &aName)
SCH_FIELD & GetReferenceField()
Return reference to the reference designator field.
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)
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.
virtual void SetExcludedFromSim(bool aExcludeFromSim, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) override
Set or clear the exclude from simulation flag.
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.
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::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.
wxString result
Test unit parsing edge cases and error handling.