| """ |
| Configuration management for the GotPsi data cleaning pipeline. |
| |
| Handles loading and validation of YAML configuration files. |
| """ |
|
|
| from pathlib import Path |
| from typing import Any, Dict |
| import yaml |
|
|
| from .exceptions import ConfigurationError |
|
|
|
|
| class Config: |
| """ |
| Configuration manager for the data cleaning pipeline. |
| |
| Loads configuration from YAML file and provides typed access |
| to configuration values. |
| """ |
|
|
| def __init__(self, config_path: str | Path): |
| """ |
| Initialize configuration from YAML file. |
| |
| Args: |
| config_path: Path to the YAML configuration file |
| |
| Raises: |
| ConfigurationError: If config file is missing or invalid |
| """ |
| self.config_path = Path(config_path) |
| self._config: Dict[str, Any] = {} |
| self._load_config() |
| self._validate_config() |
|
|
| def _load_config(self) -> None: |
| """Load configuration from YAML file.""" |
| if not self.config_path.exists(): |
| raise ConfigurationError( |
| f"Configuration file not found: {self.config_path}" |
| ) |
|
|
| try: |
| with open(self.config_path, 'r') as f: |
| self._config = yaml.safe_load(f) |
| except yaml.YAMLError as e: |
| raise ConfigurationError( |
| f"Failed to parse configuration file: {e}" |
| ) |
|
|
| def _validate_config(self) -> None: |
| """Validate that required configuration keys exist.""" |
| required_sections = [ |
| 'directories', |
| 'processing', |
| 'encoding', |
| 'delimiters', |
| 'temporal', |
| 'schema', |
| 'quality', |
| 'export', |
| 'datasets', |
| 'logging' |
| ] |
|
|
| missing = [s for s in required_sections if s not in self._config] |
| if missing: |
| raise ConfigurationError( |
| f"Missing required configuration sections: {missing}" |
| ) |
|
|
| def get(self, key: str, default: Any = None) -> Any: |
| """ |
| Get configuration value by key. |
| |
| Supports dot notation for nested keys (e.g., 'directories.raw_data') |
| |
| Args: |
| key: Configuration key (supports dot notation) |
| default: Default value if key not found |
| |
| Returns: |
| Configuration value or default |
| """ |
| keys = key.split('.') |
| value = self._config |
|
|
| for k in keys: |
| if isinstance(value, dict): |
| value = value.get(k) |
| if value is None: |
| return default |
| else: |
| return default |
|
|
| return value |
|
|
| def set(self, key: str, value: Any) -> None: |
| """ |
| Set configuration value by key. |
| |
| Supports dot notation for nested keys (e.g., 'processing.audit_mode') |
| |
| Args: |
| key: Configuration key (supports dot notation) |
| value: Value to set |
| """ |
| keys = key.split('.') |
| config = self._config |
|
|
| |
| for k in keys[:-1]: |
| if k not in config: |
| config[k] = {} |
| config = config[k] |
|
|
| |
| config[keys[-1]] = value |
|
|
| def __getitem__(self, key: str) -> Any: |
| """Get configuration value using dict-like syntax.""" |
| value = self.get(key) |
| if value is None: |
| raise KeyError(f"Configuration key not found: {key}") |
| return value |
|
|
| @property |
| def raw_data_dir(self) -> Path: |
| """Get raw data directory as Path object.""" |
| return Path(self.get('directories.raw_data', 'data/')) |
|
|
| @property |
| def output_dir(self) -> Path: |
| """Get output directory as Path object.""" |
| return Path(self.get('directories.output', 'output/')) |
|
|
| @property |
| def logs_dir(self) -> Path: |
| """Get logs directory as Path object.""" |
| return Path(self.get('directories.logs', 'logs/')) |
|
|
| @property |
| def supplemental_dir(self) -> Path: |
| """Get supplemental directory as Path object.""" |
| return Path(self.get('directories.supplemental', 'supplemental/')) |
|
|
| @property |
| def chunk_size(self) -> int: |
| """Get processing chunk size.""" |
| return self.get('processing.chunk_size', 50000) |
|
|
| @property |
| def n_workers(self) -> int: |
| """Get number of parallel workers.""" |
| return self.get('processing.n_workers', 0) |
|
|
| def dataset_config(self, dataset_name: str) -> Dict[str, Any]: |
| """ |
| Get configuration for a specific dataset. |
| |
| Args: |
| dataset_name: Name of the dataset |
| |
| Returns: |
| Dataset configuration dictionary |
| |
| Raises: |
| ConfigurationError: If dataset not configured |
| """ |
| datasets = self.get('datasets', {}) |
| if dataset_name not in datasets: |
| raise ConfigurationError( |
| f"Dataset '{dataset_name}' not found in configuration" |
| ) |
| return datasets[dataset_name] |