AnalyseMorphologique/utils/settings/SettingManager.py
Djalim Simaila fc525a0878 🔖 chore(SettingManager.py): add version attribute to SettingManager class and update config.yml version if needed
The SettingManager class now has a version attribute to keep track of the version of the configuration file. If the version in the configuration file is different from the version in the SettingManager class, the version in the configuration file is updated to match the version in the SettingManager class. This ensures that the configuration file is always up-to-date with the latest version of the application.
2023-05-02 10:33:56 +02:00

111 lines
3.5 KiB
Python

"""
Created on Fri Apr 21 2023
@name: SettingManager.py
@desc: A module to manage the settings
@auth: Djalim Simaila
@e-mail: djalim.simaila@inrae.fr
"""
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
class SettingManager:
"""
A class to manage the settings
:ivar settings: The settings
:ivar instance: The instance of the class
"""
instance = None
version = "1.1.1"
def __init__(self):
try:
with open('config.yml', 'r') as f:
self.settings = load(f.read(), Loader=Loader)
if self.settings['version'] != self.version:
self.settings['version'] = self.version
self.save()
except FileNotFoundError:
self.settings = {}
self.createInitialSettings()
@staticmethod
def get_instance()->'SettingManager':
"""
Get the instance of the class
:return: The instance of the class
"""
if SettingManager.instance is None:
SettingManager.instance = SettingManager()
return SettingManager.instance
def save(self):
"""
Save the settings to the config file
"""
with open('config.yml', 'w') as f:
f.write(dump(self.settings, Dumper=Dumper))
def createInitialSettings(self):
self.settings['version'] = self.version
self.settings['name'] = 'Analyse Morphologique'
self.settings['authors'] = 'Alexis Doghmane <alexis@doghmane.fr>, Djalim Simaila <djalim.simaila@inrae.fr>'
self.settings['description'] = 'Analyse Morphologique'
self.settings['repo'] = 'https://forgemia.inra.fr/scanner3d/analysemorphologique'
self.settings['lastGraph'] = ["Aucun" for i in range(11)]
self.settings['discretisation_method'] = 'Z0-Zi >= DeltaZ' # 'Z0-Zi < DeltaZ'
self.settings['discretisation_methods'] = ['Z0-Zi >= DeltaZ', 'Z0-Zi < DeltaZ']
self.settings['raw_data_suffix'] = '_delta_{delta_z}_analyse_brute'
self.settings['discrete_data_suffix'] = '_delta_{delta_z}_analyse_rayon'
self.settings['output_file_extension'] = '.txt'
self.settings['output_file_separator'] = '\t'
self.settings['pretiffy_output_file'] = True
self.save()
def get_last_graph(self, graph_number)->str:
"""
Get the last graph name for the specified slot
"""
return self.settings['lastGraph'][graph_number]
def set_last_graph(self, graph_number, graph_name):
"""
Set the last graph name for the specified slot
:param graph_number: The slot number
:param graph_name: The graph name
"""
self.settings['lastGraph'][graph_number] = graph_name
self.save()
def get_setting(self, setting_name)->any:
"""
Get the value of the specified setting
:param setting_name: The name of the setting
:return: The value of the setting
"""
return self.settings[setting_name]
def get_settings(self)->dict:
"""
Get the settings
:return: The settings
"""
return self.settings
def set_setting(self, setting_name, setting_value):
"""
Set the value of the specified setting
:param setting_name: The name of the setting
:param setting_value: The value of the setting
"""
self.settings[setting_name] = setting_value
self.save()