forked from devlive-community/codeforge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython2.rs
More file actions
55 lines (44 loc) · 1.52 KB
/
python2.rs
File metadata and controls
55 lines (44 loc) · 1.52 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
use super::{ExecutionResult, LanguagePlugin};
pub struct Python2Plugin;
impl LanguagePlugin for Python2Plugin {
fn get_language_name(&self) -> &'static str {
"Python 2"
}
fn get_file_extension(&self) -> &'static str {
"py"
}
fn get_commands(&self) -> Vec<&'static str> {
vec!["python", "python2"]
}
fn get_version_args(&self) -> Vec<&'static str> {
vec!["--version"]
}
fn get_execute_args(&self, file_path: &str) -> Vec<String> {
vec![file_path.to_string()]
}
fn get_path_command(&self) -> String {
"import sys; print(sys.executable)".to_string()
}
fn pre_execute_hook(&self, code: &str) -> Result<String, String> {
// 添加一些 Python 特定的预处理
let processed_code = format!(
"# CodeForge Python Execution\n# Generated at: {}\n\n{}",
chrono::Utc::now().format("%Y-%m-%d %H:%M:%S UTC"),
code
);
Ok(processed_code)
}
fn post_execute_hook(&self, result: &mut ExecutionResult) -> Result<(), String> {
// Python 特定的后处理
if result.success && result.stdout.is_empty() && result.stderr.is_empty() {
result.stdout = "代码执行成功 (无输出)".to_string();
}
// 清理 Python 特定的错误信息
if !result.stderr.is_empty() {
result.stderr = result
.stderr
.replace("Traceback (most recent call last):", "Error:");
}
Ok(())
}
}