2014-01-03 18:16:05 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-01-11 15:02:17 +08:00
|
|
|
|
using System.Linq;
|
2014-01-03 23:52:36 +08:00
|
|
|
|
using System.Threading;
|
2014-01-11 15:02:17 +08:00
|
|
|
|
using Python.Runtime;
|
2014-01-29 18:33:24 +08:00
|
|
|
|
using Wox.Helper;
|
|
|
|
|
using Wox.Plugin;
|
|
|
|
|
using Wox.PluginLoader;
|
2014-01-03 18:16:05 +08:00
|
|
|
|
|
2014-01-29 18:33:24 +08:00
|
|
|
|
namespace Wox.Commands
|
2014-01-03 18:16:05 +08:00
|
|
|
|
{
|
2014-01-03 23:52:36 +08:00
|
|
|
|
public class PluginCommand : BaseCommand
|
2014-01-03 18:16:05 +08:00
|
|
|
|
{
|
2014-01-11 15:02:17 +08:00
|
|
|
|
private string currentPythonModulePath = string.Empty;
|
|
|
|
|
private IntPtr GIL;
|
|
|
|
|
|
2014-03-18 23:41:34 +08:00
|
|
|
|
public override void Dispatch(Query q)
|
2014-01-03 23:52:36 +08:00
|
|
|
|
{
|
2014-01-11 15:02:17 +08:00
|
|
|
|
PluginPair thirdPlugin = Plugins.AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == q.ActionName);
|
|
|
|
|
if (thirdPlugin != null && !string.IsNullOrEmpty(thirdPlugin.Metadata.ActionKeyword))
|
2014-01-03 18:16:05 +08:00
|
|
|
|
{
|
2014-01-11 15:02:17 +08:00
|
|
|
|
if (thirdPlugin.Metadata.Language == AllowedLanguage.Python)
|
2014-01-03 18:16:05 +08:00
|
|
|
|
{
|
2014-01-11 15:02:17 +08:00
|
|
|
|
SwitchPythonEnv(thirdPlugin);
|
|
|
|
|
}
|
|
|
|
|
ThreadPool.QueueUserWorkItem(t =>
|
|
|
|
|
{
|
|
|
|
|
try
|
2014-01-03 18:16:05 +08:00
|
|
|
|
{
|
2014-03-23 02:46:14 +08:00
|
|
|
|
List<Result> r = thirdPlugin.Plugin.Query(q) ?? new List<Result>();
|
2014-01-11 15:02:17 +08:00
|
|
|
|
r.ForEach(o =>
|
2014-01-03 18:16:05 +08:00
|
|
|
|
{
|
2014-01-11 15:02:17 +08:00
|
|
|
|
o.PluginDirectory = thirdPlugin.Metadata.PluginDirecotry;
|
|
|
|
|
o.OriginQuery = q;
|
|
|
|
|
});
|
2014-03-18 23:41:34 +08:00
|
|
|
|
UpdateResultView(r);
|
2014-01-11 15:02:17 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception queryException)
|
|
|
|
|
{
|
|
|
|
|
Log.Error(string.Format("Plugin {0} query failed: {1}", thirdPlugin.Metadata.Name,
|
|
|
|
|
queryException.Message));
|
2014-01-03 23:52:36 +08:00
|
|
|
|
#if (DEBUG)
|
2014-01-11 15:02:17 +08:00
|
|
|
|
{
|
|
|
|
|
throw;
|
2014-01-03 23:52:36 +08:00
|
|
|
|
}
|
2014-01-11 15:02:17 +08:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SwitchPythonEnv(PluginPair thirdPlugin)
|
|
|
|
|
{
|
|
|
|
|
if (currentPythonModulePath != thirdPlugin.Metadata.PluginDirecotry)
|
|
|
|
|
{
|
|
|
|
|
currentPythonModulePath = thirdPlugin.Metadata.PluginDirecotry;
|
|
|
|
|
|
|
|
|
|
if (GIL != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
Runtime.PyEval_RestoreThread(GIL);
|
|
|
|
|
PythonEngine.Shutdown();
|
2014-01-03 18:16:05 +08:00
|
|
|
|
}
|
2014-01-11 15:02:17 +08:00
|
|
|
|
PythonEngine.Initialize();
|
|
|
|
|
IntPtr pyStrPtr = Runtime.PyString_FromString(thirdPlugin.Metadata.PluginDirecotry);
|
2014-02-06 22:22:02 +08:00
|
|
|
|
IntPtr sysDotPath = Runtime.PySys_GetObject("path");
|
|
|
|
|
Runtime.PyList_Append(sysDotPath, pyStrPtr);
|
2014-01-11 15:02:17 +08:00
|
|
|
|
GIL = PythonEngine.BeginAllowThreads();
|
2014-01-03 18:16:05 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|