Integrity check for the configuration parser

This commit is contained in:
Marco Dalla Tiezza
2024-06-06 22:34:51 +02:00
parent c7c53b5a68
commit 7db68ab2ad
4 changed files with 50 additions and 19 deletions

View File

@@ -6,7 +6,7 @@ from artemis.utils.sys_utils import copy_file
class Config(ConfigParser):
""" Custom configuration class derived from ConfigParser.
Used to get, set, save and remove any configuration from the conf file
Used to get value, set, save and remove any configuration from the conf file
"""
def __init__(self, config_file_path, space_around_delimiters=False):
@@ -15,11 +15,13 @@ class Config(ConfigParser):
self.read(self._config_file_path)
self._space_around_delimiters = space_around_delimiters
def get_or_default(self, section, option, default_value):
value = super().get(section, option)
return value if value else default_value
def value(self, section, option, default_value):
value = super().get(section, option, fallback=default_value)
return value
def set(self, section, option, value=None):
if not self.has_section(section):
self.add_section(section)
super().set(section, option, value)
self.save()
@@ -32,13 +34,42 @@ class Config(ConfigParser):
self.write(f, space_around_delimiters=self._space_around_delimiters)
def prepare_qt_conf():
if not (PREFERENCES_DIR / 'qtquickcontrols2.conf').exists():
copy_file(
BASE_DIR / 'config' / 'qtquickcontrols2.conf',
PREFERENCES_DIR / 'qtquickcontrols2.conf'
)
def merge_config_files(old_config_path, template_config_path):
""" Merge two configuration files: if the old one lacks some
sections or options from a comparison with a template,
this function will add what is missing to the old conf file
"""
old_config = ConfigParser()
old_config.read(old_config_path)
new_config = ConfigParser()
new_config.read(template_config_path)
for section in new_config.sections():
if not old_config.has_section(section):
old_config.add_section(section)
for option in new_config.options(section):
if not old_config.has_option(section, option):
old_config.set(section, option, new_config.get(section, option))
with open(old_config_path, 'w') as f:
old_config.write(f)
prepare_qt_conf()
def check_conf_file():
""" Check the integrity of the used conf file.
If it is not present it will add a copy to the PREF_DIR
and if it is different in structure (different section/options)
it will merge the conf file with the new template one
"""
active_conf = PREFERENCES_DIR / 'qtquickcontrols2.conf'
template_conf = BASE_DIR / 'config' / 'qtquickcontrols2.conf'
if not active_conf.exists():
copy_file(template_conf, active_conf)
else:
merge_config_files(active_conf, template_conf)
check_conf_file()
CONFIGURE_QT = Config((PREFERENCES_DIR / 'qtquickcontrols2.conf').resolve().as_posix())

View File

@@ -5,10 +5,10 @@ from artemis.utils.config_utils import CONFIGURE_QT
def set_ui():
os.environ['QT_QUICK_CONTROLS_STYLE'] = CONFIGURE_QT.get_or_default('Controls', 'style', 'Material')
os.environ['QT_QUICK_CONTROLS_MATERIAL_VARIANT'] = CONFIGURE_QT.get_or_default('Material', 'variant', 'Dense')
os.environ['QT_QUICK_CONTROLS_MATERIAL_THEME'] = CONFIGURE_QT.get_or_default('Material', 'theme', 'System')
os.environ['QT_QUICK_CONTROLS_MATERIAL_ACCENT'] = CONFIGURE_QT.get_or_default('Material', 'accent', 'Green')
os.environ['QT_QUICK_CONTROLS_STYLE'] = CONFIGURE_QT.value('Controls', 'style', 'Material')
os.environ['QT_QUICK_CONTROLS_MATERIAL_VARIANT'] = CONFIGURE_QT.value('Material', 'variant', 'Dense')
os.environ['QT_QUICK_CONTROLS_MATERIAL_THEME'] = CONFIGURE_QT.value('Material', 'theme', 'System')
os.environ['QT_QUICK_CONTROLS_MATERIAL_ACCENT'] = CONFIGURE_QT.value('Material', 'accent', 'Green')
if is_windows():
os.environ['QSG_RHI_BACKEND'] = 'opengl'