PowerToys/Wox/PluginLoader/PythonPlugin.cs

65 lines
2.5 KiB
C#
Raw Normal View History

2014-07-07 23:05:06 +08:00
using System.Collections.Generic;
2014-07-18 20:00:55 +08:00
using System.Diagnostics;
2014-07-06 22:57:11 +08:00
using System.IO;
2014-07-18 20:00:55 +08:00
using Wox.Helper;
2014-07-09 23:44:57 +08:00
using Wox.JsonRPC;
2014-01-29 18:33:24 +08:00
using Wox.Plugin;
2014-01-29 18:33:24 +08:00
namespace Wox.PluginLoader
{
2014-07-09 18:15:23 +08:00
public class PythonPlugin : BasePlugin
{
2014-07-06 22:57:11 +08:00
private static string woxDirectory = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
2014-07-18 20:00:55 +08:00
private ProcessStartInfo startInfo;
2014-07-09 23:44:57 +08:00
public override string SupportedLanguage
{
2014-07-09 23:44:57 +08:00
get { return 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 = string.Format("{0};{1}",
Path.Combine(woxDirectory, "PythonHome\\DLLs"),
2014-07-18 23:12:50 +08:00
Path.Combine(woxDirectory, "PythonHome\\Lib\\site-packages"));
2014-07-18 20:00:55 +08:00
if (!startInfo.EnvironmentVariables.ContainsKey("PYTHONPATH"))
{
startInfo.EnvironmentVariables.Add("PYTHONPATH", additionalPythonPath);
}
else
{
startInfo.EnvironmentVariables["PYTHONPATH"] = additionalPythonPath;
}
}
2014-07-07 23:05:06 +08:00
protected override string ExecuteQuery(Query query)
2014-01-11 00:19:14 +08:00
{
2014-07-18 20:00:55 +08:00
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel()
{
Method = "query",
Parameters = new object[] { query.GetAllRemainingParameter() },
HttpProxy = HttpProxy.Instance
};
//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(woxDirectory, "PythonHome\\pythonw.exe");
startInfo.Arguments = string.Format("-B \"{0}\" \"{1}\"", context.CurrentPluginMetadata.ExecuteFilePath, request);
2014-07-18 20:00:55 +08:00
return Execute(startInfo);
}
2014-07-09 18:15:23 +08:00
protected override string ExecuteAction(JsonRPCRequestModel rpcRequest)
{
2014-07-18 20:00:55 +08:00
startInfo.FileName = Path.Combine(woxDirectory, "PythonHome\\pythonw.exe");
startInfo.Arguments = string.Format("-B \"{0}\" \"{1}\"", context.CurrentPluginMetadata.ExecuteFilePath, rpcRequest);
2014-07-18 20:00:55 +08:00
return Execute(startInfo);
}
}
2014-07-07 23:05:06 +08:00
}