-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile_studio.py
More file actions
60 lines (51 loc) · 2.03 KB
/
profile_studio.py
File metadata and controls
60 lines (51 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""Persistence helpers for profiles and templates."""
from __future__ import annotations
import json
from pathlib import Path
from form_engine import ProxyConfig
class ProfileStudioStore:
def __init__(
self,
profiles_file: Path,
templates_file: Path,
default_data: dict,
default_settings: dict,
default_templates: dict,
):
self.profiles_file = profiles_file
self.templates_file = templates_file
self.default_data = default_data
self.default_settings = default_settings
self.default_templates = default_templates
def load_profiles(self, seed_user_data: dict, seed_proxy: ProxyConfig) -> dict:
if self.profiles_file.exists():
try:
with open(self.profiles_file, "r", encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, dict) and data:
return data
except Exception:
pass
return {
"default": {
"user_data": dict(seed_user_data),
"proxy": seed_proxy.to_dict(),
"settings": dict(self.default_settings),
}
}
def save_profiles(self, profiles: dict) -> None:
with open(self.profiles_file, "w", encoding="utf-8") as f:
json.dump(profiles, f, ensure_ascii=False, indent=2)
def load_templates(self) -> dict:
if self.templates_file.exists():
try:
with open(self.templates_file, "r", encoding="utf-8") as f:
loaded = json.load(f)
if isinstance(loaded, dict):
return {**self.default_templates, **loaded}
except Exception:
pass
return dict(self.default_templates)
def save_templates(self, templates: dict) -> None:
with open(self.templates_file, "w", encoding="utf-8") as f:
json.dump(templates, f, ensure_ascii=False, indent=2)