2013-12-21 01:20:17 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2013-12-23 19:21:51 +08:00
|
|
|
|
using System.Runtime.InteropServices;
|
2014-01-11 00:19:14 +08:00
|
|
|
|
using System.Windows.Documents;
|
2013-12-23 23:53:38 +08:00
|
|
|
|
using Newtonsoft.Json;
|
2014-01-11 00:19:14 +08:00
|
|
|
|
using Python.Runtime;
|
2013-12-21 01:20:17 +08:00
|
|
|
|
using WinAlfred.Plugin;
|
|
|
|
|
|
|
|
|
|
namespace WinAlfred.PluginLoader
|
|
|
|
|
{
|
|
|
|
|
public class PythonPluginWrapper : IPlugin
|
|
|
|
|
{
|
2014-01-11 15:02:17 +08:00
|
|
|
|
|
2013-12-23 19:21:51 +08:00
|
|
|
|
private PluginMetadata metadata;
|
|
|
|
|
|
|
|
|
|
public PythonPluginWrapper(PluginMetadata metadata)
|
2013-12-21 01:20:17 +08:00
|
|
|
|
{
|
2013-12-23 19:21:51 +08:00
|
|
|
|
this.metadata = metadata;
|
2013-12-21 01:20:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Result> Query(Query query)
|
|
|
|
|
{
|
2013-12-27 00:39:07 +08:00
|
|
|
|
try
|
2013-12-26 20:18:08 +08:00
|
|
|
|
{
|
2014-01-11 15:02:17 +08:00
|
|
|
|
string jsonResult = InvokeFunc(metadata.PluginDirecotry, metadata.ExecuteFileName.Replace(".py", ""), "query", query.RawQuery);
|
|
|
|
|
if (string.IsNullOrEmpty(jsonResult))
|
2013-12-27 20:06:49 +08:00
|
|
|
|
{
|
|
|
|
|
return new List<Result>();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-11 15:02:17 +08:00
|
|
|
|
List<PythonResult> o = JsonConvert.DeserializeObject<List<PythonResult>>(jsonResult);
|
2013-12-27 00:39:07 +08:00
|
|
|
|
List<Result> r = new List<Result>();
|
|
|
|
|
foreach (PythonResult pythonResult in o)
|
|
|
|
|
{
|
|
|
|
|
PythonResult ps = pythonResult;
|
2013-12-27 20:06:49 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(ps.ActionName))
|
|
|
|
|
{
|
2014-01-11 00:19:14 +08:00
|
|
|
|
ps.Action = () => InvokeFunc(metadata.PluginDirecotry, metadata.ExecuteFileName.Replace(".py", ""), ps.ActionName, ps.ActionPara);
|
2013-12-27 20:06:49 +08:00
|
|
|
|
}
|
2013-12-27 00:39:07 +08:00
|
|
|
|
r.Add(ps);
|
|
|
|
|
}
|
|
|
|
|
return r;
|
2013-12-26 20:18:08 +08:00
|
|
|
|
}
|
2013-12-27 00:39:07 +08:00
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
2014-01-11 15:02:17 +08:00
|
|
|
|
#if (DEBUG)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2013-12-27 00:39:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-21 01:20:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-11 15:02:17 +08:00
|
|
|
|
private string InvokeFunc(string path, string moduleName, string func, string para)
|
2014-01-11 00:19:14 +08:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-03 23:52:36 +08:00
|
|
|
|
public void Init(PluginInitContext context)
|
2013-12-21 01:20:17 +08:00
|
|
|
|
{
|
2014-01-11 15:02:17 +08:00
|
|
|
|
|
2013-12-21 01:20:17 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|