add-on config check schema of user config

This commit is contained in:
Arthur Milchior 2020-03-05 00:19:09 +01:00
parent c35afd4aaa
commit 01c74bc015
2 changed files with 25 additions and 0 deletions

View File

@ -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

View File

@ -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}