apps/config panels: move the computation of the actual 'bind' value to core to avoid having an epic python snippets in the bash code..

This commit is contained in:
Alexandre Aubin 2024-08-27 13:11:32 +02:00
parent 7c79060467
commit a6785d34bc
3 changed files with 53 additions and 113 deletions

View file

@ -126,60 +126,9 @@ _ynh_app_config_apply_one() {
fi fi
fi fi
} }
_ynh_app_config_get() { _ynh_app_config_get() {
# From settings for line in $YNH_APP_CONFIG_PANEL_OPTIONS_TYPES_AND_BINDS; do
local lines
lines=$(
python3 <<EOL
import toml
from collections import OrderedDict
with open("../config_panel.toml", "r") as f:
file_content = f.read()
loaded_toml = toml.loads(file_content, _dict=OrderedDict)
for panel_name, panel in loaded_toml.items():
if not isinstance(panel, dict): continue
bind_panel = panel.get('bind')
for section_name, section in panel.items():
if not isinstance(section, dict): continue
bind_section = section.get('bind')
if not bind_section:
bind_section = bind_panel
elif bind_section[-1] == ":" and bind_panel and ":" in bind_panel:
regex, bind_panel_file = bind_panel.split(":")
if ">" in bind_section:
bind_section = bind_section + bind_panel_file
else:
bind_section = regex + bind_section + bind_panel_file
for name, param in section.items():
if not isinstance(param, dict):
continue
bind = param.get('bind')
if not bind:
if bind_section:
bind = bind_section
else:
bind = 'settings'
elif bind[-1] == ":" and bind_section and ":" in bind_section:
regex, bind_file = bind_section.split(":")
if ">" in bind:
bind = bind + bind_file
else:
bind = regex + bind + bind_file
if bind == "settings" and param.get('type', 'string') == 'file':
bind = 'null'
print('|'.join([
name,
param.get('type', 'string'),
bind
]))
EOL
)
for line in $lines; do
# Split line into short_setting, type and bind # Split line into short_setting, type and bind
IFS='|' read short_setting type bind <<<"$line" IFS='|' read short_setting type bind <<<"$line"
binds[${short_setting}]="$bind" binds[${short_setting}]="$bind"
@ -188,7 +137,6 @@ EOL
formats[${short_setting}]="" formats[${short_setting}]=""
ynh_app_config_get_one $short_setting $type $bind ynh_app_config_get_one $short_setting $type $bind
done done
} }
_ynh_app_config_apply() { _ynh_app_config_apply() {

View file

@ -135,60 +135,9 @@ _ynh_app_config_apply_one() {
fi fi
fi fi
} }
_ynh_app_config_get() { _ynh_app_config_get() {
# From settings for line in $YNH_APP_CONFIG_PANEL_OPTIONS_TYPES_AND_BINDS; do
local lines
lines=$(
python3 <<EOL
import toml
from collections import OrderedDict
with open("../config_panel.toml", "r") as f:
file_content = f.read()
loaded_toml = toml.loads(file_content, _dict=OrderedDict)
for panel_name, panel in loaded_toml.items():
if not isinstance(panel, dict): continue
bind_panel = panel.get('bind')
for section_name, section in panel.items():
if not isinstance(section, dict): continue
bind_section = section.get('bind')
if not bind_section:
bind_section = bind_panel
elif bind_section[-1] == ":" and bind_panel and ":" in bind_panel:
regex, bind_panel_file = bind_panel.split(":")
if ">" in bind_section:
bind_section = bind_section + bind_panel_file
else:
bind_section = regex + bind_section + bind_panel_file
for name, param in section.items():
if not isinstance(param, dict):
continue
bind = param.get('bind')
if not bind:
if bind_section:
bind = bind_section
else:
bind = 'settings'
elif bind[-1] == ":" and bind_section and ":" in bind_section:
regex, bind_file = bind_section.split(":")
if ">" in bind:
bind = bind + bind_file
else:
bind = regex + bind + bind_file
if bind == "settings" and param.get('type', 'string') == 'file':
bind = 'null'
print('|'.join([
name,
param.get('type', 'string'),
bind
]))
EOL
)
for line in $lines; do
# Split line into short_setting, type and bind # Split line into short_setting, type and bind
IFS='|' read short_setting type bind <<<"$line" IFS='|' read short_setting type bind <<<"$line"
binds[${short_setting}]="$bind" binds[${short_setting}]="$bind"
@ -197,7 +146,6 @@ EOL
formats[${short_setting}]="" formats[${short_setting}]=""
ynh_app_config_get_one $short_setting $type $bind ynh_app_config_get_one $short_setting $type $bind
done done
} }
_ynh_app_config_apply() { _ynh_app_config_apply() {

View file

@ -1998,6 +1998,7 @@ ynh_app_config_run $1
"install_dir": settings.get("install_dir", ""), "install_dir": settings.get("install_dir", ""),
"YNH_APP_BASEDIR": os.path.join(APPS_SETTING_PATH, app), "YNH_APP_BASEDIR": os.path.join(APPS_SETTING_PATH, app),
"YNH_APP_PACKAGING_FORMAT": str(manifest["packaging_format"]), "YNH_APP_PACKAGING_FORMAT": str(manifest["packaging_format"]),
"YNH_APP_CONFIG_PANEL_OPTIONS_TYPES_AND_BINDS": self._dump_options_types_and_binds(),
} }
) )
app_script_env = _make_environment_for_app_script(app) app_script_env = _make_environment_for_app_script(app)
@ -2006,6 +2007,7 @@ ynh_app_config_run $1
app_script_env.update(env) app_script_env.update(env)
env = app_script_env env = app_script_env
ret, values = hook_exec(config_script, args=[action], env=env) ret, values = hook_exec(config_script, args=[action], env=env)
if ret != 0: if ret != 0:
if action == "show": if action == "show":
@ -2020,14 +2022,56 @@ ynh_app_config_run $1
ret = super()._get_config_panel() ret = super()._get_config_panel()
settings = _get_app_settings(self.entity) self._compute_binds()
for _, _, option in self._iterate():
if "bind" in option:
option["bind"] = _hydrate_app_template(option["bind"], settings)
return ret return ret
def _compute_binds(self):
"""
This compute the 'bind' statement for every option
In particular to handle __FOOBAR__ syntax
and to handle the fact that bind statements may be defined panel-wide or section-wide
"""
settings = _get_app_settings(self.entity)
for panel, section, option in self._iterate():
bind_panel = panel.get('bind')
bind_section = section.get('bind')
if not bind_section:
bind_section = bind_panel
elif bind_section[-1] == ":" and bind_panel and ":" in bind_panel:
selector, bind_panel_file = bind_panel.split(":")
if ">" in bind_section:
bind_section = bind_section + bind_panel_file
else:
bind_section = selector + bind_section + bind_panel_file
bind = option.get('bind')
if not bind:
if bind_section:
bind = bind_section
else:
bind = 'settings'
elif bind[-1] == ":" and bind_section and ":" in bind_section:
selector, bind_file = bind_section.split(":")
if ">" in bind:
bind = bind + bind_file
else:
bind = selector + bind + bind_file
if bind == "settings" and option.get('type', 'string') == 'file':
bind = 'null'
option["bind"] = _hydrate_app_template(bind, settings)
def _dump_options_types_and_binds(self):
lines = []
for _, _, option in self._iterate():
lines.append('|'.join([option['id'], option.get('type', 'string'), option["bind"]]))
return '\n'.join(lines)
app_settings_cache: Dict[str, Dict[str, Any]] = {} app_settings_cache: Dict[str, Dict[str, Any]] = {}
app_settings_cache_timestamp: Dict[str, int] = {} app_settings_cache_timestamp: Dict[str, int] = {}