diff --git a/Wox.Core/Plugin/PythonPlugin.cs b/Wox.Core/Plugin/PythonPlugin.cs index fac59fa298..a5971a7723 100644 --- a/Wox.Core/Plugin/PythonPlugin.cs +++ b/Wox.Core/Plugin/PythonPlugin.cs @@ -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); } } } \ No newline at end of file