Merge pull request #495 from Arthur-Milchior/check_schema_before_accepting

add-on config check schema of user config
This commit is contained in:
Damien Elmes 2020-03-06 21:14:47 +10:00 committed by GitHub
commit 5f4839a901
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -598,6 +598,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
######################################################################
@ -1308,6 +1326,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}