diff --git a/artemis/ui/artemis.py b/artemis/ui/artemis.py index 8499c1f..b396de6 100644 --- a/artemis/ui/artemis.py +++ b/artemis/ui/artemis.py @@ -336,7 +336,7 @@ class UIArtemis(QObject): def autoload_db(self): sig_id_path = DATA_DIR / 'SigID' / Constants.SQL_NAME - autoload = CONFIGURE_QT.get_or_default("Database", "autoload", 0) + autoload = CONFIGURE_QT.value("Database", "autoload", 0) if sig_id_path.exists() and int(autoload): self.load_db('SigID') diff --git a/artemis/ui/preferences.py b/artemis/ui/preferences.py index d1ca0c6..412e978 100644 --- a/artemis/ui/preferences.py +++ b/artemis/ui/preferences.py @@ -40,9 +40,9 @@ class UIPreferences(QObject): def load_preferences_ui(self): """ Loading all the initial preferences from the conf file to the UI """ - self.load_material_accent.emit(CONFIGURE_QT.get_or_default("Material", "Accent", "Green")) - self.load_material_theme.emit(CONFIGURE_QT.get_or_default("Material", "Theme", "System")) - self.load_autoload.emit(int(CONFIGURE_QT.get_or_default("Database", "autoload", 0))) + self.load_material_accent.emit(CONFIGURE_QT.value("Material", "Accent", "Green")) + self.load_material_theme.emit(CONFIGURE_QT.value("Material", "Theme", "System")) + self.load_autoload.emit(int(CONFIGURE_QT.value("Database", "autoload", 0))) self.show_ui.emit() diff --git a/artemis/utils/config_utils.py b/artemis/utils/config_utils.py index 52d8b87..67e7dbf 100644 --- a/artemis/utils/config_utils.py +++ b/artemis/utils/config_utils.py @@ -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()) diff --git a/artemis/utils/ui_utils.py b/artemis/utils/ui_utils.py index 34567b4..bb3d560 100644 --- a/artemis/utils/ui_utils.py +++ b/artemis/utils/ui_utils.py @@ -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'