-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.py
More file actions
279 lines (227 loc) · 9.18 KB
/
Copy pathconfig.py
File metadata and controls
279 lines (227 loc) · 9.18 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import configparser
import os
from typing import Any, Dict
from library.utils.class_dict import ClassDict
# Define configuration sections
SECTION_GENERAL = "General"
SECTION_CONVERSION = "Conversion"
# Define configuration file path
CONFIG_FILE_NAME = "nfs-resources-converter-settings.ini"
CONFIG_FILE_PATH = os.path.join(os.path.expanduser("~"), CONFIG_FILE_NAME)
# Define log file path
LOG_FILE_NAME = "nfs-resources-converter-logs.log"
LOG_FILE_PATH = os.path.join(os.path.expanduser("~"), LOG_FILE_NAME)
# Function to get the config file location
def get_config_file_location():
"""
Get the location of the configuration file.
Returns:
str: The full path to the configuration file
"""
return CONFIG_FILE_PATH
class ConfigManager:
"""
Configuration manager for NFS Resources Converter.
Handles loading configuration from different sources and provides
a unified interface for accessing configuration values.
"""
def __init__(self):
self._config = configparser.ConfigParser()
self._defaults = self._get_defaults()
self._load_config()
def _get_defaults(self) -> Dict[str, Dict[str, Any]]:
"""
Get default configuration values.
Returns:
Dict with default configuration values
"""
return {
SECTION_GENERAL: {
"blender_executable": "blender",
"ffmpeg_executable": "ffmpeg",
"print_blender_log": False,
"recent_files": [],
"show_hidden_fields": False,
},
SECTION_CONVERSION: {
"multiprocess_processes_count": 0,
"input_path": "",
"output_path": "",
"images__save_images_only": False,
"maps__save_as_chunked": False,
"maps__save_invisible_wall_collisions": False,
"maps__save_terrain_collisions": False,
"maps__save_spherical_skybox_texture": True,
"maps__add_props_to_obj": True,
"geometry__save_obj": True,
"geometry__save_blend": True,
"geometry__export_to_gg_web_engine": False,
},
}
def _load_config(self):
"""
Load configuration from file if it exists.
"""
# Create sections in config
for section in self._defaults:
if not self._config.has_section(section):
self._config.add_section(section)
# Load from config file if it exists
if os.path.exists(CONFIG_FILE_PATH):
self._config.read(CONFIG_FILE_PATH)
def _get_env_var_name(self, section: str, key: str) -> str:
"""
Get environment variable name for a configuration key.
Args:
section: Configuration section
key: Configuration key
Returns:
Environment variable name
"""
return f"NFS_RESOURCES_CONVERTER_{section.upper()}_{key.upper()}"
def get(self, section: str, key: str) -> Any:
"""
Get configuration value.
Args:
section: Configuration section
key: Configuration key
default: Default value if not found
Returns:
Configuration value
"""
default = self._get_defaults().get(section, {}).get(key)
# Check environment variable first
env_var_name = self._get_env_var_name(section, key)
env_value = os.environ.get(env_var_name)
if env_value is not None:
return self._convert_value(env_value, default)
# Check config file
try:
if self._config.has_option(section, key):
value = self._config.get(section, key)
return self._convert_value(value, default)
except (configparser.NoSectionError, configparser.NoOptionError):
pass
# Check defaults
if section in self._defaults and key in self._defaults[section]:
return self._defaults[section][key]
# Return provided default or None
return default
def _convert_value(self, value: str, default: Any) -> Any:
"""
Convert string value to appropriate type based on default value.
Args:
value: String value to convert
default: Default value used for type inference
Returns:
Converted value
"""
if default is None:
return value
if isinstance(default, bool):
return value.lower() in ('true', 'yes', '1', 'y', 't')
elif isinstance(default, int):
return int(value)
elif isinstance(default, float):
return float(value)
elif isinstance(default, list):
return value.split(',')
elif isinstance(default, dict):
# For dictionaries, we don't support conversion from string
# They should be accessed directly from defaults
return default
else:
return value
def create_default_config_file(self):
"""
Create a default configuration file.
"""
for section, options in self._defaults.items():
if not self._config.has_section(section):
self._config.add_section(section)
for key, value in options.items():
if isinstance(value, dict):
# Skip dictionaries, they're handled specially
continue
if not self._config.has_option(section, key):
self._config.set(section, key, str(value))
with open(CONFIG_FILE_PATH, 'w') as config_file:
self._config.write(config_file)
def set(self, section: str, key: str, value: Any) -> None:
"""
Set configuration value and update config.ini file.
Args:
section: Configuration section
key: Configuration key
value: Value to set
"""
# Ensure section exists
if not self._config.has_section(section):
self._config.add_section(section)
# Set value in config
self._config.set(section, key, str(value))
# Write to config file
with open(CONFIG_FILE_PATH, 'w') as config_file:
self._config.write(config_file)
# Create a singleton instance
_config_manager = ConfigManager()
# Function to get configuration value
def get_config(section: str, key: str) -> Any:
"""
Get configuration value.
Args:
section: Configuration section
key: Configuration key
Returns:
Configuration value
"""
return _config_manager.get(section, key)
# Function to set configuration value
def set_config(section: str, key: str, value: Any) -> None:
"""
Set configuration value and update config.ini file.
Args:
section: Configuration section
key: Configuration key
value: Value to set
"""
# Set value in config manager
_config_manager.set(section, key, value)
# Update module attribute if it exists
module_attr_name = key
if section != SECTION_GENERAL:
module_attr_name = f"{section.lower()}__{key}"
if module_attr_name in globals():
globals()[module_attr_name] = value
def general_config(patch: Dict = None) -> ClassDict:
config = {
"blender_executable": get_config(SECTION_GENERAL, "blender_executable"),
"ffmpeg_executable": get_config(SECTION_GENERAL, "ffmpeg_executable"),
"print_blender_log": get_config(SECTION_GENERAL, "print_blender_log"),
"recent_files": get_config(SECTION_GENERAL, "recent_files"),
"show_hidden_fields": get_config(SECTION_GENERAL, "show_hidden_fields"),
}
if patch:
config = {**config, **patch}
return ClassDict.wrap(config)
def conversion_config(patch: Dict = None) -> ClassDict:
config = {
"multiprocess_processes_count": get_config(SECTION_CONVERSION, "multiprocess_processes_count"),
"input_path": get_config(SECTION_CONVERSION, "input_path"),
"output_path": get_config(SECTION_CONVERSION, "output_path"),
"images__save_images_only": get_config(SECTION_CONVERSION, "images__save_images_only"),
"maps__save_as_chunked": get_config(SECTION_CONVERSION, "maps__save_as_chunked"),
"maps__save_invisible_wall_collisions": get_config(SECTION_CONVERSION, "maps__save_invisible_wall_collisions"),
"maps__save_terrain_collisions": get_config(SECTION_CONVERSION, "maps__save_terrain_collisions"),
"maps__save_spherical_skybox_texture": get_config(SECTION_CONVERSION, "maps__save_spherical_skybox_texture"),
"maps__add_props_to_obj": get_config(SECTION_CONVERSION, "maps__add_props_to_obj"),
"geometry__save_obj": get_config(SECTION_CONVERSION, "geometry__save_obj"),
"geometry__save_blend": get_config(SECTION_CONVERSION, "geometry__save_blend"),
"geometry__export_to_gg_web_engine": get_config(SECTION_CONVERSION, "geometry__export_to_gg_web_engine"),
}
if patch:
config = {**config, **patch}
return ClassDict.wrap(config)
# Create default config file if it doesn't exist
if not os.path.exists(CONFIG_FILE_PATH):
_config_manager.create_default_config_file()