Browse Source
API: add a schema for plugin config files
API: add a schema for plugin config files
Also flip the dependence between json_schema_validator and kicommon, and create a shared JSON_SCHEMA_VALIDATOR so that we don't have to copy/paste the schema loading code as muchpcb_db
15 changed files with 290 additions and 43 deletions
-
16api/CMakeLists.txt
-
108api/schemas/api.v1.schema.json
-
3common/CMakeLists.txt
-
38common/api/api_plugin.cpp
-
11common/api/api_plugin_manager.cpp
-
64common/json_schema_validator.cpp
-
3include/api/api_plugin.h
-
3include/api/api_plugin_manager.h
-
43include/json_schema_validator.h
-
1kicad/pcm/CMakeLists.txt
-
30kicad/pcm/pcm.cpp
-
6kicad/pcm/pcm.h
-
5thirdparty/json_schema_validator/CMakeLists.txt
-
1thirdparty/json_schema_validator/json-schema-draft7.json.cpp
-
1thirdparty/json_schema_validator/nlohmann/json-schema.hpp
@ -0,0 +1,108 @@ |
|||
{ |
|||
"$schema": "http://json-schema.org/draft-07/schema#", |
|||
"$id": "https://go.kicad.org/api/schemas/v1", |
|||
"title": "KiCad API Plugin Schema", |
|||
"description": "KiCad IPC API Plugin and Action schema", |
|||
"$ref": "#/definitions/Plugin", |
|||
"definitions": { |
|||
"Plugin": { |
|||
"type": "object", |
|||
"properties": { |
|||
"identifier": { |
|||
"type": "string", |
|||
"description": "Plugin identifier", |
|||
"pattern": "^[a-zA-Z][-_a-zA-Z0-9.]{0,98}[a-zA-Z0-9]$" |
|||
}, |
|||
"name": { |
|||
"type": "string", |
|||
"maxLength": 200 |
|||
}, |
|||
"description": { |
|||
"type": "string", |
|||
"maxLength": 500 |
|||
}, |
|||
"runtime": { |
|||
"type": "object", |
|||
"properties": { |
|||
"type": { |
|||
"type": "string", |
|||
"enum": [ |
|||
"python", |
|||
"exec" |
|||
], |
|||
"description": "How KiCad should launch the plugin" |
|||
}, |
|||
"min_version": { |
|||
"type": "string" |
|||
} |
|||
}, |
|||
"required": [ |
|||
"type" |
|||
] |
|||
}, |
|||
"actions": { |
|||
"type": "array", |
|||
"items": { |
|||
"type": "object", |
|||
"properties": { |
|||
"identifier": { |
|||
"type": "string" |
|||
}, |
|||
"name": { |
|||
"type": "string" |
|||
}, |
|||
"description": { |
|||
"type": "string" |
|||
}, |
|||
"show-button": { |
|||
"type": "boolean" |
|||
}, |
|||
"scopes": { |
|||
"type": "array", |
|||
"items": { |
|||
"type": "string", |
|||
"enum": [ |
|||
"pcb", |
|||
"schematic", |
|||
"footprint", |
|||
"symbol", |
|||
"project_manager" |
|||
] |
|||
} |
|||
}, |
|||
"entrypoint": { |
|||
"type": "string" |
|||
}, |
|||
"icons-light": { |
|||
"type": "array", |
|||
"items": { |
|||
"type": "string" |
|||
} |
|||
}, |
|||
"icons-dark": { |
|||
"type": "array", |
|||
"items": { |
|||
"type": "string" |
|||
} |
|||
} |
|||
}, |
|||
"required": [ |
|||
"identifier", |
|||
"name", |
|||
"description", |
|||
"show-button", |
|||
"entrypoint" |
|||
] |
|||
} |
|||
} |
|||
}, |
|||
"required": [ |
|||
"identifier", |
|||
"name", |
|||
"description", |
|||
"runtime", |
|||
"actions" |
|||
] |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,64 @@ |
|||
/*
|
|||
* This program source code file is part of KiCad, a free EDA CAD application. |
|||
* |
|||
* Copyright The KiCad Developers, see AUTHORS.txt for contributors. |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify it |
|||
* under the terms of the GNU General Public License as published by the |
|||
* Free Software Foundation, either version 3 of the License, or (at your |
|||
* option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, but |
|||
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|||
* General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License along |
|||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
|
|||
#include <fstream>
|
|||
#include <wx/filename.h>
|
|||
#include <wx/log.h>
|
|||
|
|||
#include <json_schema_validator.h>
|
|||
#include <locale_io.h>
|
|||
|
|||
|
|||
JSON_SCHEMA_VALIDATOR::JSON_SCHEMA_VALIDATOR( const wxFileName& aSchemaFile ) |
|||
{ |
|||
std::ifstream schema_stream( aSchemaFile.GetFullPath().fn_str() ); |
|||
nlohmann::json schema; |
|||
|
|||
try |
|||
{ |
|||
// For some obscure reason on MINGW, using UCRT option,
|
|||
// m_schema_validator.set_root_schema() hangs without switching to locale "C"
|
|||
#if defined(__MINGW32__) && defined(_UCRT)
|
|||
LOCALE_IO dummy; |
|||
#endif
|
|||
|
|||
schema_stream >> schema; |
|||
m_validator.set_root_schema( schema ); |
|||
} |
|||
catch( std::exception& e ) |
|||
{ |
|||
if( !aSchemaFile.FileExists() ) |
|||
{ |
|||
wxLogError( wxString::Format( _( "schema file '%s' not found" ), |
|||
aSchemaFile.GetFullPath() ) ); |
|||
} |
|||
else |
|||
{ |
|||
wxLogError( wxString::Format( _( "Error loading schema: %s" ), e.what() ) ); |
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
nlohmann::json JSON_SCHEMA_VALIDATOR::Validate( const nlohmann::json& aJson, |
|||
nlohmann::json_schema::error_handler& aErrorHandler, |
|||
const nlohmann::json_uri& aInitialUri ) const |
|||
{ |
|||
return m_validator.validate( aJson, aErrorHandler, aInitialUri ); |
|||
} |
|||
@ -0,0 +1,43 @@ |
|||
/* |
|||
* This program source code file is part of KiCad, a free EDA CAD application. |
|||
* |
|||
* Copyright The KiCad Developers, see AUTHORS.txt for contributors. |
|||
* |
|||
* This program is free software: you can redistribute it and/or modify it |
|||
* under the terms of the GNU General Public License as published by the |
|||
* Free Software Foundation, either version 3 of the License, or (at your |
|||
* option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, but |
|||
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|||
* General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License along |
|||
* with this program. If not, see <http://www.gnu.org/licenses/>. |
|||
*/ |
|||
|
|||
#ifndef JSON_SCHEMA_VALIDATOR_H |
|||
#define JSON_SCHEMA_VALIDATOR_H |
|||
|
|||
#include <wx/filename.h> |
|||
#include <kicommon.h> |
|||
#include <json_common.h> |
|||
#include <nlohmann/json-schema.hpp> |
|||
|
|||
class KICOMMON_API JSON_SCHEMA_VALIDATOR |
|||
{ |
|||
public: |
|||
JSON_SCHEMA_VALIDATOR( const wxFileName& aSchemaFile ); |
|||
|
|||
~JSON_SCHEMA_VALIDATOR() = default; |
|||
|
|||
nlohmann::json Validate( const nlohmann::json& aJson, |
|||
nlohmann::json_schema::error_handler& aErrorHandler, |
|||
const nlohmann::json_uri& aInitialUri = nlohmann::json_uri("#") ) const; |
|||
|
|||
private: |
|||
nlohmann::json_schema::json_validator m_validator; |
|||
}; |
|||
|
|||
#endif //JSON_SCHEMA_VALIDATOR_H |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue