Integrity check for the configuration parser
This commit is contained in:
@@ -336,7 +336,7 @@ class UIArtemis(QObject):
|
|||||||
|
|
||||||
def autoload_db(self):
|
def autoload_db(self):
|
||||||
sig_id_path = DATA_DIR / 'SigID' / Constants.SQL_NAME
|
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):
|
if sig_id_path.exists() and int(autoload):
|
||||||
self.load_db('SigID')
|
self.load_db('SigID')
|
||||||
|
|
||||||
|
|||||||
@@ -40,9 +40,9 @@ class UIPreferences(QObject):
|
|||||||
def load_preferences_ui(self):
|
def load_preferences_ui(self):
|
||||||
""" Loading all the initial preferences from the conf file to the UI
|
""" 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_accent.emit(CONFIGURE_QT.value("Material", "Accent", "Green"))
|
||||||
self.load_material_theme.emit(CONFIGURE_QT.get_or_default("Material", "Theme", "System"))
|
self.load_material_theme.emit(CONFIGURE_QT.value("Material", "Theme", "System"))
|
||||||
self.load_autoload.emit(int(CONFIGURE_QT.get_or_default("Database", "autoload", 0)))
|
self.load_autoload.emit(int(CONFIGURE_QT.value("Database", "autoload", 0)))
|
||||||
self.show_ui.emit()
|
self.show_ui.emit()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ from artemis.utils.sys_utils import copy_file
|
|||||||
|
|
||||||
class Config(ConfigParser):
|
class Config(ConfigParser):
|
||||||
""" Custom configuration class derived from 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):
|
def __init__(self, config_file_path, space_around_delimiters=False):
|
||||||
@@ -15,11 +15,13 @@ class Config(ConfigParser):
|
|||||||
self.read(self._config_file_path)
|
self.read(self._config_file_path)
|
||||||
self._space_around_delimiters = space_around_delimiters
|
self._space_around_delimiters = space_around_delimiters
|
||||||
|
|
||||||
def get_or_default(self, section, option, default_value):
|
def value(self, section, option, default_value):
|
||||||
value = super().get(section, option)
|
value = super().get(section, option, fallback=default_value)
|
||||||
return value if value else default_value
|
return value
|
||||||
|
|
||||||
def set(self, section, option, value=None):
|
def set(self, section, option, value=None):
|
||||||
|
if not self.has_section(section):
|
||||||
|
self.add_section(section)
|
||||||
super().set(section, option, value)
|
super().set(section, option, value)
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
@@ -32,13 +34,42 @@ class Config(ConfigParser):
|
|||||||
self.write(f, space_around_delimiters=self._space_around_delimiters)
|
self.write(f, space_around_delimiters=self._space_around_delimiters)
|
||||||
|
|
||||||
|
|
||||||
def prepare_qt_conf():
|
def merge_config_files(old_config_path, template_config_path):
|
||||||
if not (PREFERENCES_DIR / 'qtquickcontrols2.conf').exists():
|
""" Merge two configuration files: if the old one lacks some
|
||||||
copy_file(
|
sections or options from a comparison with a template,
|
||||||
BASE_DIR / 'config' / 'qtquickcontrols2.conf',
|
this function will add what is missing to the old conf file
|
||||||
PREFERENCES_DIR / 'qtquickcontrols2.conf'
|
"""
|
||||||
)
|
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())
|
CONFIGURE_QT = Config((PREFERENCES_DIR / 'qtquickcontrols2.conf').resolve().as_posix())
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ from artemis.utils.config_utils import CONFIGURE_QT
|
|||||||
|
|
||||||
|
|
||||||
def set_ui():
|
def set_ui():
|
||||||
os.environ['QT_QUICK_CONTROLS_STYLE'] = CONFIGURE_QT.get_or_default('Controls', 'style', 'Material')
|
os.environ['QT_QUICK_CONTROLS_STYLE'] = CONFIGURE_QT.value('Controls', 'style', 'Material')
|
||||||
os.environ['QT_QUICK_CONTROLS_MATERIAL_VARIANT'] = CONFIGURE_QT.get_or_default('Material', 'variant', 'Dense')
|
os.environ['QT_QUICK_CONTROLS_MATERIAL_VARIANT'] = CONFIGURE_QT.value('Material', 'variant', 'Dense')
|
||||||
os.environ['QT_QUICK_CONTROLS_MATERIAL_THEME'] = CONFIGURE_QT.get_or_default('Material', 'theme', 'System')
|
os.environ['QT_QUICK_CONTROLS_MATERIAL_THEME'] = CONFIGURE_QT.value('Material', 'theme', 'System')
|
||||||
os.environ['QT_QUICK_CONTROLS_MATERIAL_ACCENT'] = CONFIGURE_QT.get_or_default('Material', 'accent', 'Green')
|
os.environ['QT_QUICK_CONTROLS_MATERIAL_ACCENT'] = CONFIGURE_QT.value('Material', 'accent', 'Green')
|
||||||
|
|
||||||
if is_windows():
|
if is_windows():
|
||||||
os.environ['QSG_RHI_BACKEND'] = 'opengl'
|
os.environ['QSG_RHI_BACKEND'] = 'opengl'
|
||||||
|
|||||||
Reference in New Issue
Block a user