Merge branch 'PythonPlugin'

This commit is contained in:
qianlifeng
2014-01-12 16:31:06 +08:00
79 changed files with 14520 additions and 360 deletions

View File

@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Python.Runtime;
using WinAlfred.Helper;
using WinAlfred.Plugin;
using WinAlfred.PluginLoader;
@@ -9,6 +11,9 @@ namespace WinAlfred.Commands
{
public class PluginCommand : BaseCommand
{
private string currentPythonModulePath = string.Empty;
private IntPtr GIL;
public PluginCommand(MainWindow mainWindow)
: base(mainWindow)
{
@@ -17,36 +22,56 @@ namespace WinAlfred.Commands
public override void Dispatch(Query q)
{
foreach (PluginPair pair in Plugins.AllPlugins)
PluginPair thirdPlugin = Plugins.AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == q.ActionName);
if (thirdPlugin != null && !string.IsNullOrEmpty(thirdPlugin.Metadata.ActionKeyword))
{
if (pair.Metadata.ActionKeyword == q.ActionName)
if (thirdPlugin.Metadata.Language == AllowedLanguage.Python)
{
PluginPair pair1 = pair;
ThreadPool.QueueUserWorkItem(state =>
{
try
{
List<Result> r = pair1.Plugin.Query(q);
r.ForEach(o =>
{
o.PluginDirectory = pair1.Metadata.PluginDirecotry;
o.OriginQuery = q;
});
UpdateResultView(r);
}
catch (Exception queryException)
{
Log.Error(string.Format("Plugin {0} query failed: {1}", pair1.Metadata.Name,
queryException.Message));
#if (DEBUG)
{
throw;
}
#endif
}
});
SwitchPythonEnv(thirdPlugin);
}
ThreadPool.QueueUserWorkItem(t =>
{
try
{
List<Result> r = thirdPlugin.Plugin.Query(q);
r.ForEach(o =>
{
o.PluginDirectory = thirdPlugin.Metadata.PluginDirecotry;
o.OriginQuery = q;
});
UpdateResultView(r);
}
catch (Exception queryException)
{
Log.Error(string.Format("Plugin {0} query failed: {1}", thirdPlugin.Metadata.Name,
queryException.Message));
#if (DEBUG)
{
throw;
}
#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();
}
PythonEngine.Initialize();
IntPtr pyStrPtr = Runtime.PyString_FromString(thirdPlugin.Metadata.PluginDirecotry);
IntPtr SysDotPath = Runtime.PySys_GetObject("path");
Runtime.PyList_Append(SysDotPath, pyStrPtr);
GIL = PythonEngine.BeginAllowThreads();
}
}
}

View File

@@ -117,5 +117,16 @@ namespace WinAlfred.PluginLoader
return null;
}
}
///// <summary>
///// Change python execute file name to unique file name using GUID
///// this is because if two pythong plugin use the same
///// </summary>
///// <param name="metadata"></param>
///// <returns></returns>
//private static PluginMetadata filterPythonMetadata(PluginMetadata metadata)
//{
//}
}
}

View File

@@ -19,15 +19,18 @@ namespace WinAlfred.PluginLoader
foreach (IPlugin plugin in plugins.Select(pluginPair => pluginPair.Plugin))
{
IPlugin plugin1 = plugin;
ThreadPool.QueueUserWorkItem(o => plugin1.Init(new PluginInitContext()
ThreadPool.QueueUserWorkItem(o =>
{
Plugins = plugins,
ChangeQuery = s => window.ChangeQuery(s),
CloseApp = window.CloseApp,
HideApp = window.HideApp,
ShowApp = window.ShowApp,
ShowMsg = (title, subTitle, iconPath) => window.ShowMsg(title, subTitle, iconPath)
}));
plugin1.Init(new PluginInitContext()
{
Plugins = plugins,
ChangeQuery = s => window.ChangeQuery(s),
CloseApp = window.CloseApp,
HideApp = window.HideApp,
ShowApp = window.ShowApp,
ShowMsg = (title, subTitle, iconPath) => window.ShowMsg(title, subTitle, iconPath)
});
});
}
}

View File

@@ -1,12 +1,20 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Python.Runtime;
using WinAlfred.Plugin;
namespace WinAlfred.PluginLoader
{
public class PythonPluginLoader : BasePluginLoader
{
static PythonPluginLoader()
{
}
public override List<PluginPair> LoadPlugin()
{
List<PluginPair> plugins = new List<PluginPair>();

View File

@@ -1,19 +1,17 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Documents;
using Newtonsoft.Json;
using Python.Runtime;
using WinAlfred.Plugin;
namespace WinAlfred.PluginLoader
{
public class PythonPluginWrapper : IPlugin
{
private PluginMetadata metadata;
[DllImport("PyWinAlfred.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private extern static IntPtr ExecPython(string directory, string file, string method, string para);
[DllImport("PyWinAlfred.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private extern static void InitPythonEnv();
private PluginMetadata metadata;
public PythonPluginWrapper(PluginMetadata metadata)
{
@@ -24,24 +22,20 @@ namespace WinAlfred.PluginLoader
{
try
{
string s = Marshal.PtrToStringAnsi(ExecPython(metadata.PluginDirecotry, metadata.ExecuteFileName.Replace(".py", ""), "query", query.RawQuery));
if (string.IsNullOrEmpty(s))
string jsonResult = InvokeFunc(metadata.PluginDirecotry, metadata.ExecuteFileName.Replace(".py", ""), "query", query.RawQuery);
if (string.IsNullOrEmpty(jsonResult))
{
return new List<Result>();
}
if (s.StartsWith("PYTHONERROR"))
{
throw new ArgumentException(s);
}
List<PythonResult> o = JsonConvert.DeserializeObject<List<PythonResult>>(s);
List<PythonResult> o = JsonConvert.DeserializeObject<List<PythonResult>>(jsonResult);
List<Result> r = new List<Result>();
foreach (PythonResult pythonResult in o)
{
PythonResult ps = pythonResult;
if (!string.IsNullOrEmpty(ps.ActionName))
{
ps.Action = () => ExecPython(metadata.PluginDirecotry, metadata.ExecuteFileName.Replace(".py", ""), ps.ActionName, ps.ActionPara);
ps.Action = () => InvokeFunc(metadata.PluginDirecotry, metadata.ExecuteFileName.Replace(".py", ""), ps.ActionName, ps.ActionPara);
}
r.Add(ps);
}
@@ -49,15 +43,31 @@ namespace WinAlfred.PluginLoader
}
catch (Exception)
{
throw;
#if (DEBUG)
{
throw;
}
#endif
}
}
private string InvokeFunc(string path, string moduleName, string func, string para)
{
IntPtr gs = PythonEngine.AcquireLock();
PyObject module = PythonEngine.ImportModule(moduleName);
PyObject res = module.InvokeMethod(func, new PyString(para));
string json = Runtime.GetManagedString(res.Handle);
PythonEngine.ReleaseLock(gs);
return json;
}
public void Init(PluginInitContext context)
{
InitPythonEnv();
}
}
}