57 lines
1.5 KiB
Python
57 lines
1.5 KiB
Python
import os.path
|
|
|
|
import tomli
|
|
import tomli_w
|
|
|
|
from metar_navigate.utils import DirectoryManager
|
|
|
|
from threading import Lock
|
|
|
|
class SingletonMeta(type):
|
|
_instances = {}
|
|
_lock: Lock = Lock()
|
|
|
|
def __call__(cls, *args, **kwargs):
|
|
with cls._lock:
|
|
if cls not in cls._instances:
|
|
instance = super().__call__(*args, **kwargs)
|
|
cls._instances[cls] = instance
|
|
return cls._instances[cls]
|
|
|
|
|
|
class ConfigManager(metaclass=SingletonMeta):
|
|
def __init__(self):
|
|
self.config_dir = DirectoryManager.get_config_directory()
|
|
self.__data = None
|
|
|
|
def read_config(self):
|
|
if os.path.exists(f"{self.config_dir}/config.toml"):
|
|
try:
|
|
with open(f"{self.config_dir}/config.toml", "rb") as f:
|
|
data = tomli.load(f)
|
|
except tomli.TOMLDecodeError as e:
|
|
raise ValueError(f"Invalid TOML file; {e}")
|
|
except Exception as e:
|
|
raise ValueError(f"Error reading config file; {e}")
|
|
|
|
self.__data = data
|
|
return self.__data
|
|
|
|
else:
|
|
return None
|
|
def set_data_dict(self,data):
|
|
self.__data.update(data)
|
|
|
|
|
|
def write_config(self):
|
|
default = {
|
|
"checkwx_api_key": "",
|
|
"always_show_detailed": False,
|
|
"wind_speed_unit": "Keep",
|
|
"temp_unit": "Keep",
|
|
"visibility_unit": "Keep",
|
|
"pressure_unit": "Keep"
|
|
|
|
}
|
|
|