Example #1
0
        public void Execute(string code)
        {
            using (var cts = new CancellationTokenSource((int)Timeout * 1000))
            {
                try
                {
                    var request = new PythonRequest()
                    {
                        RequestType = RequestType.Execute,
                        Code        = code
                    };

                    PythonResponse response = RequestAsync(request, cts.Token);
                    cts.Token.ThrowIfCancellationRequested();
                    response.ThrowExceptionIfNeeded();
                }
                catch (Exception e)
                {
                    if (cts.IsCancellationRequested)
                    {
                        throw new TimeoutException(UiPath_Python.TimeoutException);
                    }
                    Trace.TraceError($"Python exception: {e}");
                    throw;
                }
            }
        }
        public List <Prediction> ParseResponse(PythonResponse pythonResponse)
        {
            var rawStrings = pythonResponse.Results.Split("\r\n\r\n");

            rawStrings = rawStrings.Take(rawStrings.Length - 1).ToArray();
            return(rawStrings.Select(x => new Prediction(x)).ToList());
        }
Example #3
0
        /// <summary>
        /// Writes the serialized Python request to the named pipe. Waits from the request to be read on Python side.
        /// Then reads the response from python(json) an deserializes it to PythonRespnse
        /// </summary>
        /// <param name="request"></param>
        /// <param name="ct"></param>
        /// <returns></returns>
        public PythonResponse RequestAsync(PythonRequest request, CancellationToken ct)
        {
            using (CancellationTokenRegistration ctr = ct.Register(() => OnCancellationRequested()))
            {
                using (var streamWriter = new StreamWriter(ClientStream, _utf8Encoding, _defaultBufferSize,
                                                           leaveOpen: true)
                {
                    AutoFlush = true
                })
                {
                    Trace.TraceInformation("Sending information to Python.");
                    streamWriter.WriteLine(request.Serialize());
                }

                ct.ThrowIfCancellationRequested();
                bool isWindows = true;
#if NETCOREAPP
                if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    isWindows = false;
                }
#endif
                if (isWindows)
                {
                    ClientStream.WaitForPipeDrain();
                }

                using (var streamReader = new StreamReader(ClientStream, _utf8Encoding, false, _defaultBufferSize,
                                                           leaveOpen: true))
                {
                    return(PythonResponse.Deserialize(streamReader.ReadLine()));
                }
            }
        }
Example #4
0
        public Argument Convert(Guid obj, string t)
        {
            using (var cts = new CancellationTokenSource((int)Timeout * 1000))
            {
                try
                {
                    var request = new PythonRequest()
                    {
                        RequestType = RequestType.Convert,
                        Instance    = obj,
                        Type        = t
                    };

                    PythonResponse response = RequestAsync(request, cts.Token);
                    cts.Token.ThrowIfCancellationRequested();
                    response.ThrowExceptionIfNeeded();
                    return(response.Argument);
                }
                catch (Exception e)
                {
                    if (cts.IsCancellationRequested)
                    {
                        throw new TimeoutException(UiPath_Python.TimeoutException);
                    }
                    Trace.TraceError($"Python exception: {e}");
                    throw;
                }
            }
        }
Example #5
0
        public Guid LoadScript(string code)
        {
            using (var cts = new CancellationTokenSource((int)Timeout * 1000))
            {
                try
                {
                    var request = new PythonRequest()
                    {
                        RequestType = RequestType.LoadScript,
                        Code        = code
                    };

                    PythonResponse response = RequestAsync(request, cts.Token);

                    cts.Token.ThrowIfCancellationRequested();

                    Trace.TraceInformation("LoadScript Guid:: " + response.Guid);

                    return(response.Guid);
                }
                catch (Exception e)
                {
                    if (cts.IsCancellationRequested)
                    {
                        throw new TimeoutException(UiPath_Python.TimeoutException);
                    }
                    Trace.TraceError($"Python exception: {e}");
                    throw;
                }
            }
        }
Example #6
0
        public Guid InvokeMethod(Guid instance, string method, IEnumerable <Argument> args)
        {
            using (var cts = new CancellationTokenSource((int)Timeout * 1000))
            {
                try
                {
                    var request = new PythonRequest()
                    {
                        RequestType = RequestType.InvokeMethod,
                        Instance    = instance,
                        Method      = method,
                        Arguments   = args
                    };

                    PythonResponse response = RequestAsync(request, cts.Token);
                    cts.Token.ThrowIfCancellationRequested();
                    response.ThrowExceptionIfNeeded();
                    return(response.Guid);
                }
                catch (Exception e)
                {
                    if (cts.IsCancellationRequested)
                    {
                        throw new TimeoutException(UiPath_Python.TimeoutException);
                    }
                    Trace.TraceError($"Python exception: {e}");
                    throw;
                }
            }
        }
