111 lines
3.0 KiB
Python
111 lines
3.0 KiB
Python
"""Configuration management for disk reorganizer"""
|
|
import json
|
|
from pathlib import Path
|
|
from dataclasses import dataclass, asdict
|
|
from typing import Optional
|
|
|
|
|
|
@dataclass
|
|
class DatabaseConfig:
|
|
"""Database connection configuration"""
|
|
host: str = '192.168.1.159'
|
|
port: int = 5432
|
|
database: str = 'disk_reorganizer_db'
|
|
user: str = 'disk_reorg_user'
|
|
password: str = 'heel-goed-wachtwoord'
|
|
|
|
def to_dict(self) -> dict:
|
|
"""Convert to dictionary"""
|
|
return asdict(self)
|
|
|
|
|
|
@dataclass
|
|
class ProcessingConfig:
|
|
"""Processing behavior configuration"""
|
|
batch_size: int = 1000
|
|
commit_interval: int = 100
|
|
parallel_workers: int = 4
|
|
chunk_size: int = 8192
|
|
hash_algorithm: str = 'sha256'
|
|
verify_operations: bool = True
|
|
preserve_timestamps: bool = True
|
|
|
|
def to_dict(self) -> dict:
|
|
"""Convert to dictionary"""
|
|
return asdict(self)
|
|
|
|
|
|
@dataclass
|
|
class LoggingConfig:
|
|
"""Logging configuration"""
|
|
level: str = 'INFO'
|
|
log_file: str = 'disk_reorganizer.log'
|
|
console_output: bool = True
|
|
file_output: bool = True
|
|
|
|
def to_dict(self) -> dict:
|
|
"""Convert to dictionary"""
|
|
return asdict(self)
|
|
|
|
|
|
@dataclass
|
|
class Config:
|
|
"""Main configuration container"""
|
|
database: DatabaseConfig = None
|
|
processing: ProcessingConfig = None
|
|
logging: LoggingConfig = None
|
|
|
|
def __post_init__(self):
|
|
"""Initialize nested configs with defaults if not provided"""
|
|
if self.database is None:
|
|
self.database = DatabaseConfig()
|
|
if self.processing is None:
|
|
self.processing = ProcessingConfig()
|
|
if self.logging is None:
|
|
self.logging = LoggingConfig()
|
|
|
|
@classmethod
|
|
def from_file(cls, config_path: Path) -> 'Config':
|
|
"""Load configuration from JSON file"""
|
|
if not config_path.exists():
|
|
return cls()
|
|
|
|
with open(config_path, 'r') as f:
|
|
data = json.load(f)
|
|
|
|
return cls(
|
|
database=DatabaseConfig(**data.get('database', {})),
|
|
processing=ProcessingConfig(**data.get('processing', {})),
|
|
logging=LoggingConfig(**data.get('logging', {}))
|
|
)
|
|
|
|
def to_file(self, config_path: Path) -> None:
|
|
"""Save configuration to JSON file"""
|
|
data = {
|
|
'database': self.database.to_dict(),
|
|
'processing': self.processing.to_dict(),
|
|
'logging': self.logging.to_dict()
|
|
}
|
|
|
|
with open(config_path, 'w') as f:
|
|
json.dump(data, f, indent=2)
|
|
|
|
def to_dict(self) -> dict:
|
|
"""Convert to dictionary"""
|
|
return {
|
|
'database': self.database.to_dict(),
|
|
'processing': self.processing.to_dict(),
|
|
'logging': self.logging.to_dict()
|
|
}
|
|
|
|
|
|
def load_config(config_path: Optional[Path] = None) -> Config:
|
|
"""Load configuration from file or return default"""
|
|
if config_path is None:
|
|
config_path = Path('config.json')
|
|
|
|
if config_path.exists():
|
|
return Config.from_file(config_path)
|
|
|
|
return Config()
|