Files
DS_L10N/lib/config_loader.py
2025-10-29 13:32:42 +09:00

88 lines
2.5 KiB
Python

"""
Configuration Loader for DS_L10N
YAML 설정 파일 로더
"""
import yaml
from pathlib import Path
from typing import Dict, Any
class Config:
"""설정 관리 클래스"""
def __init__(self, config_path: Path):
self.config_path = config_path
self.data: Dict[str, Any] = {}
self.base_dir = config_path.parent
self.load()
def load(self):
"""YAML 설정 파일 로드"""
if not self.config_path.exists():
raise FileNotFoundError(f"설정 파일을 찾을 수 없습니다: {self.config_path}")
with open(self.config_path, 'r', encoding='utf-8') as f:
self.data = yaml.safe_load(f)
def get(self, key: str, default=None):
"""설정 값 가져오기 (점 표기법 지원)"""
keys = key.split('.')
value = self.data
for k in keys:
if isinstance(value, dict):
value = value.get(k)
else:
return default
if value is None:
return default
return value
def get_path(self, key: str, default=None) -> Path:
"""경로 설정 가져오기 (상대 경로를 절대 경로로 변환)"""
value = self.get(key, default)
if value is None:
return None
path = Path(value)
# 상대 경로면 base_dir 기준으로 절대 경로 변환
if not path.is_absolute():
path = (self.base_dir / path).resolve()
return path
def get_all_paths(self) -> Dict[str, Path]:
"""모든 경로 설정 가져오기"""
paths_config = self.get('paths', {})
return {key: self.get_path(f'paths.{key}') for key in paths_config.keys()}
def get_languages(self) -> Dict[str, Any]:
"""언어 설정 가져오기"""
return {
'source': self.get('languages.source', 'ko'),
'targets': self.get('languages.targets', [])
}
def get_validation_config(self) -> Dict[str, bool]:
"""검증 설정 가져오기"""
return self.get('validation', {})
def __getitem__(self, key: str):
"""딕셔너리 스타일 접근"""
return self.get(key)
def __repr__(self):
return f"Config(config_path={self.config_path})"
def load_config(config_path: Path = None) -> Config:
"""설정 파일 로드"""
if config_path is None:
# 현재 스크립트 위치 기준으로 config.yaml 찾기
config_path = Path(__file__).parent.parent / 'config.yaml'
return Config(config_path)