Example #7
0
        public void Initialize(string path, string libraryPath, Version version, string workingFolder)
        {
            using (var cts = new CancellationTokenSource((int)Timeout * 1000))
            {
                try
                {
                    var request = new PythonRequest()
                    {
                        RequestType   = RequestType.Initialize,
                        ScriptPath    = path,
                        LibraryPath   = libraryPath,
                        PythonVersion = version.ToString(),
                        WorkingFolder = workingFolder
                    };

                    PythonResponse response = RequestAsync(request, Token);
                    Token.ThrowIfCancellationRequested();
                    response.ThrowExceptionIfNeeded();
                }
                catch (Exception e)
                {
                    if (cts.IsCancellationRequested)
                    {
                        throw new TimeoutException(UiPath_Python.TimeoutException);
                    }
                    Trace.TraceError($"Python exception: {e}");
                    throw;
                }
            }
        }
Example #8
0
        public void Shutdown()
        {
            using (var cts = new CancellationTokenSource((int)Timeout * 1000))
            {
                var request = new PythonRequest()
                {
                    RequestType = RequestType.Shutdown
                };

                PythonResponse response = RequestAsync(request, cts.Token);

                cts.Token.ThrowIfCancellationRequested();
            }
        }
        internal async void RunServer()
        {
            PythonResponse response = new PythonResponse
            {
                ResultState = ResultState.Successful
            };

            pipeName   = Process.GetCurrentProcess().Id.ToString();
            pipeServer = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte,
                                                   PipeOptions.Asynchronous);

            pipeServer.WaitForConnection();

            using (var streamReader = new StreamReader(pipeServer, _utf8Encoding, false, _defaultBufferSize,
                                                       leaveOpen: true))
                using (var streamWriter = new StreamWriter(pipeServer, _utf8Encoding, _defaultBufferSize,
                                                           leaveOpen: true)
                {
                    AutoFlush = true
                })

                    while (true)
                    {
                        try
                        {
                            var request = PythonRequest.Deserialize(await streamReader.ReadLineAsync());

                            switch (request.RequestType)
                            {
                            case RequestType.Initialize:
                                Initialize(request.ScriptPath, request.LibraryPath, (Version)Enum.Parse(typeof(Version), request.PythonVersion), request.WorkingFolder);
                                response.ResultState = ResultState.Successful;
                                streamWriter.WriteLine(response.Serialize());

                                break;

                            case RequestType.Shutdown:
                                response = new PythonResponse
                                {
                                    ResultState = ResultState.Successful
                                };

                                streamWriter.WriteLine(response.Serialize());
                                Shutdown();
                                break;

                            case RequestType.Execute:
                                Execute(request.Code);
                                response = new PythonResponse
                                {
                                    ResultState = ResultState.Successful
                                };
                                streamWriter.WriteLine(response.Serialize());
                                break;

                            case RequestType.LoadScript:
                                var guid = LoadScript(request.Code);
                                response = new PythonResponse
                                {
                                    ResultState = ResultState.Successful,
                                    Guid        = guid
                                };

                                streamWriter.WriteLine(response.Serialize());
                                break;

                            case RequestType.InvokeMethod:
                                var guidI = InvokeMethod(request.Instance, request.Method, request.Arguments);
                                response = new PythonResponse
                                {
                                    ResultState = ResultState.Successful
                                };
                                response.Guid = guidI;
                                streamWriter.WriteLine(response.Serialize());
                                break;

                            case RequestType.Convert:
                                Argument arg = Convert(request.Instance, request.Type);
                                response = new PythonResponse
                                {
                                    Argument    = arg,
                                    ResultState = ResultState.Successful
                                };

                                streamWriter.WriteLine(response.Serialize());
                                break;

                            default:
                                Shutdown();
                                break;
                            }

                            WaitForPipeDrain(pipeServer);
                        }
                        catch (Exception ex)
                        {
                            response = new PythonResponse
                            {
                                ResultState = ResultState.InstantiationException,
                            };
                            response.Errors = new List <string>();
                            response.Errors.Add(ex.Message);

                            streamWriter.WriteLine(response.Serialize());
                            WaitForPipeDrain(pipeServer);
                            throw;
                        }
                    }
        }