Use XDG Base Directory Specification for config file location
This commit is contained in:
@@ -13,6 +13,7 @@ from typing import Dict, Any, List
|
|||||||
import importlib.util
|
import importlib.util
|
||||||
import glob
|
import glob
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
# Default configuration values used when config file or env vars are missing
|
# Default configuration values used when config file or env vars are missing
|
||||||
CONFIG_DEFAULTS = {
|
CONFIG_DEFAULTS = {
|
||||||
@@ -31,7 +32,7 @@ CONFIG_DEFAULTS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class System2MQTT:
|
class System2MQTT:
|
||||||
def __init__(self, config_path: str = "config.yaml"):
|
def __init__(self, config_path: str = None):
|
||||||
self.config = self._load_config(config_path)
|
self.config = self._load_config(config_path)
|
||||||
self.hostname = socket.gethostname()
|
self.hostname = socket.gethostname()
|
||||||
self.client = None # paho MQTT client initialized in connect()
|
self.client = None # paho MQTT client initialized in connect()
|
||||||
@@ -40,15 +41,36 @@ class System2MQTT:
|
|||||||
self.collectors = self._load_collectors()
|
self.collectors = self._load_collectors()
|
||||||
self.last_run = {} # Speichert den Zeitpunkt des letzten Laufs für jeden Sammler
|
self.last_run = {} # Speichert den Zeitpunkt des letzten Laufs für jeden Sammler
|
||||||
|
|
||||||
def _load_config(self, config_path: str) -> Dict[str, Any]:
|
def _get_default_config_path(self) -> str:
|
||||||
|
"""Get default config path following XDG Base Directory Specification.
|
||||||
|
|
||||||
|
Returns path in order of preference:
|
||||||
|
1. $XDG_CONFIG_HOME/system2mqtt/config.yaml
|
||||||
|
2. ~/.config/system2mqtt/config.yaml (if XDG_CONFIG_HOME not set)
|
||||||
|
"""
|
||||||
|
xdg_config_home = os.environ.get('XDG_CONFIG_HOME')
|
||||||
|
if xdg_config_home:
|
||||||
|
config_dir = Path(xdg_config_home) / 'system2mqtt'
|
||||||
|
else:
|
||||||
|
config_dir = Path.home() / '.config' / 'system2mqtt'
|
||||||
|
|
||||||
|
return str(config_dir / 'config.yaml')
|
||||||
|
|
||||||
|
def _load_config(self, config_path: str = None) -> Dict[str, Any]:
|
||||||
"""Load configuration from YAML file, apply defaults and environment overrides.
|
"""Load configuration from YAML file, apply defaults and environment overrides.
|
||||||
|
|
||||||
Precedence: CONFIG_DEFAULTS < config file < environment variables
|
Precedence: CONFIG_DEFAULTS < config file < environment variables
|
||||||
|
Config path precedence: SYSTEM2MQTT_CONFIG env var > parameter > XDG location
|
||||||
"""
|
"""
|
||||||
# Determine config path: env var overrides parameter
|
# Determine config path with precedence:
|
||||||
|
# 1. SYSTEM2MQTT_CONFIG environment variable
|
||||||
|
# 2. Provided config_path parameter
|
||||||
|
# 3. XDG Base Directory Specification location
|
||||||
env_path = os.environ.get('SYSTEM2MQTT_CONFIG')
|
env_path = os.environ.get('SYSTEM2MQTT_CONFIG')
|
||||||
if env_path:
|
if env_path:
|
||||||
config_path = env_path
|
config_path = env_path
|
||||||
|
elif config_path is None:
|
||||||
|
config_path = self._get_default_config_path()
|
||||||
|
|
||||||
config = {}
|
config = {}
|
||||||
# Start with defaults
|
# Start with defaults
|
||||||
|
|||||||
Reference in New Issue
Block a user