waifuc-gui / waifuc_gui /config_manager.py
LittleApple-fp16's picture
Upload 7 files
32b00b0 verified
import os
import json
import uuid
from typing import Dict, Any
class ConfigManager:
def __init__(self, session_id: str):
self.session_id = session_id
self.config_dir = f"/tmp/user_{session_id}"
self.config_file = os.path.join(self.config_dir, "config.json")
self.config: Dict[str, Any] = {}
self.load_configs()
def load_configs(self):
self.config = {
"pixiv_refresh_token": "",
"output_dir": f"/tmp/user_{self.session_id}/output",
"default_num_items": 100,
"default_resize_size": 512,
"language": "zh"
}
if os.path.exists(self.config_file):
try:
with open(self.config_file, 'r') as f:
self.config.update(json.load(f))
except Exception as e:
print(f"Failed to load session config: {e}")
def save_configs(self):
os.makedirs(self.config_dir, exist_ok=True)
try:
with open(self.config_file, 'w') as f:
json.dump(self.config, f, indent=2)
except Exception as e:
print(f"Failed to save session config: {e}")
def get_config(self, key: str, default: Any = None) -> Any:
return self.config.get(key, default)
def set_config(self, key: str, value: Any):
self.config[key] = value
self.save_configs()
def get_all_configs(self) -> Dict[str, Any]:
return self.config.copy()
def export_config(self) -> str:
export_path = os.path.join(self.config_dir, "config_export.json")
with open(export_path, 'w') as f:
json.dump(self.config, f, indent=2)
return export_path
def import_config(self, config_file):
with open(config_file, 'r') as f:
new_config = json.load(f)
self.config.update(new_config)
self.save_configs()
return "Configuration imported" if self.config.get("language") == "en" else "配置已导入"
def cleanup(self):
try:
shutil.rmtree(self.config_dir, ignore_errors=True)
except Exception as e:
print(f"Failed to cleanup session: {e}")