From f31e2524361b1ecf6409099e4e5550e2fd6c959a Mon Sep 17 00:00:00 2001 From: Patrick Wu Date: Wed, 29 Sep 2021 00:37:26 +0800 Subject: [PATCH] system_setup: reduce code duplication in getting default value --- system_setup/common/{conf.py => wsl_conf.py} | 50 ++++++++++++++++++- system_setup/common/wsl_utils.py | 33 ------------ system_setup/server/controllers/configure.py | 2 +- .../server/controllers/wslconfadvanced.py | 28 ++--------- .../server/controllers/wslconfbase.py | 17 ++----- 5 files changed, 58 insertions(+), 72 deletions(-) rename system_setup/common/{conf.py => wsl_conf.py} (67%) diff --git a/system_setup/common/conf.py b/system_setup/common/wsl_conf.py similarity index 67% rename from system_setup/common/conf.py rename to system_setup/common/wsl_conf.py index 6c283d5f..1dc9e428 100644 --- a/system_setup/common/conf.py +++ b/system_setup/common/wsl_conf.py @@ -19,8 +19,56 @@ import logging from configparser import ConfigParser +from os import path -log = logging.getLogger("system_setup.common.conf") +log = logging.getLogger("system_setup.common.wsl_conf") + +config_ref = { + "wsl": { + "automount": { + "enabled": "automount", + "mountfstab": "mountfstab", + "root": "custom_path", + "options": "custom_mount_opt", + }, + "network": { + "generatehosts": "gen_host", + "generateresolvconf": "gen_resolvconf", + }, + "interop": { + "enabled": "interop_enabled", + "appendwindowspath": "interop_appendwindowspath", + } + }, + "ubuntu": { + "GUI": { + "theme": "gui_theme", + "followwintheme": "gui_followwintheme", + }, + "Interop": { + "guiintegration": "legacy_gui", + "audiointegration": "legacy_audio", + "advancedipdetection": "adv_ip_detect", + }, + "Motd": { + "wslnewsenabled": "wsl_motd_news", + } + } +} + + +def wsl_config_loader(data, pathname, id): + if path.exists(pathname): + config = ConfigParser() + config.read(pathname) + for conf_sec in config: + if conf_sec in config_ref[id]: + conf_sec_list = config[conf_sec] + for conf_item in conf_sec_list: + if conf_item in config_ref[id][conf_sec]: + data[config_ref[id][conf_sec][conf_item]] = \ + conf_sec_list[conf_item] + return data class WSLConfig: diff --git a/system_setup/common/wsl_utils.py b/system_setup/common/wsl_utils.py index 0dcadfe3..f6a393ad 100644 --- a/system_setup/common/wsl_utils.py +++ b/system_setup/common/wsl_utils.py @@ -20,39 +20,6 @@ import subprocess log = logging.getLogger("subiquity.system_setup.common.wsl_utils") -config_ref = { - "wsl": { - "automount": { - "enabled": "automount", - "mountfstab": "mountfstab", - "root": "custom_path", - "options": "custom_mount_opt", - }, - "network": { - "generatehosts": "gen_host", - "generateresolvconf": "gen_resolvconf", - }, - "interop": { - "enabled": "interop_enabled", - "appendwindowspath": "interop_appendwindowspath", - } - }, - "ubuntu": { - "GUI": { - "theme": "gui_theme", - "followwintheme": "gui_followwintheme", - }, - "Interop": { - "guiintegration": "legacy_gui", - "audiointegration": "legacy_audio", - "advancedipdetection": "adv_ip_detect", - }, - "Motd": { - "wslnewsenabled": "wsl_motd_news", - } - } -} - def is_reconfigure(is_dryrun): if is_dryrun and \ diff --git a/system_setup/server/controllers/configure.py b/system_setup/server/controllers/configure.py index 0e4525f2..721a99ed 100644 --- a/system_setup/server/controllers/configure.py +++ b/system_setup/server/controllers/configure.py @@ -21,7 +21,7 @@ from subiquity.common.types import ApplicationState from subiquity.server.controller import SubiquityController from subiquitycore.context import with_context from subiquitycore.utils import run_command -from system_setup.common.conf import WSLConfigHandler +from system_setup.common.wsl_conf import WSLConfigHandler log = logging.getLogger("system_setup.server.controllers.configure") diff --git a/system_setup/server/controllers/wslconfadvanced.py b/system_setup/server/controllers/wslconfadvanced.py index b47bdeb0..15b2b3b1 100644 --- a/system_setup/server/controllers/wslconfadvanced.py +++ b/system_setup/server/controllers/wslconfadvanced.py @@ -16,15 +16,13 @@ import logging import attr -from os import path -import configparser from subiquitycore.context import with_context from subiquity.common.apidef import API from subiquity.common.types import WSLConfigurationAdvanced from subiquity.server.controller import SubiquityController -from system_setup.common.wsl_utils import config_ref +from system_setup.common.wsl_conf import wsl_config_loader log = logging.getLogger( 'system_setup.server.controllers.wsl_configuration_advanced') @@ -57,26 +55,10 @@ class WSLConfigurationAdvancedController(SubiquityController): # load the config file data = {} - if path.exists('/etc/wsl.conf'): - wslconfig = configparser.ConfigParser() - wslconfig.read('/etc/wsl.conf') - for conf_sec in wslconfig: - if conf_sec in config_ref['wsl']: - conf_sec_list = wslconfig[conf_sec] - for conf_item in conf_sec_list: - if conf_item in config_ref['wsl'][conf_sec]: - data[config_ref['wsl'][conf_sec][conf_item]] = \ - conf_sec_list[conf_item] - if path.exists('/etc/ubuntu-wsl.conf'): - ubuntuconfig = configparser.ConfigParser() - ubuntuconfig.read('/etc/ubuntu-wsl.conf') - for conf_sec in ubuntuconfig: - if conf_sec in config_ref['ubuntu']: - conf_sec_list = ubuntuconfig[conf_sec] - for conf_item in conf_sec_list: - if conf_item in config_ref['ubuntu'][conf_sec]: - data[config_ref['ubuntu'][conf_sec][conf_item]] = \ - conf_sec_list[conf_item] + + data = wsl_config_loader(data, "/etc/wsl.conf", "wsl") + data = wsl_config_loader(data, "/etc/ubuntu-wsl.conf", "ubuntu") + if data: def bool_converter(x): return x.lower() == 'true' diff --git a/system_setup/server/controllers/wslconfbase.py b/system_setup/server/controllers/wslconfbase.py index b33bc9ee..4fe24a64 100644 --- a/system_setup/server/controllers/wslconfbase.py +++ b/system_setup/server/controllers/wslconfbase.py @@ -13,10 +13,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -import configparser import logging -from os import path - import attr from subiquitycore.context import with_context @@ -25,7 +22,7 @@ from subiquity.common.apidef import API from subiquity.common.types import WSLConfigurationBase from subiquity.server.controller import SubiquityController -from system_setup.common.wsl_utils import config_ref +from system_setup.common.wsl_conf import wsl_config_loader log = logging.getLogger('system_setup.server' + '.controllers.wsl_configuration_base') @@ -53,16 +50,8 @@ class WSLConfigurationBaseController(SubiquityController): # load the config file data = {} - if path.exists('/etc/wsl.conf'): - wslconfig = configparser.ConfigParser() - wslconfig.read('/etc/wsl.conf') - for conf_sec in wslconfig: - if conf_sec in config_ref['wsl']: - conf_sec_list = wslconfig[conf_sec] - for conf_item in conf_sec_list: - if conf_item in config_ref['wsl'][conf_sec]: - data[config_ref['wsl'][conf_sec][conf_item]] = \ - conf_sec_list[conf_item] + data = wsl_config_loader(data, "/etc/wsl.conf", "wsl") + if data: def bool_converter(x): return x.lower() == 'true'