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