File size: 5,987 Bytes
b1b3bae |
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Globalization;
namespace DWSIM.Libraries.PythonLink
{
public class Python
{
public Process PythonProcess { get; set; }
private string PythonEchoString { get; set; }
public Python(string PathToPythonBinaries)
{
StartPython(PathToPythonBinaries, false);
}
public Python(string PathToPythonBinaries, bool CreateWindow)
{
StartPython(PathToPythonBinaries, CreateWindow);
}
string ptob;
bool cw;
private void StartPython(string PathToPythonBinaries, bool CreateWindow)
{
ptob = PathToPythonBinaries;
cw = CreateWindow;
this.PythonEchoString = Guid.NewGuid().ToString();
PythonProcess = new Process();
ProcessStartInfo pi = new ProcessStartInfo();
if (System.Environment.OSVersion.Platform == PlatformID.Unix)
{
pi.FileName = "python";
}
else
{
if (PathToPythonBinaries[PathToPythonBinaries.Length - 1] != '\\')
PathToPythonBinaries = PathToPythonBinaries + Path.DirectorySeparatorChar;
pi.FileName = PathToPythonBinaries + "python.exe";
}
pi.RedirectStandardInput = true;
pi.RedirectStandardOutput = true;
pi.RedirectStandardError = true;
pi.UseShellExecute = false;
pi.CreateNoWindow = !CreateWindow;
pi.Verb = "open";
pi.Arguments = "-i";
//
pi.WorkingDirectory = ".";
PythonProcess.StartInfo = pi;
PythonProcess.Start();
PythonProcess.ErrorDataReceived += new DataReceivedEventHandler(PythonProcess_ErrorDataReceived);
PythonProcess.OutputDataReceived += new DataReceivedEventHandler(PythonProcess_OutputDataReceived);
PythonProcess.BeginOutputReadLine();
PythonProcess.BeginErrorReadLine();
PythonEntryText = ExecuteCommand("echo = '" + PythonEchoString + "'", true);
}
public double GetScalar(string command)
{
string rasp = ExecuteCommand(command, false);
if (rasp == "None")
{
return 0.0d;
}
else
{
return double.Parse(rasp, CultureInfo.InvariantCulture);
}
}
public double[] GetVector(string command)
{
string rasp = ExecuteCommand(command, false);
return rasp.TrimStart(new char[] { '(' }).TrimEnd(new char[] { ')' }).Trim().Split(new char[] { ',' }).Select((item) => Double.Parse(item, CultureInfo.InvariantCulture)).ToArray();
}
StringBuilder SharedBuilder = new StringBuilder();
ManualResetEvent PythonDoneEvent = new ManualResetEvent(false);
public string PythonEntryText { get; internal set; }
public void WorkThread(object o)
{
var command = ((object[])o)[0];
SharedBuilder.Clear();
PythonDoneEvent.Reset();
if (command != null)
{
if (command is string)
{
PythonProcess.StandardInput.WriteLine((string)command);
}
else
{
foreach (string s in (string[])((object[])o)[0])
{
PythonProcess.StandardInput.WriteLine(s);
}
}
}
if ((bool)((object[])o)[1])
PythonProcess.StandardInput.WriteLine("echo");
else
PythonProcess.StandardInput.WriteLine("sys.stdout.flush()");
PythonDoneEvent.WaitOne();
}
public string ExecuteCommand(string command, bool writeecho)
{
Thread tmp = new Thread(new ParameterizedThreadStart(WorkThread));
tmp.Start(new object[] { command, writeecho });
if (!tmp.Join((int)(GlobalSettings.Settings.PythonTimeoutInMinutes * 60 * 1000)))
{
tmp.Abort();
throw new Exception("Python timeout");
}
return SharedBuilder.ToString();
}
public string ExecuteMultilineCommand(string[] command, bool writeecho)
{
Thread tmp = new Thread(new ParameterizedThreadStart(WorkThread));
tmp.Start(new object[] { command, writeecho });
if (!tmp.Join((int)(GlobalSettings.Settings.PythonTimeoutInMinutes * 60 * 1000)))
{
tmp.Abort();
throw new Exception("Python timeout");
}
return SharedBuilder.ToString();
}
void PythonProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data == null)
{
SharedBuilder.Clear();
PythonDoneEvent.Set();
return;
}
if (e.Data.Trim() == "'" + PythonEchoString + "'")
PythonDoneEvent.Set();
else
{
SharedBuilder.Append(e.Data);
PythonDoneEvent.Set();
}
}
void PythonProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
//Console.WriteLine(e.Data);
//throw new Exception(e.Data);
}
}
public delegate void PythonRestartedEventHandler(object sender, EventArgs e);
}
}
|