2025-03-06 11:54:11 +01:00

55 lines
1.7 KiB
Python

from importlib.metadata import PackageNotFoundError
from os import path, environ
from cpdctrl.utility.config_file import ConfigFile
class AppConfig:
"""
Configuration File
"""
APP_NAME: str = "cpdctrl-gui"
APP_VERSION: str = "unknown"
CONFIG_DIR: str = path.expanduser("~/.config/cpdctrl")
MAIN_CFG: ConfigFile = None
MEAS_CFG: ConfigFile = None
@classmethod
def initialize(cls) -> None:
"""
Perform any necessary initializations here, e.g.:
- Loading settings from a file
"""
if 'XDG_CONFIG_HOME' in environ.keys():
AppConfig.CONFIG_DIR = path.join(environ["XDG_CONFIG_HOME"], "cpdctrl")
# Main CFG
AppConfig.MAIN_CFG_PATH = path.join(AppConfig.CONFIG_DIR, "cpdctrl-gui.yaml")
AppConfig.MAIN_CFG = ConfigFile(AppConfig.MAIN_CFG_PATH, {
"voltage_measurement_device_auto_reconnect": True,
"led_device_auto_reconnect": True,
"datadir": "~/data2",
})
# Measurement CFG
AppConfig.MEAS_CFG_PATH = path.join(AppConfig.CONFIG_DIR, "cpdctrl-gui-measurement.yaml")
AppConfig.MEAS_CFG = ConfigFile(AppConfig.MEAS_CFG_PATH, {})
# Get the Version from importlib
# This will always show the version of the installed package, not the local development version
from importlib.metadata import version, PackageNotFoundError
try:
AppConfig.APP_VERSION = version("cpdctrl-gui")
except PackageNotFoundError:
pass
@classmethod
def finalize(cls) -> None:
"""
Write configuration to file
"""
AppConfig.MAIN_CFG.save()
AppConfig.MEAS_CFG.save()