LocalScriptsWeb/backend/script_groups/base_script.py

78 lines
2.8 KiB
Python
Raw Permalink Normal View History

2025-02-07 19:08:39 -03:00
# backend/script_groups/base_script.py
from typing import Dict, Any
from pathlib import Path
import json
from datetime import datetime
2025-02-07 19:08:39 -03:00
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_work_config(self, work_dir: str, group_id: str) -> Dict[str, Any]:
"""Get configuration from work directory"""
2025-02-07 19:08:39 -03:00
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 work directory config: {e}")
2025-02-07 19:08:39 -03:00
return {}
def save_work_config(self, work_dir: str, group_id: str, settings: Dict[str, Any]):
"""Save configuration to work directory"""
2025-02-07 19:08:39 -03:00
config_file = Path(work_dir) / "script_config.json"
try:
# Cargar configuración existente o crear nueva
2025-02-07 19:08:39 -03:00
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": {},
"created_at": datetime.now().isoformat()
2025-02-07 19:08:39 -03:00
}
# Actualizar configuración
2025-02-07 19:08:39 -03:00
if "group_settings" not in config:
config["group_settings"] = {}
config["group_settings"][group_id] = settings
config["updated_at"] = datetime.now().isoformat()
2025-02-07 19:08:39 -03:00
# Guardar configuración
2025-02-07 19:08:39 -03:00
with open(config_file, 'w', encoding='utf-8') as f:
json.dump(config, f, indent=4)
except Exception as e:
print(f"Error saving work directory config: {e}")
raise
def get_group_data(self, group_dir: Path) -> Dict[str, Any]:
"""Get group configuration data"""
data_file = group_dir / "data.json"
if data_file.exists():
try:
with open(data_file, 'r', encoding='utf-8') as f:
return json.load(f)
except Exception as e:
print(f"Error loading group data: {e}")
return {}