62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
# backend/script_groups/base_script.py
|
|
from typing import Dict, Any
|
|
from pathlib import Path
|
|
import json
|
|
|
|
class BaseScript:
|
|
"""Base class for all scripts"""
|
|
|
|
def run(self, work_dir: str, profile: Dict[str, Any]) -> Dict[str, Any]:
|
|
"""
|
|
Execute the script
|
|
|
|
Args:
|
|
work_dir (str): Working directory path
|
|
profile (Dict[str, Any]): Current profile configuration
|
|
|
|
Returns:
|
|
Dict[str, Any]: Execution results
|
|
"""
|
|
raise NotImplementedError("Script must implement run method")
|
|
|
|
def get_config(self, work_dir: str, group_id: str) -> Dict[str, Any]:
|
|
"""Get group configuration from work directory"""
|
|
config_file = Path(work_dir) / "script_config.json"
|
|
|
|
if config_file.exists():
|
|
try:
|
|
with open(config_file, 'r', encoding='utf-8') as f:
|
|
config = json.load(f)
|
|
return config.get("group_settings", {}).get(group_id, {})
|
|
except Exception as e:
|
|
print(f"Error loading config: {e}")
|
|
|
|
return {}
|
|
|
|
def save_config(self, work_dir: str, group_id: str, settings: Dict[str, Any]):
|
|
"""Save group configuration to work directory"""
|
|
config_file = Path(work_dir) / "script_config.json"
|
|
|
|
try:
|
|
# Load existing config or create new
|
|
if config_file.exists():
|
|
with open(config_file, 'r', encoding='utf-8') as f:
|
|
config = json.load(f)
|
|
else:
|
|
config = {
|
|
"version": "1.0",
|
|
"group_settings": {}
|
|
}
|
|
|
|
# Update settings
|
|
if "group_settings" not in config:
|
|
config["group_settings"] = {}
|
|
config["group_settings"][group_id] = settings
|
|
|
|
# Save config
|
|
with open(config_file, 'w', encoding='utf-8') as f:
|
|
json.dump(config, f, indent=4)
|
|
|
|
except Exception as e:
|
|
print(f"Error saving config: {e}")
|