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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Python syntax validation for package.py files.
use super::{Severity, ValidationIssue, Validator};
use crate::core::Result;
use regex::Regex;
/// Validates Python syntax in package.py files.
pub struct PythonValidator {
/// Regex patterns for common Python syntax issues
patterns: PythonPatterns,
}
struct PythonPatterns {
/// Pattern for detecting invalid indentation
#[allow(dead_code)]
invalid_indent: Regex,
/// Pattern for detecting unclosed brackets
#[allow(dead_code)]
unclosed_brackets: Regex,
/// Pattern for detecting invalid string literals
#[allow(dead_code)]
invalid_strings: Regex,
/// Pattern for detecting invalid variable names
invalid_names: Regex,
/// Pattern for detecting missing colons
missing_colons: Regex,
}
impl PythonValidator {
/// Create a new Python validator.
pub fn new() -> Result<Self> {
let patterns = PythonPatterns {
invalid_indent: Regex::new(r"^[ \t]+[^ \t#]")?,
unclosed_brackets: Regex::new(r"[\[\(\{][^\]\)\}]*$")?,
invalid_strings: Regex::new(r#"(["'])[^"']*$"#)?,
invalid_names: Regex::new(r"\b\d+[a-zA-Z_]")?,
missing_colons: Regex::new(
r"^\s*(if|elif|else|for|while|def|class|try|except|finally|with)\b[^:]*$",
)?,
};
Ok(Self { patterns })
}
/// Check for indentation issues.
fn check_indentation(&self, content: &str) -> Vec<ValidationIssue> {
let mut issues = Vec::new();
for (line_num, line) in content.lines().enumerate() {
let line_num = line_num as u32 + 1;
// Skip empty lines and comments
if line.trim().is_empty() || line.trim().starts_with('#') {
continue;
}
let indent = line.len() - line.trim_start().len();
// Check for mixed tabs and spaces
if line.starts_with('\t') && line.contains(' ') {
issues.push(
ValidationIssue::new(
Severity::Error,
line_num,
1,
indent as u32,
"Mixed tabs and spaces in indentation",
"E101",
)
.with_suggestion("Use either tabs or spaces consistently"),
);
}
// Simple indentation check: if previous line ended with ':', this line should be indented
if line_num > 1 {
let lines: Vec<&str> = content.lines().collect();
if let Some(prev_line) = lines.get((line_num - 2) as usize) {
if prev_line.trim().ends_with(':') && indent == 0 && !line.trim().is_empty() {
issues.push(
ValidationIssue::new(
Severity::Error,
line_num,
1,
1,
"Expected an indented block",
"E111",
)
.with_suggestion("Indent this line"),
);
}
}
}
}
issues
}
/// Check for syntax errors.
fn check_syntax_errors(&self, content: &str) -> Vec<ValidationIssue> {
let mut issues = Vec::new();
for (line_num, line) in content.lines().enumerate() {
let line_num = line_num as u32 + 1;
let trimmed = line.trim();
// Skip empty lines and comments
if trimmed.is_empty() || trimmed.starts_with('#') {
continue;
}
// Check for missing colons
if self.patterns.missing_colons.is_match(trimmed) {
let col = line.len() as u32;
issues.push(
ValidationIssue::new(
Severity::Error,
line_num,
col,
1,
"Missing colon at end of statement",
"E999",
)
.with_suggestion("Add ':' at the end of the line"),
);
}
// Check for unclosed strings
if self.check_unclosed_strings(line) {
issues.push(
ValidationIssue::new(
Severity::Error,
line_num,
1,
line.len() as u32,
"Unclosed string literal",
"E902",
)
.with_suggestion("Close the string literal"),
);
}
// Check for invalid variable names
if let Some(mat) = self.patterns.invalid_names.find(trimmed) {
issues.push(
ValidationIssue::new(
Severity::Error,
line_num,
mat.start() as u32 + 1,
mat.len() as u32,
"Invalid variable name (cannot start with digit)",
"E999",
)
.with_suggestion("Variable names must start with a letter or underscore"),
);
}
}
issues
}
/// Check for unclosed string literals.
fn check_unclosed_strings(&self, line: &str) -> bool {
let mut in_single_quote = false;
let mut in_double_quote = false;
let mut escaped = false;
for ch in line.chars() {
if escaped {
escaped = false;
continue;
}
match ch {
'\\' => escaped = true,
'\'' if !in_double_quote => in_single_quote = !in_single_quote,
'"' if !in_single_quote => in_double_quote = !in_double_quote,
_ => {}
}
}
in_single_quote || in_double_quote
}
/// Check for bracket matching.
fn check_bracket_matching(&self, content: &str) -> Vec<ValidationIssue> {
let mut issues = Vec::new();
let mut bracket_stack = Vec::new();
let mut _line_positions: Vec<u32> = Vec::new();
// Track line positions for error reporting
let mut current_line = 1u32;
let mut current_col = 1u32;
for ch in content.chars() {
match ch {
'(' | '[' | '{' => {
bracket_stack.push((ch, current_line, current_col));
}
')' | ']' | '}' => {
if let Some((open_bracket, _open_line, _open_col)) = bracket_stack.pop() {
let expected_close = match open_bracket {
'(' => ')',
'[' => ']',
'{' => '}',
_ => unreachable!(),
};
if ch != expected_close {
issues.push(
ValidationIssue::new(
Severity::Error,
current_line,
current_col,
1,
format!(
"Mismatched bracket: expected '{}', found '{}'",
expected_close, ch
),
"E999",
)
.with_suggestion(format!(
"Change '{}' to '{}'",
ch, expected_close
)),
);
}
} else {
issues.push(
ValidationIssue::new(
Severity::Error,
current_line,
current_col,
1,
format!("Unmatched closing bracket '{}'", ch),
"E999",
)
.with_suggestion(
"Remove the extra closing bracket or add matching opening bracket",
),
);
}
}
'\n' => {
current_line += 1;
current_col = 1;
continue;
}
_ => {}
}
current_col += 1;
}
// Check for unclosed brackets
for (bracket, line, col) in bracket_stack {
let expected_close = match bracket {
'(' => ')',
'[' => ']',
'{' => '}',
_ => unreachable!(),
};
issues.push(
ValidationIssue::new(
Severity::Error,
line,
col,
1,
format!("Unclosed bracket '{}'", bracket),
"E999",
)
.with_suggestion(format!("Add closing bracket '{}'", expected_close)),
);
}
issues
}
/// Check for common Python style issues.
fn check_style_issues(&self, content: &str) -> Vec<ValidationIssue> {
let mut issues = Vec::new();
for (line_num, line) in content.lines().enumerate() {
let line_num = line_num as u32 + 1;
// Check line length (PEP 8 recommends 79 characters)
if line.len() > 79 {
issues.push(
ValidationIssue::new(
Severity::Warning,
line_num,
80,
(line.len() - 79) as u32,
"Line too long (>79 characters)",
"W501",
)
.with_suggestion("Break long lines or use line continuation"),
);
}
// Check for trailing whitespace
if line.ends_with(' ') || line.ends_with('\t') {
let trimmed_len = line.trim_end().len();
issues.push(
ValidationIssue::new(
Severity::Warning,
line_num,
trimmed_len as u32 + 1,
(line.len() - trimmed_len) as u32,
"Trailing whitespace",
"W291",
)
.with_suggestion("Remove trailing whitespace"),
);
}
}
issues
}
}
impl Default for PythonValidator {
fn default() -> Self {
Self::new().expect("Failed to create PythonValidator")
}
}
impl Validator for PythonValidator {
fn validate(&self, content: &str, _file_path: &str) -> Result<Vec<ValidationIssue>> {
let mut issues = Vec::new();
// Run all validation checks
issues.extend(self.check_indentation(content));
issues.extend(self.check_syntax_errors(content));
issues.extend(self.check_bracket_matching(content));
issues.extend(self.check_style_issues(content));
// Sort issues by line number, then by column
issues.sort_by(|a, b| a.line.cmp(&b.line).then_with(|| a.column.cmp(&b.column)));
Ok(issues)
}
fn name(&self) -> &str {
"PythonValidator"
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_python_validator_creation() {
let validator = PythonValidator::new();
assert!(validator.is_ok());
}
#[test]
fn test_valid_python_code() {
let validator = PythonValidator::new().unwrap();
let content = r#"
name = "test"
version = "1.0.0"
description = "A test package"
def build():
pass
"#;
let issues = validator.validate(content, "test.py").unwrap();
// Should have minimal issues (maybe style warnings)
assert!(issues.iter().all(|i| i.severity != Severity::Error));
}
#[test]
fn test_syntax_errors() {
let validator = PythonValidator::new().unwrap();
let content = r#"
name = "test
version = 1.0.0"
def build(
pass
"#;
let issues = validator.validate(content, "test.py").unwrap();
assert!(issues.iter().any(|i| i.severity == Severity::Error));
}
#[test]
fn test_indentation_errors() {
let validator = PythonValidator::new().unwrap();
let content = r#"def build():
pass"#;
let issues = validator.validate(content, "test.py").unwrap();
// Debug print to see what issues we get
for issue in &issues {
println!("Issue: {} - {}", issue.code, issue.message);
}
assert!(issues.iter().any(|i| i.code.starts_with("E1")));
}
#[test]
fn test_bracket_matching() {
let validator = PythonValidator::new().unwrap();
let content = r#"requires = ["python", "maya"
tools = {"tool1": "path1"
"#;
let issues = validator.validate(content, "test.py").unwrap();
// Debug print to see what issues we get
for issue in &issues {
println!("Issue: {} - {}", issue.code, issue.message);
}
// This should find unclosed brackets
assert!(issues
.iter()
.any(|i| i.message.contains("bracket") || i.message.contains("Unclosed")));
}
}