Spaces:
Sleeping
Sleeping
File size: 1,474 Bytes
1c57e18 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import yaml
import logging
from pathlib import Path
logger = logging.getLogger(__name__)
class Config:
BASE_DIR = Path(__file__).parent.parent
CONFIG_PATH = BASE_DIR / "config"
ICON_OUTPUT_PATH = BASE_DIR / "temp"
def __init__(self) -> None:
self.load_from_folder()
def load_from_folder(self):
"""
Load all YAML configuration files from the CONFIG_PATH directory and add them as attributes to the Config instance.
"""
if not self.CONFIG_PATH.exists():
# Fallback for different working directory layouts
fallback_path = Path().resolve() / "config"
if fallback_path.exists():
self.CONFIG_PATH = fallback_path
logger.info(f"Loading configuration from {self.CONFIG_PATH}")
config_files = self.CONFIG_PATH.glob("*.yaml")
loaded_configs = []
for config_file in config_files:
config_name = config_file.stem
try:
with open(config_file, 'r') as file:
config_data = yaml.safe_load(file)
setattr(self, config_name, config_data)
loaded_configs.append(config_name)
except Exception as e:
logger.error(f"Failed to load config {config_file}: {e}")
logger.info(f"Loaded configurations: {', '.join(loaded_configs)}")
if __name__ == "__main__":
config = Config() # For debugging purposes |