From 0b04da641967e25396705ba1a416d0c8f4b6c23d Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Wed, 11 Mar 2020 00:56:14 +0100 Subject: [PATCH 1/2] snake case for addon_schema --- qt/aqt/addons.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/qt/aqt/addons.py b/qt/aqt/addons.py index e8788ee7b..9495187fa 100644 --- a/qt/aqt/addons.py +++ b/qt/aqt/addons.py @@ -601,11 +601,11 @@ and have been disabled: %(found)s" # Schema ###################################################################### - def _addonSchemaPath(self, dir): + def _addon_schema_path(self, dir): return os.path.join(self.addonsFolder(dir), "config.schema.json") - def _addonSchema(self, dir): - path = self._addonSchemaPath(dir) + def _addon_schema(self, dir): + path = self._addon_schema_path(dir) try: if not os.path.exists(path): # True is a schema accepting everything @@ -1328,7 +1328,7 @@ 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)) + jsonschema.validate(new_conf, self.parent().mgr._addon_schema(self.addon)) except ValidationError as e: # The user did edit the configuration and entered a value # which can not be interpreted. From 5482cafef22252ce788e94d82f8ce010ea756fb9 Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Wed, 11 Mar 2020 01:03:52 +0100 Subject: [PATCH 2/2] Allow add-on dev to show personalized message. The error should be in "error_msg" They can show: * the original error message with {problem} * the path of the error with {path}, * the value which does not match against the subchema with {error} * the subschema against which the error occurs with {schema} Thanks to Glutanimate for the idea https://github.com/ankitects/anki/pull/495#issuecomment-596685227 --- qt/aqt/addons.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/qt/aqt/addons.py b/qt/aqt/addons.py index 9495187fa..1d9db284a 100644 --- a/qt/aqt/addons.py +++ b/qt/aqt/addons.py @@ -1332,14 +1332,26 @@ class ConfigEditor(QDialog): except ValidationError as e: # The user did edit the configuration and entered a value # which can not be interpreted. - showInfo( - tr( + schema = e.schema + erroneous_conf = new_conf + for link in e.path: + erroneous_conf = erroneous_conf[link] + path = "/".join(str(path) for path in e.path) + if "error_msg" in schema: + msg = schema["error_msg"].format( + problem=e.message, + path=path, + schema=str(schema), + erroneous_conf=erroneous_conf, + ) + else: + msg = tr( TR.ADDONS_CONFIG_VALIDATION_ERROR, problem=e.message, - path="/".join(str(path) for path in e.path), - schema=str(e.schema), + path=path, + schema=str(schema), ) - ) + showInfo(msg) return except Exception as e: showInfo(_("Invalid configuration: ") + repr(e))