83 lines
2.8 KiB
Python
83 lines
2.8 KiB
Python
"""配置加载模块"""
|
|
import yaml
|
|
from pathlib import Path
|
|
from typing import Dict, Any, List
|
|
|
|
|
|
class ConfigLoader:
|
|
"""加载和验证YAML配置文件"""
|
|
|
|
def __init__(self, config_path: str):
|
|
"""
|
|
Args:
|
|
config_path: 配置文件路径
|
|
"""
|
|
self.config_path = Path(config_path)
|
|
self.config = None
|
|
|
|
def load(self) -> Dict[str, Any]:
|
|
"""加载配置文件"""
|
|
if not self.config_path.exists():
|
|
raise FileNotFoundError(f"配置文件不存在: {self.config_path}")
|
|
|
|
with open(self.config_path, 'r', encoding='utf-8') as f:
|
|
self.config = yaml.safe_load(f)
|
|
|
|
self._validate()
|
|
return self.config
|
|
|
|
def _validate(self):
|
|
"""验证配置文件格式"""
|
|
if not isinstance(self.config, dict):
|
|
raise ValueError("配置文件格式错误:根节点必须是字典")
|
|
|
|
# 检查必需字段
|
|
if 'point_cloud' not in self.config:
|
|
raise ValueError("配置文件缺少 'point_cloud' 字段")
|
|
|
|
if 'tasks' not in self.config:
|
|
raise ValueError("配置文件缺少 'tasks' 字段")
|
|
|
|
if not isinstance(self.config['tasks'], list):
|
|
raise ValueError("'tasks' 必须是列表")
|
|
|
|
# 验证每个任务
|
|
for i, task in enumerate(self.config['tasks']):
|
|
self._validate_task(task, i)
|
|
|
|
def _validate_task(self, task: Dict[str, Any], index: int):
|
|
"""验证单个任务配置"""
|
|
required_fields = ['name', 'type']
|
|
for field in required_fields:
|
|
if field not in task:
|
|
raise ValueError(f"任务 {index} 缺少必需字段: {field}")
|
|
|
|
# 验证任务类型
|
|
valid_types = ['plane_to_plane', 'point_to_plane', 'point_to_point']
|
|
if task['type'] not in valid_types:
|
|
raise ValueError(f"任务 {index} 类型无效: {task['type']}, 必须是 {valid_types} 之一")
|
|
|
|
def get_point_cloud_path(self) -> str:
|
|
"""获取点云文件路径"""
|
|
return self.config['point_cloud']
|
|
|
|
def get_tasks(self) -> List[Dict[str, Any]]:
|
|
"""获取所有任务配置"""
|
|
return self.config['tasks']
|
|
|
|
def get_global_settings(self) -> Dict[str, Any]:
|
|
"""获取全局设置"""
|
|
return self.config.get('global_settings', {})
|
|
|
|
def is_visualization_enabled(self) -> bool:
|
|
"""是否启用可视化"""
|
|
return self.get_global_settings().get('enable_visualization', False)
|
|
|
|
def get_visualization_dir(self) -> str:
|
|
"""获取可视化输出目录"""
|
|
return self.get_global_settings().get('visualization_dir', 'visualizations')
|
|
|
|
def get_results_dir(self) -> str:
|
|
"""获取结果输出目录"""
|
|
return self.get_global_settings().get('results_dir', 'results')
|