-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
49 lines (42 loc) · 1.75 KB
/
Copy pathconfig.py
File metadata and controls
49 lines (42 loc) · 1.75 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
import json
from pathlib import Path
from typing import Any, Dict
DEFAULT_CONFIG: Dict[str, Any] = {
"delay_seconds": 0.1,
"mouse_button": "left", # options: left, right, middle
"click_type": "single", # options: single, double
"hotkey": "f8",
"random_delay_range": [0.0, 0.05],
"hold_time_seconds": 0.01
}
class ConfigLoader:
def __init__(self, config_path: str = "config.json") -> None:
self.config_path = Path(config_path)
self.config = self.load_config()
def load_config(self) -> Dict[str, Any]:
"""Loads configuration from a file, falling back to defaults for missing options."""
if not self.config_path.exists():
self.save_defaults()
return DEFAULT_CONFIG.copy()
try:
with open(self.config_path, "r", encoding="utf-8") as f:
user_data = json.load(f)
# Merge loaded configurations with the defaults
merged = DEFAULT_CONFIG.copy()
for key, val in user_data.items():
if key in merged and isinstance(val, type(merged[key])):
merged[key] = val
return merged
except (json.JSONDecodeError, OSError):
# Return default config if file is corrupted or unreadable
return DEFAULT_CONFIG.copy()
def save_defaults(self) -> None:
"""Saves standard default settings to configuration file."""
try:
with open(self.config_path, "w", encoding="utf-8") as f:
json.dump(DEFAULT_CONFIG, f, indent=4)
except OSError:
pass
def get(self, key: str) -> Any:
"""Access config options with safety fallback."""
return self.config.get(key, DEFAULT_CONFIG.get(key))