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

64 lines
2.4 KiB
C#
Raw Normal View History

2022-10-23 21:47:09 +00:00
using System;
using System.Diagnostics;
using System.IO;
2023-03-26 19:04:06 +00:00
using System.Text.Json;
2021-02-08 06:37:37 +00:00
using System.Threading;
using System.Threading.Tasks;
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
{
2022-09-01 02:37:29 +00:00
internal class PythonPlugin : JsonRPCPlugin
{
private readonly ProcessStartInfo _startInfo;
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
2022-09-01 02:38:10 +00:00
var path = Path.Combine(Constant.ProgramDirectory, JsonRPC);
_startInfo.EnvironmentVariables["PYTHONPATH"] = path;
2021-11-30 08:36:38 +00:00
_startInfo.EnvironmentVariables["FLOW_VERSION"] = Constant.Version;
_startInfo.EnvironmentVariables["FLOW_PROGRAM_DIRECTORY"] = Constant.ProgramDirectory;
_startInfo.EnvironmentVariables["FLOW_APPLICATION_DIRECTORY"] = Constant.ApplicationDirectory;
//Add -B flag to tell python don't write .py[co] files. Because .pyc contains location infos which will prevent python portable
_startInfo.ArgumentList.Add("-B");
2014-07-18 12:00:55 +00:00
}
2021-08-13 05:55:07 +00:00
protected override Task<Stream> RequestAsync(JsonRPCRequestModel request, CancellationToken token = default)
2014-01-10 16:19:14 +00:00
{
2023-03-26 19:04:06 +00:00
_startInfo.ArgumentList[2] = JsonSerializer.Serialize(request);
2021-02-08 06:37:37 +00:00
return ExecuteAsync(_startInfo, token);
}
2021-08-13 05:55:07 +00:00
protected override string Request(JsonRPCRequestModel rpcRequest, CancellationToken token = default)
{
2022-10-23 21:47:09 +00:00
// since this is not static, request strings will build up in ArgumentList if index is not specified
2023-03-26 19:05:41 +00:00
_startInfo.ArgumentList[2] = JsonSerializer.Serialize(rpcRequest);
2022-09-01 02:34:47 +00:00
_startInfo.WorkingDirectory = Context.CurrentPluginMetadata.PluginDirectory;
2021-02-08 06:37:37 +00:00
// TODO: Async Action
return Execute(_startInfo);
}
public override async Task InitAsync(PluginInitContext context)
{
_startInfo.ArgumentList.Add(context.CurrentPluginMetadata.ExecuteFilePath);
_startInfo.ArgumentList.Add("");
await base.InitAsync(context);
2021-08-13 22:51:11 +00:00
_startInfo.WorkingDirectory = context.CurrentPluginMetadata.PluginDirectory;
}
}
2021-11-30 08:36:38 +00:00
}