< Summary

Information
Class: Elsa.Expressions.Python.Handlers.GenerateWorkflowVariableAccessors
Assembly: Elsa.Expressions.Python
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.Python/Handlers/GenerateWorkflowVariableAccessors.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 27
Coverable lines: 27
Total lines: 53
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
HandleAsync(...)0%620%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.Python/Handlers/GenerateWorkflowVariableAccessors.cs

#LineLine coverage
 1using System.Text;
 2using Elsa.Expressions.Models;
 3using Elsa.Extensions;
 4using Elsa.Mediator.Contracts;
 5using Elsa.Expressions.Python.Notifications;
 6using JetBrains.Annotations;
 7
 8namespace Elsa.Expressions.Python.Handlers;
 9
 10/// <summary>
 11/// Configures the C# evaluator with methods to access workflow variables.
 12/// </summary>
 13[UsedImplicitly]
 14public class GenerateWorkflowVariableAccessors : INotificationHandler<EvaluatingPython>
 15{
 16    /// <inheritdoc />
 17    public Task HandleAsync(EvaluatingPython notification, CancellationToken cancellationToken)
 18    {
 019        var expressionExecutionContext = notification.Context;
 020        var variables = expressionExecutionContext.GetVariablesInScope().ToList();
 021        var sb = new StringBuilder();
 022        sb.AppendLine("class WorkflowVariablesProxy:");
 023        sb.AppendLine("    def __init__(self, execution_context):");
 024        sb.AppendLine("        self.execution_context = execution_context");
 025        sb.AppendLine();
 026        sb.AppendLine("    def get(self, name):");
 027        sb.AppendLine("        return self.execution_context.GetVariable(name)");
 028        sb.AppendLine();
 029        sb.AppendLine("    def set(self, name, value):");
 030        sb.AppendLine("        self.execution_context.SetVariable(name, value)");
 031        sb.AppendLine();
 32
 033        foreach (var variable in variables)
 34        {
 035            var variableName = variable.Name;
 036            var variableType = variable.GetVariableType();
 037            var friendlyTypeName = variableType.GetFriendlyTypeName(Brackets.Square);
 038            sb.AppendLine($"    @property");
 039            sb.AppendLine($"    def {variableName}(self):");
 040            sb.AppendLine($"        return self.execution_context.GetVariable[{friendlyTypeName}]('{variableName}')");
 041            sb.AppendLine($"    @{variableName}.setter");
 042            sb.AppendLine($"    def {variableName}(self, value):");
 043            sb.AppendLine($"        self.execution_context.SetVariable('{variableName}', value)");
 44        }
 45
 046        sb.AppendLine();
 047        sb.AppendLine("variables = WorkflowVariablesProxy(execution_context);");
 48
 049        notification.AppendScript(sb.ToString());
 50
 051        return Task.CompletedTask;
 52    }
 53}