|
| 1 | +use super::{ExecutionResult, LanguagePlugin}; |
| 2 | + |
| 3 | +pub struct Python3Plugin; |
| 4 | + |
| 5 | +impl LanguagePlugin for Python3Plugin { |
| 6 | + fn get_language_name(&self) -> &'static str { |
| 7 | + "Python 3" |
| 8 | + } |
| 9 | + |
| 10 | + fn get_file_extension(&self) -> &'static str { |
| 11 | + "py" |
| 12 | + } |
| 13 | + |
| 14 | + fn get_commands(&self) -> Vec<&'static str> { |
| 15 | + vec!["python", "python3"] |
| 16 | + } |
| 17 | + |
| 18 | + fn get_version_args(&self) -> Vec<&'static str> { |
| 19 | + vec!["--version"] |
| 20 | + } |
| 21 | + |
| 22 | + fn get_execute_args(&self, file_path: &str) -> Vec<String> { |
| 23 | + vec![file_path.to_string()] |
| 24 | + } |
| 25 | + |
| 26 | + fn get_path_command(&self) -> String { |
| 27 | + "import sys; print(sys.executable)".to_string() |
| 28 | + } |
| 29 | + |
| 30 | + fn pre_execute_hook(&self, code: &str) -> Result<String, String> { |
| 31 | + // 添加一些 Python 特定的预处理 |
| 32 | + let processed_code = format!( |
| 33 | + "# CodeForge Python Execution\n# Generated at: {}\n\n{}", |
| 34 | + chrono::Utc::now().format("%Y-%m-%d %H:%M:%S UTC"), |
| 35 | + code |
| 36 | + ); |
| 37 | + Ok(processed_code) |
| 38 | + } |
| 39 | + |
| 40 | + fn post_execute_hook(&self, result: &mut ExecutionResult) -> Result<(), String> { |
| 41 | + // Python 特定的后处理 |
| 42 | + if result.success && result.stdout.is_empty() && result.stderr.is_empty() { |
| 43 | + result.stdout = "Code executed successfully (no output)".to_string(); |
| 44 | + } |
| 45 | + |
| 46 | + // 清理 Python 特定的错误信息 |
| 47 | + if !result.stderr.is_empty() { |
| 48 | + result.stderr = result |
| 49 | + .stderr |
| 50 | + .replace("Traceback (most recent call last):", "Error:"); |
| 51 | + } |
| 52 | + |
| 53 | + Ok(()) |
| 54 | + } |
| 55 | +} |
0 commit comments