| | | 1 | | using System.Text; |
| | | 2 | | using Elsa.Expressions.Helpers; |
| | | 3 | | using Elsa.Expressions.Models; |
| | | 4 | | using Elsa.Mediator.Contracts; |
| | | 5 | | using Elsa.Expressions.Python.Contracts; |
| | | 6 | | using Elsa.Expressions.Python.Models; |
| | | 7 | | using Elsa.Expressions.Python.Notifications; |
| | | 8 | | using Python.Runtime; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Expressions.Python.Services; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Evaluates Python expressions using IronPython. |
| | | 14 | | /// </summary> |
| | | 15 | | public class PythonNetPythonEvaluator : IPythonEvaluator |
| | | 16 | | { |
| | | 17 | | private const string ReturnVarName = "elsa_python_result_variable_name"; |
| | | 18 | | private readonly INotificationSender _notificationSender; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="PythonNetPythonEvaluator"/> class. |
| | | 22 | | /// </summary> |
| | 0 | 23 | | public PythonNetPythonEvaluator(INotificationSender notificationSender) |
| | | 24 | | { |
| | 0 | 25 | | _notificationSender = notificationSender; |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | public async Task<object?> EvaluateAsync(string expression, Type returnType, ExpressionExecutionContext context, Can |
| | | 30 | | { |
| | 0 | 31 | | using var gil = Py.GIL(); |
| | 0 | 32 | | using var scope = Py.CreateScope(); |
| | 0 | 33 | | var notification = new EvaluatingPython(scope, context); |
| | | 34 | | |
| | 0 | 35 | | scope.Import("System"); |
| | | 36 | | |
| | | 37 | | // Add globals. |
| | 0 | 38 | | scope.Set("execution_context", new ExecutionContextProxy(context)); |
| | 0 | 39 | | scope.Set("input", new InputProxy(context)); |
| | 0 | 40 | | scope.Set("output", new OutputProxy(context)); |
| | 0 | 41 | | scope.Set("outcome", new OutcomeProxy(context)); |
| | | 42 | | |
| | 0 | 43 | | await _notificationSender.SendAsync(notification, cancellationToken); |
| | 0 | 44 | | var wrappedScript = WrapInExecuteScriptFunction(expression); |
| | 0 | 45 | | scope.Exec(wrappedScript); |
| | 0 | 46 | | var result = scope.Get<object>(ReturnVarName); |
| | 0 | 47 | | return result.ConvertTo(returnType); |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Wraps the user script in a function called execute_script() and returns the result of that function. |
| | | 52 | | /// </summary> |
| | | 53 | | private static string WrapInExecuteScriptFunction(string userScript, int indentationLevel = 1) |
| | | 54 | | { |
| | 0 | 55 | | var lines = userScript.Split('\n'); |
| | 0 | 56 | | var wrappedScript = new StringBuilder(); |
| | 0 | 57 | | var indentation = new string(' ', 4 * indentationLevel); |
| | 0 | 58 | | wrappedScript.AppendLine("def execute_script():"); |
| | | 59 | | |
| | 0 | 60 | | foreach (var line in lines.Take(lines.Length - 1)) |
| | 0 | 61 | | wrappedScript.AppendLine(indentation + line); |
| | | 62 | | |
| | 0 | 63 | | var lastLine = lines.LastOrDefault() ?? ""; |
| | | 64 | | |
| | 0 | 65 | | if (!lastLine.StartsWith("return")) |
| | 0 | 66 | | lastLine = $"return {lastLine}"; |
| | | 67 | | |
| | 0 | 68 | | wrappedScript.AppendLine(indentation + lastLine); |
| | | 69 | | |
| | 0 | 70 | | wrappedScript.AppendLine($"{ReturnVarName} = execute_script()"); |
| | 0 | 71 | | return wrappedScript.ToString(); |
| | | 72 | | } |
| | | 73 | | } |