2014-12-26 19:36:43 +08:00
|
|
|
|
using System.Diagnostics;
|
2014-07-06 22:57:11 +08:00
|
|
|
|
using System.IO;
|
2015-01-05 22:41:17 +08:00
|
|
|
|
using Wox.Core.UserSettings;
|
2016-01-08 09:55:24 +08:00
|
|
|
|
using Wox.Infrastructure;
|
2014-01-29 18:33:24 +08:00
|
|
|
|
using Wox.Plugin;
|
2013-12-21 01:20:17 +08:00
|
|
|
|
|
2014-12-26 19:36:43 +08:00
|
|
|
|
namespace Wox.Core.Plugin
|
2013-12-21 01:20:17 +08:00
|
|
|
|
{
|
2014-12-26 19:36:43 +08:00
|
|
|
|
internal class PythonPlugin : JsonRPCPlugin
|
2013-12-21 01:20:17 +08:00
|
|
|
|
{
|
2016-04-27 09:15:53 +08:00
|
|
|
|
private static readonly string PythonHome = Path.Combine(Infrastructure.Wox.ProgramPath, "PythonHome");
|
2016-01-08 09:55:24 +08:00
|
|
|
|
private readonly ProcessStartInfo _startInfo;
|
2013-12-21 01:20:17 +08:00
|
|
|
|
|
2016-01-08 09:55:24 +08:00
|
|
|
|
public override string SupportedLanguage => AllowedLanguage.Python;
|
2013-12-21 01:20:17 +08:00
|
|
|
|
|
2014-07-18 20:00:55 +08:00
|
|
|
|
public PythonPlugin()
|
|
|
|
|
{
|
2016-01-08 09:55:24 +08:00
|
|
|
|
_startInfo = new ProcessStartInfo
|
|
|
|
|
{
|
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
RedirectStandardOutput = true,
|
|
|
|
|
RedirectStandardError = true
|
|
|
|
|
};
|
|
|
|
|
string additionalPythonPath = $"{Path.Combine(PythonHome, "DLLs")};{Path.Combine(PythonHome, "Lib", "site-packages")}";
|
|
|
|
|
if (!_startInfo.EnvironmentVariables.ContainsKey("PYTHONPATH"))
|
2014-07-18 20:00:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
2016-01-08 09:55:24 +08:00
|
|
|
|
_startInfo.EnvironmentVariables.Add("PYTHONPATH", additionalPythonPath);
|
2014-07-18 20:00:55 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-01-08 09:55:24 +08:00
|
|
|
|
_startInfo.EnvironmentVariables["PYTHONPATH"] = additionalPythonPath;
|
2014-07-18 20:00:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-07 23:05:06 +08:00
|
|
|
|
protected override string ExecuteQuery(Query query)
|
2014-01-11 00:19:14 +08:00
|
|
|
|
{
|
2016-01-07 05:34:42 +08:00
|
|
|
|
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel
|
|
|
|
|
{
|
2016-01-08 09:55:24 +08:00
|
|
|
|
Method = "query",
|
|
|
|
|
Parameters = new object[] { query.GetAllRemainingParameter() },
|
|
|
|
|
HttpProxy = HttpProxy.Instance
|
|
|
|
|
};
|
2014-07-18 20:00:55 +08:00
|
|
|
|
//Add -B flag to tell python don't write .py[co] files. Because .pyc contains location infos which will prevent python portable
|
2016-01-08 09:55:24 +08:00
|
|
|
|
_startInfo.FileName = Path.Combine(PythonHome, "pythonw.exe");
|
|
|
|
|
_startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{request}\"";
|
2014-07-18 20:00:55 +08:00
|
|
|
|
|
2016-01-08 09:55:24 +08:00
|
|
|
|
return Execute(_startInfo);
|
2014-02-09 20:55:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-26 19:36:43 +08:00
|
|
|
|
protected override string ExecuteCallback(JsonRPCRequestModel rpcRequest)
|
2014-02-09 20:55:18 +08:00
|
|
|
|
{
|
2016-01-08 09:55:24 +08:00
|
|
|
|
_startInfo.FileName = Path.Combine(PythonHome, "pythonw.exe");
|
|
|
|
|
_startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{rpcRequest}\"";
|
|
|
|
|
return Execute(_startInfo);
|
2013-12-21 01:20:17 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-07 23:05:06 +08:00
|
|
|
|
}
|