Flow.Launcher/Flow.Launcher.Core/Plugin/PythonPlugin.cs

64 lines
2.5 KiB
C#
Raw Normal View History

using System;
using System.Diagnostics;
using System.IO;
2020-04-21 09:12:17 +00:00
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Plugin;
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher.Core.Plugin
{
2014-12-26 11:36:43 +00:00
internal class PythonPlugin : JsonRPCPlugin
{
private readonly ProcessStartInfo _startInfo;
public override string SupportedLanguage { get; set; } = AllowedLanguage.Python;
public PythonPlugin(string filename)
2014-07-18 12:00:55 +00:00
{
_startInfo = new ProcessStartInfo
{
2016-05-05 11:30:23 +00: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 12:00:55 +00:00
}
2014-07-07 15:05:06 +00:00
protected override string ExecuteQuery(Query query)
2014-01-10 16:19:14 +00:00
{
2016-01-06 21:34:42 +00:00
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel
{
Method = "query",
Parameters = new object[] { query.Search },
};
2014-07-18 12:00:55 +00: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 12:00:55 +00:00
return Execute(_startInfo);
}
2014-12-26 11:36:43 +00: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 13:34:04 +00: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 15:05:06 +00:00
}