system_setup: reduce code duplication in getting default value

This commit is contained in:
Patrick Wu 2021-09-29 00:37:26 +08:00
parent 225cb1bd37
commit f31e252436
5 changed files with 58 additions and 72 deletions

View File

@ -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:

View File

@ -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 \

View File

@ -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")

View File

@ -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'

View File

@ -13,10 +13,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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'