-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPythonScriptCaller.cs
63 lines (57 loc) · 2.18 KB
/
PythonScriptCaller.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using static System.String;
public class PythonScriptCaller {
public PythonScriptCaller(string python_executable, string file) {
PythonExecutable = python_executable;
FileName = file;
}
public string PythonExecutable { get; }
public string FileName { get; }
public Action<string> Outputs = (_) => { };
public Action<string> Errors = (_) => { };
public void Call(IEnumerable<string> args) {
var start = new ProcessStartInfo {
FileName = PythonExecutable,
Arguments = $"{FileName} {Join(" ", args.Select(x => $"\"{x}\""))}",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
var process = Process.Start(start);
process.WaitForExit();
var errors = process.StandardError.ReadToEnd();
if (!String.IsNullOrEmpty(errors)) Errors.Invoke(errors);
var outputs = process.StandardOutput.ReadToEnd();
if (!String.IsNullOrEmpty(outputs)) Outputs.Invoke(outputs);
//using (var process = Process.Start(start)) {
// process.ErrorDataReceived += (s, e) => {
// OutputEvent.Invoke($"Error: {e.Data}");
// };
// process.OutputDataReceived += (s, e) => {
// OutputEvent.Invoke($"Output: {e.Data}");
// };
//using (var reader = process.StandardError) {
// var result = reader.ReadToEnd();
// if (result.Length > 0) OutputEvent.Invoke(reader.ReadLine());
//}
//using (var reader = process.StandardOutput) {
// //string result = reader.ReadToEnd();
// //OutputEvent.Invoke(result);
// while (!reader.EndOfStream) {
// OutputEvent.Invoke(reader.ReadLine());
// }
//}
//using (var errors = process.StandardError) {
// var error = errors.ReadToEnd();
// Log.Error($"Error: {error}");
//}
//}
}
public void Call (params string[] args) {
Call(args.ToList());
}
}