-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
221 lines (180 loc) · 8.1 KB
/
config.py
File metadata and controls
221 lines (180 loc) · 8.1 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
"""
Configuration Management for TFLite Backend
This module provides centralized configuration management with validation,
default values, and environment variable loading.
"""
import os
from typing import Dict, Any, Tuple, Optional
from pathlib import Path
# Load environment variables from .env file
try:
from dotenv import load_dotenv
env_path = Path(__file__).parent / '.env'
load_dotenv(dotenv_path=env_path)
except ImportError:
pass
class ConfigurationError(Exception):
"""Exception raised when configuration is invalid or missing."""
pass
class Config:
"""
Application configuration with validation and defaults.
Environment Variables:
VITE_BLOCKLY_API: URL of the Blockly compiler service
COMPILATION_TIMEOUT: Timeout for compilation requests in seconds (default: 120)
DEFAULT_BOARD: Default board type for compilation (default: esp32:esp32:sensebox_eye)
DEFAULT_OPTIMIZATION: Default optimization level (default: default)
MAX_CONTENT_LENGTH: Maximum request size in bytes (default: 10MB)
CONVERSION_TIMEOUT: Timeout for TFLite conversion in seconds (default: 60)
FLASK_ENV: Flask environment (development, production, testing)
DEBUG: Enable debug mode (default: False)
"""
# Required configuration keys
REQUIRED_KEYS = ['VITE_BLOCKLY_API']
# Default values
DEFAULTS = {
'COMPILATION_TIMEOUT': 120,
'DEFAULT_BOARD': 'esp32:esp32:sensebox_eye',
'DEFAULT_OPTIMIZATION': 'default',
'MAX_CONTENT_LENGTH': 10 * 1024 * 1024, # 10MB
'CONVERSION_TIMEOUT': 60,
'FLASK_ENV': 'development',
'DEBUG': False,
'MAX_RETRIES': 3,
'RETRY_DELAY': 2,
'TEMP_DIR_PREFIX': 'tflite_conversion_',
}
# Valid optimization levels
VALID_OPTIMIZATIONS = ['default', 'size', 'speed']
# Timeout constraints
MIN_TIMEOUT = 10 # seconds
MAX_TIMEOUT = 600 # 10 minutes
def __init__(self):
"""Initialize configuration from environment variables."""
self._config = {}
self._load_from_env()
def _load_from_env(self):
"""Load configuration from environment variables with defaults."""
# Load required configuration
for key in self.REQUIRED_KEYS:
value = os.getenv(key)
if value:
self._config[key] = value
# Load optional configuration with defaults
for key, default_value in self.DEFAULTS.items():
env_value = os.getenv(key)
if env_value is not None:
# Type conversion
if isinstance(default_value, bool):
self._config[key] = env_value.lower() in ('true', '1', 'yes')
elif isinstance(default_value, int):
try:
self._config[key] = int(env_value)
except ValueError:
self._config[key] = default_value
else:
self._config[key] = env_value
else:
self._config[key] = default_value
def get(self, key: str, default: Any = None) -> Any:
"""
Get configuration value.
Args:
key: Configuration key
default: Default value if key not found
Returns:
Configuration value or default
"""
return self._config.get(key, default)
def validate(self) -> Tuple[bool, Optional[str]]:
"""
Validate configuration values.
Returns:
Tuple[bool, Optional[str]]: (is_valid, error_message)
"""
# Check required configuration
for key in self.REQUIRED_KEYS:
if key not in self._config or not self._config[key]:
return False, f"Required configuration '{key}' is missing. Please set it in your environment or .env file."
# Validate compiler URL format
compiler_url = self._config.get('VITE_BLOCKLY_API', '')
if not compiler_url.startswith(('http://', 'https://')):
return False, f"Invalid VITE_BLOCKLY_API URL format: '{compiler_url}'. Must start with http:// or https://"
# Validate compilation timeout
compilation_timeout = self._config.get('COMPILATION_TIMEOUT', 0)
if not isinstance(compilation_timeout, int) or compilation_timeout < self.MIN_TIMEOUT:
return False, f"COMPILATION_TIMEOUT must be at least {self.MIN_TIMEOUT} seconds, got: {compilation_timeout}"
if compilation_timeout > self.MAX_TIMEOUT:
return False, f"COMPILATION_TIMEOUT cannot exceed {self.MAX_TIMEOUT} seconds, got: {compilation_timeout}"
# Validate conversion timeout
conversion_timeout = self._config.get('CONVERSION_TIMEOUT', 0)
if not isinstance(conversion_timeout, int) or conversion_timeout < self.MIN_TIMEOUT:
return False, f"CONVERSION_TIMEOUT must be at least {self.MIN_TIMEOUT} seconds, got: {conversion_timeout}"
if conversion_timeout > self.MAX_TIMEOUT:
return False, f"CONVERSION_TIMEOUT cannot exceed {self.MAX_TIMEOUT} seconds, got: {conversion_timeout}"
# Validate optimization level
optimization = self._config.get('DEFAULT_OPTIMIZATION', 'default')
if optimization not in self.VALID_OPTIMIZATIONS:
return False, f"Invalid DEFAULT_OPTIMIZATION: '{optimization}'. Must be one of: {', '.join(self.VALID_OPTIMIZATIONS)}"
# Validate max content length
max_content = self._config.get('MAX_CONTENT_LENGTH', 0)
if not isinstance(max_content, int) or max_content <= 0:
return False, f"MAX_CONTENT_LENGTH must be a positive integer, got: {max_content}"
# Validate max retries
max_retries = self._config.get('MAX_RETRIES', 0)
if not isinstance(max_retries, int) or max_retries < 1 or max_retries > 10:
return False, f"MAX_RETRIES must be between 1 and 10, got: {max_retries}"
return True, None
def validate_or_raise(self):
"""
Validate configuration and raise exception if invalid.
Raises:
ConfigurationError: If configuration is invalid
"""
is_valid, error_msg = self.validate()
if not is_valid:
raise ConfigurationError(error_msg)
def to_dict(self) -> Dict[str, Any]:
"""
Get configuration as dictionary.
Returns:
Dict containing all configuration values
"""
return self._config.copy()
def __repr__(self) -> str:
"""String representation of configuration."""
# Hide sensitive values
safe_config = self._config.copy()
if 'VITE_BLOCKLY_API' in safe_config:
safe_config['VITE_BLOCKLY_API'] = safe_config['VITE_BLOCKLY_API'][:20] + '...'
return f"Config({safe_config})"
# Global configuration instance
config = Config()
def get_config() -> Config:
"""
Get the global configuration instance.
Returns:
Config: Global configuration instance
"""
return config
def validate_config_on_startup():
"""
Validate configuration on application startup.
This function should be called when the Flask app initializes
to ensure all required configuration is present and valid.
Raises:
ConfigurationError: If configuration is invalid
"""
config.validate_or_raise()
print("\n" + "="*60)
print("Configuration validated successfully!")
print("="*60)
print(f"Compiler URL: {config.get('VITE_BLOCKLY_API')}")
print(f"Compilation Timeout: {config.get('COMPILATION_TIMEOUT')}s")
print(f"Conversion Timeout: {config.get('CONVERSION_TIMEOUT')}s")
print(f"Default Board: {config.get('DEFAULT_BOARD')}")
print(f"Default Optimization: {config.get('DEFAULT_OPTIMIZATION')}")
print(f"Max Content Length: {config.get('MAX_CONTENT_LENGTH') / (1024*1024):.1f}MB")
print(f"Environment: {config.get('FLASK_ENV')}")
print(f"Debug Mode: {config.get('DEBUG')}")
print("="*60 + "\n")