|
@@ -10,6 +10,8 @@ import (
|
|
|
"os"
|
|
|
"os/exec"
|
|
|
"reflect"
|
|
|
+ "regexp"
|
|
|
+ "strconv"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
@@ -86,6 +88,9 @@ func ExecPythonCode(edbCode, reqCode string) (dataMap models.EdbDataFromPython,
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
+ // 获取指定头部的换行符数量
|
|
|
+ frontNumN := getPythonFrontNumN()
|
|
|
+
|
|
|
defer func() {
|
|
|
os.Remove(pythonFile)
|
|
|
}()
|
|
@@ -102,6 +107,12 @@ func ExecPythonCode(edbCode, reqCode string) (dataMap models.EdbDataFromPython,
|
|
|
err = cmd.Run()
|
|
|
if err != nil {
|
|
|
errMsg = errMsgOut.String()
|
|
|
+ // 替换Python文件名
|
|
|
+ errMsg = strings.Replace(errMsgOut.String(), pythonFile, "python file", -1)
|
|
|
+ tmpErrMsg, tmpErr := replaceLineNumber(errMsg, frontNumN)
|
|
|
+ if tmpErr == nil {
|
|
|
+ errMsg = tmpErrMsg
|
|
|
+ }
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -209,3 +220,39 @@ func getPythonFront3Str() string {
|
|
|
func getPythonLaterStr() string {
|
|
|
return "\n\nprint(\"result=\", result.to_json())\ndb.close()"
|
|
|
}
|
|
|
+
|
|
|
+// getPythonFrontNumN
|
|
|
+// @Description: 获取指定头部的换行符数量
|
|
|
+// @author: Roc
|
|
|
+// @datetime 2024-05-08 10:15:51
|
|
|
+// @return int
|
|
|
+func getPythonFrontNumN() int {
|
|
|
+ re := regexp.MustCompile("\n")
|
|
|
+ matches := re.FindAllStringIndex(getPythonFrontStr(), -1)
|
|
|
+ return len(matches) // 每个匹配的结果对应一个换行符
|
|
|
+}
|
|
|
+
|
|
|
+func replaceLineNumber(errorText string, frontNumber int) (string, error) {
|
|
|
+ // 编译正则表达式,匹配 "line" 后面跟一个或多个空格,再跟一个或多个数字的模式
|
|
|
+ re := regexp.MustCompile(`line\s+\d+`)
|
|
|
+ match := re.FindStringSubmatch(errorText)
|
|
|
+ if match == nil {
|
|
|
+ return "", fmt.Errorf("未找到行号信息")
|
|
|
+ }
|
|
|
+
|
|
|
+ // 从匹配到的字符串中提取行号数字
|
|
|
+ oldLineNumberStr := match[0]
|
|
|
+ oldLineNumber, err := strconv.Atoi(oldLineNumberStr[len("line "):])
|
|
|
+ if err != nil {
|
|
|
+ return "", fmt.Errorf("解析行号失败: %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ newLineNumber := oldLineNumber - frontNumber
|
|
|
+
|
|
|
+ // 构建新的行号字符串
|
|
|
+ newLineNumberStr := fmt.Sprintf("line %d", newLineNumber)
|
|
|
+
|
|
|
+ // 替换原来的行号为新的行号
|
|
|
+ newErrorText := re.ReplaceAllString(errorText, newLineNumberStr)
|
|
|
+ return newErrorText, nil
|
|
|
+}
|