72 lines
2.5 KiB
Python
72 lines
2.5 KiB
Python
# backend/core/workdir_config.py
|
|
from pathlib import Path
|
|
import json
|
|
from typing import Dict, Any, Optional
|
|
from datetime import datetime
|
|
|
|
class WorkDirConfigManager:
|
|
"""Manages configuration files in work directories"""
|
|
|
|
DEFAULT_CONFIG = {
|
|
"version": "1.0",
|
|
"created_at": "",
|
|
"updated_at": "",
|
|
"group_settings": {}
|
|
}
|
|
|
|
def __init__(self, work_dir: str):
|
|
self.work_dir = Path(work_dir)
|
|
self.config_file = self.work_dir / "script_config.json"
|
|
|
|
def get_config(self) -> Dict[str, Any]:
|
|
"""Get configuration for work directory"""
|
|
if self.config_file.exists():
|
|
try:
|
|
with open(self.config_file, 'r', encoding='utf-8') as f:
|
|
return json.load(f)
|
|
except Exception as e:
|
|
print(f"Error loading work dir config: {e}")
|
|
return self._create_default_config()
|
|
return self._create_default_config()
|
|
|
|
def _create_default_config(self) -> Dict[str, Any]:
|
|
"""Create default configuration"""
|
|
config = self.DEFAULT_CONFIG.copy()
|
|
now = datetime.now().isoformat()
|
|
config["created_at"] = now
|
|
config["updated_at"] = now
|
|
return config
|
|
|
|
def save_config(self, config: Dict[str, Any]):
|
|
"""Save configuration to file"""
|
|
# Ensure work directory exists
|
|
self.work_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
# Update timestamp
|
|
config["updated_at"] = datetime.now().isoformat()
|
|
|
|
# Save config
|
|
with open(self.config_file, 'w', encoding='utf-8') as f:
|
|
json.dump(config, f, indent=4)
|
|
|
|
def get_group_config(self, group_id: str) -> Dict[str, Any]:
|
|
"""Get configuration for specific script group"""
|
|
config = self.get_config()
|
|
return config["group_settings"].get(group_id, {})
|
|
|
|
def update_group_config(self, group_id: str, settings: Dict[str, Any]):
|
|
"""Update configuration for specific script group"""
|
|
config = self.get_config()
|
|
|
|
if "group_settings" not in config:
|
|
config["group_settings"] = {}
|
|
|
|
config["group_settings"][group_id] = settings
|
|
self.save_config(config)
|
|
|
|
def remove_group_config(self, group_id: str):
|
|
"""Remove configuration for specific script group"""
|
|
config = self.get_config()
|
|
if group_id in config.get("group_settings", {}):
|
|
del config["group_settings"][group_id]
|
|
self.save_config(config) |