41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
import os
|
|
import yaml
|
|
from typing import Dict, Any
|
|
import asyncio
|
|
|
|
async def read_config(config_file):
|
|
"""
|
|
Reads and parses a YAML configuration file from the parent directory.
|
|
Args:
|
|
'config_file' (str) with the name of the YAML file.
|
|
If not provided, defaults to 'config.yaml'.
|
|
Returns:
|
|
Dictionary with the parsed configuration and input arguments.
|
|
Raises:
|
|
OSError: If the file cannot be read or parsed.
|
|
"""
|
|
try:
|
|
# Get the config file name from args, default to 'config.yaml'
|
|
config_file = "./config/"+config_file
|
|
|
|
# Construct path to the parent directory
|
|
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
|
|
config_path = os.path.join(parent_dir, config_file)
|
|
|
|
# Check if file exists
|
|
if not os.path.exists(config_path):
|
|
raise OSError(f"Configuration file '{config_file}' not found in parent directory")
|
|
|
|
# Read and parse YAML file
|
|
with open(config_path, 'r', encoding='utf-8') as f:
|
|
config_data = yaml.safe_load(f)
|
|
|
|
if config_data is None:
|
|
raise OSError(f"Configuration file '{config_file}' is empty or invalid")
|
|
|
|
return config_data
|
|
|
|
except yaml.YAMLError as e:
|
|
raise OSError(f"Error parsing YAML file: {str(e)}")
|
|
except Exception as e:
|
|
raise OSError(f"Error reading configuration file: {str(e)}") |