Use existing installed python

1. use installed python
2. add settings to choose python directory
3. add py3 compability
4. create hello world python example
This commit is contained in:
bao-qian
2016-05-05 01:57:03 +01:00
parent bc0f5a9136
commit 785843198a
27 changed files with 362 additions and 177 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Wox.Infrastructure.Exception;
@@ -8,42 +9,105 @@ using Wox.Plugin;
namespace Wox.Core.Plugin
{
internal class CSharpPluginLoader : IPluginLoader
public static class PluginsLoader
{
public IEnumerable<PluginPair> LoadPlugin(List<PluginMetadata> pluginMetadatas)
public const string PATH = "PATH";
public const string Python = "python";
public const string PythonExecutable = "pythonw.exe";
public static IEnumerable<PluginPair> CSharpPlugins(IEnumerable<PluginMetadata> source)
{
var plugins = new List<PluginPair>();
List<PluginMetadata> CSharpPluginMetadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp.ToUpper()).ToList();
var metadatas = source.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp);
foreach (PluginMetadata metadata in CSharpPluginMetadatas)
foreach (var metadata in metadatas)
{
Assembly assembly;
try
{
Assembly asm = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IPlugin))).ToList();
if (types.Count == 0)
{
Log.Warn($"Couldn't load plugin {metadata.Name}: didn't find the class that implement IPlugin");
continue;
}
foreach (Type type in types)
{
PluginPair pair = new PluginPair
{
Plugin = Activator.CreateInstance(type) as IPlugin,
Metadata = metadata
};
plugins.Add(pair);
}
assembly = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
}
catch (Exception e)
{
Log.Error(new WoxPluginException(metadata.Name, $"Couldn't load plugin", e));
Log.Error(new WoxPluginException(metadata.Name, "Couldn't load assembly", e));
continue;
}
var types = assembly.GetTypes();
Type type;
try
{
type = types.First(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IPlugin)));
}
catch (InvalidOperationException e)
{
Log.Error(new WoxPluginException(metadata.Name, "Can't find class implement IPlugin", e));
continue;
}
IPlugin plugin;
try
{
plugin = (IPlugin)Activator.CreateInstance(type);
}
catch (Exception e)
{
Log.Error(new WoxPluginException(metadata.Name, "Can't create instance", e));
continue;
}
PluginPair pair = new PluginPair
{
Plugin = plugin,
Metadata = metadata
};
plugins.Add(pair);
}
return plugins;
}
public static IEnumerable<PluginPair> PythonPlugins(IEnumerable<PluginMetadata> source, string pythonDirecotry)
{
var metadatas = source.Where(o => o.Language.ToUpper() == AllowedLanguage.Python);
string filename;
if (string.IsNullOrEmpty(pythonDirecotry))
{
var paths = Environment.GetEnvironmentVariable(PATH);
if (paths != null)
{
var pythonPaths = paths.Split(';').Where(p => p.ToLower().Contains(Python));
if (pythonPaths.Any())
{
filename = PythonExecutable;
}
else
{
Log.Error(new WoxException("Python can't be found in PATH."));
return new List<PluginPair>();
}
}
else
{
Log.Error(new WoxException("Path variable is not set."));
return new List<PluginPair>();
}
}
else
{
var path = Path.Combine(pythonDirecotry, PythonExecutable);
if (File.Exists(path))
{
filename = path;
}
else
{
Log.Error(new WoxException("Can't find python executable in python directory"));
return new List<PluginPair>();
}
}
var plugins = metadatas.Select(metadata => new PluginPair
{
Plugin = new PythonPlugin(filename),
Metadata = metadata
});
return plugins;
}
}

View File

@@ -1,10 +0,0 @@
using System.Collections.Generic;
using Wox.Plugin;
namespace Wox.Core.Plugin
{
internal interface IPluginLoader
{
IEnumerable<PluginPair> LoadPlugin(List<PluginMetadata> pluginMetadatas);
}
}

View File

