configpanel: update set

This commit is contained in:
axolotle 2023-04-18 21:04:05 +02:00
parent 5f9ea58313
commit 6b3691ce53
2 changed files with 31 additions and 20 deletions

View file

@ -132,7 +132,7 @@ class SettingsConfigPanel(ConfigPanel):
entity_type = "global" entity_type = "global"
save_path_tpl = SETTINGS_PATH save_path_tpl = SETTINGS_PATH
save_mode = "diff" save_mode = "diff"
virtual_settings = ["root_password", "root_password_confirm", "passwordless_sudo"] virtual_settings = {"root_password", "root_password_confirm", "passwordless_sudo"}
def __init__(self, config_path=None, save_path=None, creation=False): def __init__(self, config_path=None, save_path=None, creation=False):
super().__init__("settings") super().__init__("settings")

View file

@ -50,6 +50,7 @@ if TYPE_CHECKING:
from pydantic.typing import AbstractSetIntStr, MappingIntStrAny from pydantic.typing import AbstractSetIntStr, MappingIntStrAny
from yunohost.utils.form import FormModel, Hooks from yunohost.utils.form import FormModel, Hooks
from yunohost.log import OperationLogger
logger = getLogger("yunohost.configpanel") logger = getLogger("yunohost.configpanel")
@ -390,15 +391,15 @@ class ConfigPanel:
return result return result
def set( def set(
self, key=None, value=None, args=None, args_file=None, operation_logger=None self,
key: Union[str, None] = None,
value: Any = None,
args: Union[str, None] = None,
args_file: Union[str, None] = None,
operation_logger: Union["OperationLogger", None] = None,
): ):
self.filter_key = key or "" self.filter_key = parse_filter_key(key)
panel_id, section_id, option_id = self.filter_key
# Read config panel toml
self._get_config_panel()
if not self.config:
raise YunohostValidationError("config_no_panel")
if (args is not None or args_file is not None) and value is not None: if (args is not None or args_file is not None) and value is not None:
raise YunohostValidationError( raise YunohostValidationError(
@ -406,27 +407,35 @@ class ConfigPanel:
raw_msg=True, raw_msg=True,
) )
if self.filter_key.count(".") != 2 and value is not None: if not option_id and value is not None:
raise YunohostValidationError("config_cant_set_value_on_section") raise YunohostValidationError("config_cant_set_value_on_section")
# Import and parse pre-answered options # Import and parse pre-answered options
logger.debug("Import and parse pre-answered options") logger.debug("Import and parse pre-answered options")
if option_id and value is not None: if option_id and value is not None:
self.args = {option_id: value} prefilled_answers = {option_id: value}
else: else:
self.args = parse_prefilled_values(args, value, args_file) prefilled_answers = parse_prefilled_values(args, args_file)
# Read or get values and hydrate the config self.config, self.form = self._get_config_panel()
self._get_raw_settings() # FIXME find a better way to exclude previous settings
self._hydrate() previous_settings = self.form.dict()
BaseOption.operation_logger = operation_logger
self._ask() # FIXME Not sure if this is need (redact call to operation logger does it on all the instances)
# BaseOption.operation_logger = operation_logger
self.form = self._ask(
self.config,
self.form,
prefilled_answers=prefilled_answers,
hooks=self.hooks,
)
if operation_logger: if operation_logger:
operation_logger.start() operation_logger.start()
try: try:
self._apply() self._apply(self.form, previous_settings)
except YunohostError: except YunohostError:
raise raise
# Script got manually interrupted ... # Script got manually interrupted ...
@ -452,6 +461,8 @@ class ConfigPanel:
self._reload_services() self._reload_services()
logger.success("Config updated as expected") logger.success("Config updated as expected")
if operation_logger:
operation_logger.success() operation_logger.success()
def list_actions(self): def list_actions(self):
@ -625,7 +636,7 @@ class ConfigPanel:
return (config, settings) return (config, settings)
def ask( def _ask(
self, self,
config: ConfigPanelModel, config: ConfigPanelModel,
settings: "FormModel", settings: "FormModel",