add-on config check schema of user config
This commit is contained in:
parent
c35afd4aaa
commit
01c74bc015
@ -594,6 +594,24 @@ and have been disabled: %(found)s"
|
|||||||
def configUpdatedAction(self, addon: str) -> Callable[[Any], None]:
|
def configUpdatedAction(self, addon: str) -> Callable[[Any], None]:
|
||||||
return self._configUpdatedActions.get(addon)
|
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
|
# Add-on Config API
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
@ -1304,6 +1322,12 @@ class ConfigEditor(QDialog):
|
|||||||
txt = gui_hooks.addon_config_editor_will_save_json(txt)
|
txt = gui_hooks.addon_config_editor_will_save_json(txt)
|
||||||
try:
|
try:
|
||||||
new_conf = json.loads(txt)
|
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:
|
except Exception as e:
|
||||||
showInfo(_("Invalid configuration: ") + repr(e))
|
showInfo(_("Invalid configuration: ") + repr(e))
|
||||||
return
|
return
|
||||||
|
@ -6,3 +6,4 @@ addons-failed-to-load =
|
|||||||
{$traceback}
|
{$traceback}
|
||||||
# Shown in the add-on configuration screen (Tools>Add-ons>Config), in the title bar
|
# Shown in the add-on configuration screen (Tools>Add-ons>Config), in the title bar
|
||||||
addons-config-window-title = Configure '{$name}'
|
addons-config-window-title = Configure '{$name}'
|
||||||
|
addons-config-validation-error = There was a problem with the provided configuration: {$problem}
|
Loading…
Reference in New Issue
Block a user