Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/python inline task #275

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ dependencies {

implementation "org.openjdk.nashorn:nashorn-core:15.4"

//jython dependency
implementation "org.python:jython-standalone:2.7.4"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using something like https://www.graalvm.org/python/


// JAXB is not bundled with Java 11, dependencies added explicitly
// These are needed by Apache BVAL
implementation "jakarta.xml.bind:jakarta.xml.bind-api:${revJAXB}"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2024 Conductor Authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.netflix.conductor.core.execution.evaluators;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Map;
import java.util.Properties;

import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
import org.springframework.stereotype.Component;

@Component(PythonEvaluator.NAME)
public class PythonEvaluator implements Evaluator {
public static final String NAME = "python";

@Override
public Object evaluate(String expression, Object inputs) {

PythonInterpreter interpreter = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

try {
Properties props = new Properties();
props.setProperty("python.import.site", "false");
PythonInterpreter.initialize(System.getProperties(), props, new String[] {});

interpreter = new PythonInterpreter();
interpreter.setOut(new PrintStream(outputStream));

if (inputs instanceof Map) {
Map<String, Object> input = (Map<String, Object>) inputs;

// Set variables in the interpreter
for (Map.Entry<String, Object> entry : input.entrySet()) {
interpreter.set(entry.getKey(), entry.getValue());
}
// Build the global declaration dynamically
StringBuilder globalDeclaration = new StringBuilder("def evaluate():\n global ");

for (Map.Entry<String, Object> entry : input.entrySet()) {
globalDeclaration.append(entry.getKey()).append(", ");
}

// Remove the trailing comma and space, and add a newline
if (globalDeclaration.length() > 0) {
globalDeclaration.setLength(globalDeclaration.length() - 2);
}
globalDeclaration.append("\n");

// Wrap the expression in a function to handle multi-line statements
String wrappedExpression =
globalDeclaration.toString() // Add global declaration line
+ " "
+ expression.replace(
"\n", "\n ") // Indent the original expression
+ "\n"
+ "result = evaluate()";

// Execute the wrapped expression
interpreter.exec(wrappedExpression);

// Get the result
PyObject result = interpreter.get("result");
return result.__tojava__(Object.class);
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (interpreter != null) {
interpreter.close();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2024 Conductor Authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.netflix.conductor.sdk.workflow.def.tasks;

import com.netflix.conductor.common.metadata.tasks.TaskType;

public class Python extends Task<Python> {
public Python(String taskReferenceName, String script) {
super(taskReferenceName, TaskType.INLINE);
if (script == null || script.isEmpty()) {
throw new IllegalArgumentException("Python script cannot be null or empty");
}
super.input("evaluatorType", "python");
super.input("expression", script);
}
}