protected async override Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken) { IEngine pythonEngine = PythonScope.GetPythonEngine(context); string scriptFile = ScriptFile.Get(context); string scriptCode = Code.Get(context); // safeguard checks if (scriptFile.IsNullOrEmpty() && scriptCode.IsNullOrEmpty()) { throw new InvalidOperationException(Resources.NoScriptSpecifiedException); } if (!scriptFile.IsNullOrEmpty() && !File.Exists(scriptFile)) { throw new FileNotFoundException(Resources.ScriptFileNotFoundException, scriptFile); } // load script from file if not specified if (scriptCode.IsNullOrEmpty()) { scriptCode = File.ReadAllText(ScriptFile.Get(context)); } PythonObject result = null; try { result = await pythonEngine.LoadScript(scriptCode, cancellationToken); } catch (Exception e) { Trace.TraceError($"Error loading Python script: {e.ToString()}"); throw new InvalidOperationException(Resources.LoadScriptException, e); } return((asyncCodeActivityContext) => { Result.Set(asyncCodeActivityContext, result); }); }
protected async override Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken) { IEngine pythonEngine = PythonScope.GetPythonEngine(context); string scriptFile = ScriptFile.Get(context); string scriptCode = Code.Get(context); //IEnumerable<object> parameters = Parameters.Get(context); // safeguard checks if (scriptFile.IsNullOrEmpty() && scriptCode.IsNullOrEmpty()) { throw new InvalidOperationException(Resources.NoScriptSpecifiedException); } if (!scriptFile.IsNullOrEmpty() && !File.Exists(scriptFile)) { throw new FileNotFoundException(Resources.ScriptFileNotFoundException, scriptFile); } // load script from file if not specified if (scriptCode.IsNullOrEmpty()) { scriptCode = File.ReadAllText(ScriptFile.Get(context)); } try { await pythonEngine.Execute(scriptCode, cancellationToken); } catch (Exception e) { Trace.TraceError($"Error running Python script: {e}"); throw new InvalidOperationException(Resources.RunScriptException, e); } return((asyncCodeActivityContext) => { }); }
protected override void Execute(CodeActivityContext context) { IEngine pythonEngine = PythonScope.GetPythonEngine(context); PythonObject pyObject = PythonObject.Get(context); if (null == pyObject) { throw new ArgumentNullException(nameof(PythonObject)); } T result; try { result = (T)pythonEngine.Convert(pyObject, typeof(T)); } catch (Exception e) { Trace.TraceError($"Error casting Python object: {e}"); throw new InvalidOperationException(Resources.ConvertException, e); } Result.Set(context, result); }
protected async override Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken) { IEngine pythonEngine = PythonScope.GetPythonEngine(context); if (pythonEngine == null) { throw new InvalidOperationException(Resources.PythonEngineNotFoundException); } PythonObject pyObject = Instance.Get(context); string methodName = Name.Get(context); IEnumerable <object> parameters = Parameters.Get(context); // safeguard checks if (methodName.IsNullOrEmpty()) { throw new InvalidOperationException(Resources.InvalidMethodNameException); } PythonObject result = null; try { result = await pythonEngine.InvokeMethod(pyObject, methodName, parameters, cancellationToken); } catch (Exception e) { Trace.TraceError($"Error invoking Python function: {e.ToString()}"); throw new InvalidOperationException(Resources.InvokeException, e); } return(asyncCodeActivityContext => { Result.Set(asyncCodeActivityContext, result); }); }