welcome gitea
This commit is contained in:
68
config.py
Normal file
68
config.py
Normal file
@@ -0,0 +1,68 @@
|
||||
"""
|
||||
Configuration management for VocabListGenerator
|
||||
"""
|
||||
|
||||
import sys
|
||||
import yaml
|
||||
from typing import Dict, Any, List
|
||||
|
||||
|
||||
class Config:
|
||||
"""Configuration loader and validator for VocabListGenerator"""
|
||||
|
||||
def __init__(self, config_path: str = "conf"):
|
||||
self.config_path = config_path
|
||||
self.data = self._load_config()
|
||||
self._validate_config()
|
||||
|
||||
def _load_config(self) -> Dict[str, Any]:
|
||||
"""Load configuration from YAML file"""
|
||||
try:
|
||||
with open(self.config_path, 'r', encoding='utf-8') as f:
|
||||
return yaml.safe_load(f)
|
||||
except FileNotFoundError:
|
||||
print(f"Configuration file '{self.config_path}' not found!")
|
||||
sys.exit(1)
|
||||
except yaml.YAMLError as e:
|
||||
print(f"Error parsing configuration file: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
def _validate_config(self):
|
||||
"""Validate required configuration fields"""
|
||||
if self.data is None:
|
||||
print(f"Configuration file '{self.config_path}' is empty or invalid!")
|
||||
sys.exit(1)
|
||||
|
||||
for section in ['llm', 'vocab']:
|
||||
if section not in self.data:
|
||||
print(f"Missing required section '{section}' in conf")
|
||||
sys.exit(1)
|
||||
|
||||
vocab = self.data['vocab']
|
||||
|
||||
if 'languages' not in vocab or not isinstance(vocab['languages'], list) or len(vocab['languages']) != 2:
|
||||
print("conf: 'vocab.languages' must contain exactly 2 language IDs (first and second language)")
|
||||
sys.exit(1)
|
||||
|
||||
if 'amount' not in vocab or not isinstance(vocab['amount'], int) or vocab['amount'] < 1:
|
||||
print("conf: 'vocab.amount' must be a positive integer")
|
||||
sys.exit(1)
|
||||
|
||||
if 'category' not in vocab or not vocab['category']:
|
||||
print("conf: 'vocab.category' must be a non-empty string")
|
||||
sys.exit(1)
|
||||
|
||||
@property
|
||||
def llm_config(self) -> Dict[str, Any]:
|
||||
"""LLM connection settings"""
|
||||
return self.data['llm']
|
||||
|
||||
@property
|
||||
def vocab_config(self) -> Dict[str, Any]:
|
||||
"""Vocabulary generator settings"""
|
||||
return self.data['vocab']
|
||||
|
||||
@property
|
||||
def manifest_config(self) -> Dict[str, Any]:
|
||||
"""Manifest settings (optional section)"""
|
||||
return self.data.get('manifest', {})
|
||||
Reference in New Issue
Block a user