From 01c74bc01562f1a8442eace84cf7e05d546885f1 Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Thu, 5 Mar 2020 00:19:09 +0100 Subject: [PATCH] add-on config check schema of user config --- qt/aqt/addons.py | 24 ++++++++++++++++++++++++ qt/ftl/addons.ftl | 1 + 2 files changed, 25 insertions(+) diff --git a/qt/aqt/addons.py b/qt/aqt/addons.py index ecd7c56dd..af27ec994 100644 --- a/qt/aqt/addons.py +++ b/qt/aqt/addons.py @@ -594,6 +594,24 @@ and have been disabled: %(found)s" def configUpdatedAction(self, addon: str) -> Callable[[Any], None]: return self._configUpdatedActions.get(addon) + # Schema + ###################################################################### + + def _addonSchemaPath(self, dir): + return os.path.join(self.addonsFolder(dir), "config.schema.json") + + def _addonSchema(self, dir): + path = self._addonSchemaPath(dir) + try: + if not os.path.exists(path): + # True is a schema accepting everything + return True + with open(path, encoding="utf-8") as f: + return json.load(f) + except json.decoder.JSONDecodeError as e: + print("The schema is not valid:") + print(e) + # Add-on Config API ###################################################################### @@ -1304,6 +1322,12 @@ class ConfigEditor(QDialog): txt = gui_hooks.addon_config_editor_will_save_json(txt) try: new_conf = json.loads(txt) + jsonschema.validate(new_conf, self.parent().mgr._addonSchema(self.addon)) + except ValidationError as e: + # The user did edit the configuration and entered a value + # which can not be interpreted. + showInfo(tr(TR.ADDONS_CONFIG_VALIDATION_ERROR, problem=e.message)) + return except Exception as e: showInfo(_("Invalid configuration: ") + repr(e)) return diff --git a/qt/ftl/addons.ftl b/qt/ftl/addons.ftl index 14ed8a491..4ba2ddb93 100644 --- a/qt/ftl/addons.ftl +++ b/qt/ftl/addons.ftl @@ -6,3 +6,4 @@ addons-failed-to-load = {$traceback} # Shown in the add-on configuration screen (Tools>Add-ons>Config), in the title bar addons-config-window-title = Configure '{$name}' +addons-config-validation-error = There was a problem with the provided configuration: {$problem} \ No newline at end of file