-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
148 lines (128 loc) · 4.86 KB
/
config.py
File metadata and controls
148 lines (128 loc) · 4.86 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""Configuration management for the function visualizer."""
import json
import os
from pathlib import Path
class Config:
"""Configuration manager for the visualizer application."""
def __init__(self, config_file="config.json"):
self.config_file = Path(config_file)
self.default_config = {
"window": {
"width": 800,
"height": 600,
"title": "Function Visualizer"
},
"visualization": {
"default_time_step": 0.05,
"default_visual_fidelity": 100.0,
"frame_rate": 20, # FPS
"update_interval_ms": 50,
"auto_randomize_enabled": True,
"auto_randomize_interval_sec": 5,
"auto_randomize_from_saved": False
},
"performance": {
"enable_gpu": True,
"max_resolution": 1920,
"min_resolution": 320,
"auto_adjust_fidelity": True,
"auto_scaling": True
},
"ui": {
"show_toolbar": True,
"show_fps": True,
"show_hardware_info": True,
"theme": "default"
},
"saving": {
"auto_save_interval": 0, # 0 = disabled
"save_directory": "saves",
"image_format": "PNG",
"video_format": "MP4"
},
"logging": {
"level": "INFO",
"log_to_file": True,
"log_file": "visualizer.log"
}
}
self.config = self.load_config()
def load_config(self):
"""Load configuration from file or create default."""
if self.config_file.exists():
try:
with open(self.config_file, 'r') as f:
loaded_config = json.load(f)
# Merge with defaults to ensure all keys exist
return self._merge_configs(self.default_config, loaded_config)
except (json.JSONDecodeError, IOError) as e:
print(f"Error loading config: {e}. Using defaults.")
return self.default_config.copy()
else:
self.save_config(self.default_config)
return self.default_config.copy()
def _merge_configs(self, default, loaded):
"""Recursively merge loaded config with defaults."""
result = default.copy()
for key, value in loaded.items():
if key in result and isinstance(result[key], dict) and isinstance(value, dict):
result[key] = self._merge_configs(result[key], value)
else:
result[key] = value
return result
def save_config(self, config=None):
"""Save current configuration to file."""
if config is None:
config = self.config
try:
self.config_file.parent.mkdir(parents=True, exist_ok=True)
with open(self.config_file, 'w') as f:
json.dump(config, f, indent=2)
except IOError as e:
print(f"Error saving config: {e}")
def get(self, key_path, default=None):
"""Get configuration value using dot notation (e.g., 'window.width')."""
keys = key_path.split('.')
value = self.config
try:
for key in keys:
value = value[key]
return value
except (KeyError, TypeError):
return default
def set(self, key_path, value):
"""Set configuration value using dot notation."""
keys = key_path.split('.')
config = self.config
# Navigate to the parent of the target key
for key in keys[:-1]:
if key not in config:
config[key] = {}
config = config[key]
# Set the value
config[keys[-1]] = value
self.save_config()
def reset_to_defaults(self):
"""Reset configuration to default values."""
self.config = self.default_config.copy()
self.save_config()
def get_window_config(self):
"""Get window configuration."""
return self.get('window', {})
def get_visualization_config(self):
"""Get visualization configuration."""
return self.get('visualization', {})
def get_performance_config(self):
"""Get performance configuration."""
return self.get('performance', {})
def get_ui_config(self):
"""Get UI configuration."""
return self.get('ui', {})
def get_saving_config(self):
"""Get saving configuration."""
return self.get('saving', {})
def get_logging_config(self):
"""Get logging configuration."""
return self.get('logging', {})
# Global configuration instance
config = Config()