PowerToys/Wox.Core/Plugin/PythonPlugin.cs

64 lines
2.4 KiB
C#
Raw Normal View History

using System;
using System.Diagnostics;
using System.IO;
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 readonly ProcessStartInfo _startInfo;
public override string SupportedLanguage { get; set; } = AllowedLanguage.Python;
public PythonPlugin(string filename)
2014-07-18 20:00:55 +08:00
{
_startInfo = new ProcessStartInfo
{
2016-05-05 19:30:23 +08:00
FileName = filename,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
};
// temp fix for issue #667
var path = Path.Combine(Constant.ProgramDirectory, JsonRPC);
_startInfo.EnvironmentVariables["PYTHONPATH"] = path;
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.Search },
};
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.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{request}\"";
// todo happlebao why context can't be used in constructor
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;
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.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{rpcRequest}\"";
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;
return Execute(_startInfo);
}
2017-04-11 21:34:04 +08:00
protected override string ExecuteContextMenu(Result selectedResult) {
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel {
Method = "context_menu",
Parameters = new object[] { selectedResult.ContextData },
};
_startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{request}\"";
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;
return Execute(_startInfo);
}
}
2014-07-07 23:05:06 +08:00
}