PowerToys/Wox.Core/Plugin/PythonPlugin.cs

59 lines
2.2 KiB
C#
Raw Normal View History

2014-12-26 19:36:43 +08:00
using System.Diagnostics;
2014-07-06 22:57:11 +08:00
using System.IO;
using Wox.Core.UserSettings;
using Wox.Infrastructure;
2014-01-29 18:33:24 +08:00
using Wox.Plugin;
2014-12-26 19:36:43 +08:00
namespace Wox.Core.Plugin
{
2014-12-26 19:36:43 +08:00
internal class PythonPlugin : JsonRPCPlugin
{
private static readonly string PythonHome = Path.Combine(WoxDirectroy.Executable, "PythonHome");
private readonly ProcessStartInfo _startInfo;
public override string SupportedLanguage => AllowedLanguage.Python;
2014-07-18 20:00:55 +08:00
public PythonPlugin()
{
_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
{
_startInfo.EnvironmentVariables.Add("PYTHONPATH", additionalPythonPath);
2014-07-18 20:00:55 +08:00
}
else
{
_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
{
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
_startInfo.FileName = Path.Combine(PythonHome, "pythonw.exe");
_startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{request}\"";
2014-07-18 20:00:55 +08:00
return Execute(_startInfo);
}
2014-12-26 19:36:43 +08:00
protected override string ExecuteCallback(JsonRPCRequestModel rpcRequest)
{
_startInfo.FileName = Path.Combine(PythonHome, "pythonw.exe");
_startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{rpcRequest}\"";
return Execute(_startInfo);
}
}
2014-07-07 23:05:06 +08:00
}