Files
ResourceTranslate/config.py
2026-02-14 18:12:28 +01:00

74 lines
2.3 KiB
Python

"""
Configuration management for Android XML Translation Tool
"""
import os
import sys
import yaml
from pathlib import Path
from typing import Dict, List, Optional, Any
class Config:
"""Configuration loader and validator"""
def __init__(self, config_path: str = "config.yaml"):
self.config_path = config_path
self.data = self._load_config()
if self.data is not None: # Only validate if data was loaded
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)
required_sections = ['llm', 'android', 'translation']
for section in required_sections:
if section not in self.data:
print(f"Missing required section: {section}")
sys.exit(1)
@property
def llm_config(self) -> Dict[str, Any]:
return self.data['llm']
@property
def android_config(self) -> Dict[str, Any]:
return self.data['android']
@property
def translation_config(self) -> Dict[str, Any]:
return self.data['translation']
@property
def output_config(self) -> Dict[str, Any]:
return self.data.get('output', {})
@property
def examples_config(self) -> Dict[str, Any]:
return self.data.get('examples', {})
def has_examples_config(self) -> bool:
"""Check if examples configuration is present and valid"""
examples = self.data.get('examples', {})
return bool(
examples and
examples.get('input_folder') and
examples.get('base_folder') and
examples.get('target_folders')
)