@@ -14,6 +14,7 @@ namespace Wox.Core.Plugin
{
/// <summary>
/// Represent the plugin that using JsonPRC
/// every JsonRPC plugin should has its own plugin instance
/// </summary>
internal abstract class JsonRPCPlugin : IPlugin
{
@@ -22,7 +23,7 @@ namespace Wox.Core.Plugin
/// <summary>
/// The language this JsonRPCPlugin support
/// </summary>
public abstract string SupportedLanguage { get; }
public abstract string SupportedLanguage { get; set; }
protected abstract string ExecuteQuery(Query query);
protected abstract string ExecuteCallback(JsonRPCRequestModel rpcRequest);
@@ -30,7 +31,7 @@ namespace Wox.Core.Plugin
public List<Result> Query(Query query)
{
string output = ExecuteQuery(query);
if (!string.IsNullOrEmpty(output))
if (!String.IsNullOrEmpty(output))
{
try
{
@@ -46,7 +47,7 @@ namespace Wox.Core.Plugin
{
if (result1.JsonRPCAction == null) return false;
if (!string.IsNullOrEmpty(result1.JsonRPCAction.Method))
if (!String.IsNullOrEmpty(result1.JsonRPCAction.Method))
{
if (result1.JsonRPCAction.Method.StartsWith("Wox."))
{
@@ -59,7 +60,7 @@ namespace Wox.Core.Plugin
string actionReponse = ExecuteCallback(result1.JsonRPCAction);
JsonRPCRequestModel jsonRpcRequestModel = JsonConvert.DeserializeObject<JsonRPCRequestModel>(actionReponse);
if (jsonRpcRequestModel != null
&& !string.IsNullOrEmpty(jsonRpcRequestModel.Method)
&& !String.IsNullOrEmpty(jsonRpcRequestModel.Method)
&& jsonRpcRequestModel.Method.StartsWith("Wox."))
{
ExecuteWoxAPI(jsonRpcRequestModel.Method.Substring(4), jsonRpcRequestModel.Parameters);
@@ -125,7 +126,7 @@ namespace Wox.Core.Plugin
{
using (Process process = Process.Start(startInfo))
{
if (process != null)
if (process != null)
{
using (StreamReader reader = process.StandardOutput)
{
@@ -135,12 +136,12 @@ namespace Wox.Core.Plugin
MessageBox.Show(new Form { TopMost = true }, result.Substring(6));
return "";
}
if (string.IsNullOrEmpty(result))
if (String.IsNullOrEmpty(result))
{
using (StreamReader errorReader = process.StandardError)
{
string error = errorReader.ReadToEnd();
if (!string.IsNullOrEmpty(error))
if (!String.IsNullOrEmpty(error))
{
throw new WoxJsonRPCException(error);
}

View File

@@ -1,21 +0,0 @@
using System.Collections.Generic;
using System.Linq;
using Wox.Plugin;
namespace Wox.Core.Plugin
{
internal class JsonRPCPluginLoader<T> : IPluginLoader where T : JsonRPCPlugin, new()
{
public IEnumerable<PluginPair> LoadPlugin(List<PluginMetadata> pluginMetadatas)
{
T jsonRPCPlugin = new T();
List<PluginMetadata> jsonRPCPluginMetadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == jsonRPCPlugin.SupportedLanguage.ToUpper()).ToList();
return jsonRPCPluginMetadatas.Select(metadata => new PluginPair
{
Plugin = new T(), //every JsonRPC plugin should has its own plugin instance
Metadata = metadata
}).ToList();
}
}
}

View File

@@ -25,13 +25,11 @@ namespace Wox.Core.Plugin
/// </summary>
public static List<PluginPair> AllPlugins { get; private set; }
public static readonly List<PluginPair> GlobalPlugins = new List<PluginPair>();
public static readonly Dictionary<string, PluginPair> NonGlobalPlugins = new Dictionary<string, PluginPair>();
private static IEnumerable<PluginPair> InstantQueryPlugins { get; set; }
public static IPublicAPI API { private set; get; }
private static PluginsSettings _settings;
private static readonly string[] Directories = {Infrastructure.Wox.PreinstalledDirectory, Infrastructure.Wox.UserDirectory };
@@ -57,12 +55,15 @@ namespace Wox.Core.Plugin
}
}
public static void InitializePlugins(IPublicAPI api)
public static void InitializePlugins(IPublicAPI api, PluginsSettings settings)
{
_settings = settings;
var metadatas = PluginConfig.Parse(Directories);
var plugins1 = new CSharpPluginLoader().LoadPlugin(metadatas);
var plugins2 = new JsonRPCPluginLoader<PythonPlugin>().LoadPlugin(metadatas);
var plugins1 = PluginsLoader.CSharpPlugins(metadatas);
var plugins2 = PluginsLoader.PythonPlugins(metadatas, _settings.PythonDirectory);
AllPlugins = plugins1.Concat(plugins2).ToList();
_settings.UpdatePluginSettings(AllPlugins);
//load plugin i18n languages
ResourceMerger.UpdatePluginLanguages();

View File

@@ -1,37 +1,25 @@
using System.Diagnostics;
using System.IO;
using System;
using System.Diagnostics;
using Wox.Core.UserSettings;
using Wox.Infrastructure;
using Wox.Plugin;
namespace Wox.Core.Plugin
{
internal class PythonPlugin : JsonRPCPlugin
{
private static readonly string PythonHome = Path.Combine(Infrastructure.Wox.ProgramPath, "PythonHome");
private readonly ProcessStartInfo _startInfo;
public override string SupportedLanguage { get; set; } = AllowedLanguage.Python;
public override string SupportedLanguage => AllowedLanguage.Python;
public PythonPlugin()
public PythonPlugin(string filename)
{
_startInfo = new ProcessStartInfo
{
FileName = @"C:\Program Files\Python 3.5\pythonw.exe",
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);
}
else
{
_startInfo.EnvironmentVariables["PYTHONPATH"] = additionalPythonPath;
}
}
protected override string ExecuteQuery(Query query)
@@ -39,11 +27,10 @@ namespace Wox.Core.Plugin
JsonRPCServerRequestModel request = new JsonRPCServerRequestModel
{
Method = "query",
Parameters = new object[] { query.GetAllRemainingParameter() },
Parameters = new object[] { query.Search },
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(PythonHome, "pythonw.exe");
_startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{request}\"";
return Execute(_startInfo);
@@ -51,7 +38,6 @@ namespace Wox.Core.Plugin
protected override string ExecuteCallback(JsonRPCRequestModel rpcRequest)
{
_startInfo.FileName = Path.Combine(PythonHome, "pythonw.exe");
_startInfo.Arguments = $"-B \"{context.CurrentPluginMetadata.ExecuteFilePath}\" \"{rpcRequest}\"";
return Execute(_startInfo);
}