2014-01-03 10:16:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-01-11 07:02:17 +00:00
|
|
|
|
using System.Linq;
|
2014-01-03 15:52:36 +00:00
|
|
|
|
using System.Threading;
|
2014-01-11 07:02:17 +00:00
|
|
|
|
using Python.Runtime;
|
2014-01-03 10:16:05 +00:00
|
|
|
|
using WinAlfred.Helper;
|
|
|
|
|
using WinAlfred.Plugin;
|
|
|
|
|
using WinAlfred.PluginLoader;
|
|
|
|
|
|
|
|
|
|
namespace WinAlfred.Commands
|
|
|
|
|
{
|
2014-01-03 15:52:36 +00:00
|
|
|
|
public class PluginCommand : BaseCommand
|
2014-01-03 10:16:05 +00:00
|
|
|
|
{
|
2014-01-11 07:02:17 +00:00
|
|
|
|
private string currentPythonModulePath = string.Empty;
|
|
|
|
|
private IntPtr GIL;
|
|
|
|
|
|
2014-01-26 13:18:01 +00:00
|
|
|
|
public override void Dispatch(Query q,bool updateView = true)
|
2014-01-03 15:52:36 +00:00
|
|
|
|
{
|
2014-01-11 07:02:17 +00:00
|
|
|
|
PluginPair thirdPlugin = Plugins.AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == q.ActionName);
|
|
|
|
|
if (thirdPlugin != null && !string.IsNullOrEmpty(thirdPlugin.Metadata.ActionKeyword))
|
2014-01-03 10:16:05 +00:00
|
|
|
|
{
|
2014-01-11 07:02:17 +00:00
|
|
|
|
if (thirdPlugin.Metadata.Language == AllowedLanguage.Python)
|
2014-01-03 10:16:05 +00:00
|
|
|
|
{
|
2014-01-11 07:02:17 +00:00
|
|
|
|
SwitchPythonEnv(thirdPlugin);
|
|
|
|
|
}
|
|
|
|
|
ThreadPool.QueueUserWorkItem(t =>
|
|
|
|
|
{
|
|
|
|
|
try
|
2014-01-03 10:16:05 +00:00
|
|
|
|
{
|
2014-01-11 07:02:17 +00:00
|
|
|
|
List<Result> r = thirdPlugin.Plugin.Query(q);
|
|
|
|
|
r.ForEach(o =>
|
2014-01-03 10:16:05 +00:00
|
|
|
|
{
|
2014-01-11 07:02:17 +00:00
|
|
|
|
o.PluginDirectory = thirdPlugin.Metadata.PluginDirecotry;
|
|
|
|
|
o.OriginQuery = q;
|
|
|
|
|
});
|
2014-01-13 10:43:44 +00:00
|
|
|
|
if(updateView) UpdateResultView(r);
|
2014-01-11 07:02:17 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception queryException)
|
|
|
|
|
{
|
|
|
|
|
Log.Error(string.Format("Plugin {0} query failed: {1}", thirdPlugin.Metadata.Name,
|
|
|
|
|
queryException.Message));
|
2014-01-03 15:52:36 +00:00
|
|
|
|
#if (DEBUG)
|
2014-01-11 07:02:17 +00:00
|
|
|
|
{
|
|
|
|
|
throw;
|
2014-01-03 15:52:36 +00:00
|
|
|
|
}
|
2014-01-11 07:02:17 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SwitchPythonEnv(PluginPair thirdPlugin)
|
|
|
|
|
{
|
|
|
|
|
if (currentPythonModulePath != thirdPlugin.Metadata.PluginDirecotry)
|
|
|
|
|
{
|
|
|
|
|
//this must initial in main thread
|
|
|
|
|
currentPythonModulePath = thirdPlugin.Metadata.PluginDirecotry;
|
|
|
|
|
|
|
|
|
|
if (GIL != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
Runtime.PyEval_RestoreThread(GIL);
|
|
|
|
|
PythonEngine.Shutdown();
|
2014-01-03 10:16:05 +00:00
|
|
|
|
}
|
2014-01-11 07:02:17 +00:00
|
|
|
|
PythonEngine.Initialize();
|
|
|
|
|
IntPtr pyStrPtr = Runtime.PyString_FromString(thirdPlugin.Metadata.PluginDirecotry);
|
|
|
|
|
IntPtr SysDotPath = Runtime.PySys_GetObject("path");
|
|
|
|
|
Runtime.PyList_Append(SysDotPath, pyStrPtr);
|
|
|
|
|
GIL = PythonEngine.BeginAllowThreads();
|
2014-01-03 10:16:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|