-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation_ui.py
More file actions
193 lines (157 loc) · 6.38 KB
/
validation_ui.py
File metadata and controls
193 lines (157 loc) · 6.38 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
"""
Validation UI - wxPython integration for CLI validator.
Functions for displaying validation results in the dialog UI.
"""
import wx
# Handle both package and standalone import
try:
from .cli_validator import CLIValidator, ValidationLevel, ValidationResult
except ImportError:
from cli_validator import CLIValidator, ValidationLevel, ValidationResult
from typing import Dict, List
def validate_and_log(dialog, params: dict, template_type: str = "") -> bool:
"""
Validate CLI parameters and log results with colors to dialog.
Args:
dialog: The RenderDialog instance (must have log/log_error/log_warning methods)
params: Dictionary of CLI parameters to validate
template_type: Optional template type for context
Returns:
True if validation passed (no errors), False if errors found
"""
validator = CLIValidator()
results = validator.validate(params)
if not results:
# No issues - log success in green
if hasattr(dialog, 'log_success'):
dialog.log_success("✓ CLI parameters validated successfully")
else:
dialog.log("✓ CLI parameters validated successfully")
return True
# Log each result with appropriate color
has_errors = False
dialog.log("-" * 40)
dialog.log(f"CLI Validation Results ({template_type or 'params'}):")
for result in results:
if result.level == ValidationLevel.ERROR:
has_errors = True
msg = f"❌ ERROR: {result.message}"
if result.param:
msg += f" [{result.param}]"
if hasattr(dialog, 'log_error'):
dialog.log_error(msg)
else:
dialog.log(msg)
if result.suggestion:
if hasattr(dialog, 'log_error'):
dialog.log_error(f" → {result.suggestion}")
else:
dialog.log(f" → {result.suggestion}")
elif result.level == ValidationLevel.WARNING:
msg = f"⚠️ WARNING: {result.message}"
if result.param:
msg += f" [{result.param}]"
if hasattr(dialog, 'log_warning'):
dialog.log_warning(msg)
else:
dialog.log(msg)
if result.suggestion:
if hasattr(dialog, 'log_warning'):
dialog.log_warning(f" → {result.suggestion}")
else:
dialog.log(f" → {result.suggestion}")
else: # INFO
msg = f"ℹ️ INFO: {result.message}"
dialog.log(msg)
dialog.log("-" * 40)
return not has_errors
def validate_before_render(dialog, params: dict, template_type: str = "") -> bool:
"""
Validate parameters before starting a render.
Shows error dialog if validation fails.
Args:
dialog: The RenderDialog instance
params: Dictionary of CLI parameters
template_type: Template type for context
Returns:
True if OK to proceed, False if should abort
"""
is_valid = validate_and_log(dialog, params, template_type)
if not is_valid:
# Show error dialog
wx.MessageBox(
"CLI parameter validation failed.\n\n"
"Check the log for details (errors shown in red).\n\n"
"Fix the errors and try again.",
"Validation Error",
wx.OK | wx.ICON_ERROR
)
return False
return True
def update_validation_status(dialog, params: dict, template_type: str):
"""
Run validation and update the status indicator in the UI.
This is called when parameters change to provide immediate feedback.
Args:
dialog: The RenderDialog instance
params: Dictionary of CLI parameters
template_type: Template type (spinrender/static/pinout)
"""
validator = CLIValidator()
results = validator.validate(params)
# Get the validation status control for this template type
status_ctrl_map = {
"spinrender": "anim_validation_status",
"static": "static_validation_status",
"pinout": "pinout_validation_status"
}
status_ctrl = getattr(dialog, status_ctrl_map.get(template_type, ""), None)
if status_ctrl:
if not results:
status_ctrl.SetLabel("✓")
status_ctrl.SetForegroundColour(wx.Colour(0, 140, 0))
status_ctrl.SetToolTip("All CLI parameters are valid")
elif validator.has_errors(results):
error_count = len(validator.get_errors(results))
status_ctrl.SetLabel(f"❌ {error_count}")
status_ctrl.SetForegroundColour(wx.Colour(200, 0, 0))
# Build tooltip with errors
error_msgs = [r.message for r in validator.get_errors(results)]
status_ctrl.SetToolTip("ERRORS:\n" + "\n".join(f"• {m}" for m in error_msgs[:5]))
else:
warning_count = len(validator.get_warnings(results))
status_ctrl.SetLabel(f"⚠️ {warning_count}")
status_ctrl.SetForegroundColour(wx.Colour(200, 120, 0))
# Build tooltip with warnings
warn_msgs = [r.message for r in validator.get_warnings(results)]
status_ctrl.SetToolTip("WARNINGS:\n" + "\n".join(f"• {m}" for m in warn_msgs[:5]))
# Refresh the parent layout
parent = status_ctrl.GetParent()
if parent:
parent.Layout()
# Store validation results for later use
setattr(dialog, f'_{template_type}_validation_results', results)
def format_validation_results(results: List[ValidationResult]) -> str:
"""
Format validation results as a human-readable string.
Args:
results: List of ValidationResult objects
Returns:
Formatted string
"""
if not results:
return "✓ All parameters valid"
lines = []
for r in results:
prefix = {
ValidationLevel.ERROR: "❌ ERROR",
ValidationLevel.WARNING: "⚠️ WARNING",
ValidationLevel.INFO: "ℹ️ INFO"
}.get(r.level, "?")
line = f"{prefix}: {r.message}"
if r.param:
line += f" (param: {r.param})"
if r.suggestion:
line += f"\n → {r.suggestion}"
lines.append(line)
return "\n".join(lines)