mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-15 12:32:09 +08:00
feature #I64PDW 增加Python脚本支持
This commit is contained in:
27
liteflow-script-plugin/liteflow-script-python/pom.xml
Normal file
27
liteflow-script-plugin/liteflow-script-python/pom.xml
Normal file
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-script-plugin</artifactId>
|
||||
<version>2.9.5</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>liteflow-script-python</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-core</artifactId>
|
||||
<version>${revision}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.python</groupId>
|
||||
<artifactId>jython-standalone</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.yomahub.liteflow.script.python;
|
||||
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import cn.hutool.core.util.CreditCodeUtil;
|
||||
import cn.hutool.core.util.ReUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.script.jsr223.JSR223ScriptExecutor;
|
||||
import org.python.antlr.PythonParser;
|
||||
import org.python.util.CodegenUtils;
|
||||
import org.python.util.JycompileAntTask;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Python脚本语言的执行器实现
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.9.5
|
||||
*/
|
||||
public class PythonScriptExecutor extends JSR223ScriptExecutor {
|
||||
|
||||
@Override
|
||||
protected String scriptEngineName() {
|
||||
return "python";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String convertScript(String script) {
|
||||
String[] lineArray = script.split("\\n");
|
||||
List<String> noBlankLineList = Arrays.stream(lineArray).filter(
|
||||
s -> !StrUtil.isBlank(s)
|
||||
).collect(Collectors.toList());
|
||||
|
||||
//用第一行的缩进的空格数作为整个代码的缩进量
|
||||
String blankStr = ReUtil.getGroup0("^[ ]*", noBlankLineList.get(0));
|
||||
|
||||
//重新构建python脚本
|
||||
StringBuilder scriptSB = new StringBuilder();
|
||||
noBlankLineList.forEach(s
|
||||
-> scriptSB.append(StrUtil.format("{}\n", s.replaceFirst(blankStr, StrUtil.EMPTY))));
|
||||
|
||||
return scriptSB.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
# Python的实现
|
||||
com.yomahub.liteflow.script.python.PythonScriptExecutor
|
||||
Reference in New Issue
Block a user