Refactoring directory path for python plugin

This commit is contained in:
bao-qian 2016-01-08 01:55:24 +00:00
parent 8ee94d75ca
commit e9ddfa6cf9

View File

@ -1,41 +1,36 @@
using System.Diagnostics;
using System.IO;
using System.Reflection;
using Wox.Core.UserSettings;
using Wox.Infrastructure;
using Wox.Plugin;
namespace Wox.Core.Plugin
{
internal class PythonPlugin : JsonRPCPlugin
{
private static string woxDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
private ProcessStartInfo startInfo;
private static readonly string PythonHome = Path.Combine(WoxDirectroy.Executable, "PythonHome");
private readonly ProcessStartInfo _startInfo;
public override string SupportedLanguage
{
get { return AllowedLanguage.Python; }
}
public override string SupportedLanguage => AllowedLanguage.Python;
public PythonPlugin()
{
startInfo = new ProcessStartInfo
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
string additionalPythonPath = string.Format("{0};{1}",
Path.Combine(woxDirectory, "PythonHome\\DLLs"),
Path.Combine(woxDirectory, "PythonHome\\Lib\\site-packages"));
if (!startInfo.EnvironmentVariables.ContainsKey("PYTHONPATH"))
_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"))
{
startInfo.EnvironmentVariables.Add("PYTHONPATH", additionalPythonPath);
_startInfo.EnvironmentVariables.Add("PYTHONPATH", additionalPythonPath);
}
else
{
startInfo.EnvironmentVariables["PYTHONPATH"] = additionalPythonPath;
_startInfo.EnvironmentVariables["PYTHONPATH"] = additionalPythonPath;
}
}
@ -43,22 +38,22 @@ namespace Wox.Core.Plugin
{
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel
{
Method = "query",
Parameters = new object[] { query.GetAllRemainingParameter() },
HttpProxy = HttpProxy.Instance
};
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);
_startInfo.FileName = Path.Combine(PythonHome, "pythonw.exe");
_startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{request}\"";
return Execute(startInfo);
return Execute(_startInfo);
}
protected override string ExecuteCallback(JsonRPCRequestModel rpcRequest)
{
startInfo.FileName = Path.Combine(woxDirectory, "PythonHome\\pythonw.exe");
startInfo.Arguments = string.Format("-B \"{0}\" \"{1}\"", context.CurrentPluginMetadata.ExecuteFilePath, rpcRequest);
return Execute(startInfo);
_startInfo.FileName = Path.Combine(PythonHome, "pythonw.exe");
_startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{rpcRequest}\"";
return Execute(_startInfo);
}
}
}