Skip to content

Commit

Permalink
[KOGITO-9354] Adding search path configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Jun 22, 2023
1 parent b10c23a commit 9c09b5a
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package org.kie.kogito.serverless.workflow.python;

import java.util.List;

import org.kie.kogito.serverless.workflow.utils.ConfigResolverHolder;

import jep.Interpreter;
import jep.SharedInterpreter;

Expand All @@ -23,13 +27,16 @@ public class PythonWorkItemHandlerUtils {
private PythonWorkItemHandlerUtils() {
}

private static ThreadLocal<Interpreter> interpreter = new ThreadLocal<>();
private static final String PYTHON_SYS_PATH = "sys.path.append('%s')\n";

private static final ThreadLocal<Interpreter> interpreter = new ThreadLocal<>();

protected static Interpreter interpreter() {
Interpreter py = interpreter.get();
if (py == null) {
py = new SharedInterpreter();
interpreter.set(py);
ConfigResolverHolder.getConfigResolver().getConfigProperty("sonata.python.searchPath", List.class).ifPresent(PythonWorkItemHandlerUtils::addToSearchPath);
}
return py;
}
Expand All @@ -42,8 +49,14 @@ protected static void closeInterpreter() {
}
}

private static void addToSearchPath(List<String> paths) {
Interpreter py = interpreter.get();
StringBuilder sb = new StringBuilder("import sys\n");
paths.forEach(path -> sb.append(String.format(PYTHON_SYS_PATH, path)));
py.exec(sb.toString());
}

protected static Object getValue(String key) {
return interpreter().getValue(key);
}

}

0 comments on commit 9c09b5a

Please sign in to comment.