2013-12-20 18:20:17 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-01-15 15:45:02 +01:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2013-12-23 12:21:51 +01:00
|
|
|
|
using System.Runtime.InteropServices;
|
2014-01-10 17:19:14 +01:00
|
|
|
|
using System.Windows.Documents;
|
2013-12-23 16:53:38 +01:00
|
|
|
|
using Newtonsoft.Json;
|
2014-01-10 17:19:14 +01:00
|
|
|
|
using Python.Runtime;
|
2014-01-29 11:33:24 +01:00
|
|
|
|
using Wox.Helper;
|
|
|
|
|
using Wox.Plugin;
|
2013-12-20 18:20:17 +01:00
|
|
|
|
|
2014-01-29 11:33:24 +01:00
|
|
|
|
namespace Wox.PluginLoader
|
2013-12-20 18:20:17 +01:00
|
|
|
|
{
|
|
|
|
|
public class PythonPluginWrapper : IPlugin
|
|
|
|
|
{
|
2014-01-11 08:02:17 +01:00
|
|
|
|
|
2013-12-23 12:21:51 +01:00
|
|
|
|
private PluginMetadata metadata;
|
2014-01-15 15:45:02 +01:00
|
|
|
|
private string moduleName;
|
2013-12-23 12:21:51 +01:00
|
|
|
|
|
|
|
|
|
public PythonPluginWrapper(PluginMetadata metadata)
|
2013-12-20 18:20:17 +01:00
|
|
|
|
{
|
2013-12-23 12:21:51 +01:00
|
|
|
|
this.metadata = metadata;
|
2014-01-15 15:45:02 +01:00
|
|
|
|
moduleName = metadata.ExecuteFileName.Replace(".py", "");
|
2013-12-20 18:20:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Result> Query(Query query)
|
|
|
|
|
{
|
2013-12-26 17:39:07 +01:00
|
|
|
|
try
|
2013-12-26 13:18:08 +01:00
|
|
|
|
{
|
2014-01-15 15:45:02 +01:00
|
|
|
|
string jsonResult = InvokeFunc("query", query.RawQuery);
|
2014-01-11 08:02:17 +01:00
|
|
|
|
if (string.IsNullOrEmpty(jsonResult))
|
2013-12-27 13:06:49 +01:00
|
|
|
|
{
|
|
|
|
|
return new List<Result>();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-11 08:02:17 +01:00
|
|
|
|
List<PythonResult> o = JsonConvert.DeserializeObject<List<PythonResult>>(jsonResult);
|
2013-12-26 17:39:07 +01:00
|
|
|
|
List<Result> r = new List<Result>();
|
|
|
|
|
foreach (PythonResult pythonResult in o)
|
|
|
|
|
{
|
|
|
|
|
PythonResult ps = pythonResult;
|
2013-12-27 13:06:49 +01:00
|
|
|
|
if (!string.IsNullOrEmpty(ps.ActionName))
|
|
|
|
|
{
|
2014-01-15 15:45:02 +01:00
|
|
|
|
ps.Action = () => InvokeFunc(ps.ActionName, ps.ActionPara);
|
2013-12-27 13:06:49 +01:00
|
|
|
|
}
|
2013-12-26 17:39:07 +01:00
|
|
|
|
r.Add(ps);
|
|
|
|
|
}
|
|
|
|
|
return r;
|
2013-12-26 13:18:08 +01:00
|
|
|
|
}
|
2013-12-26 17:39:07 +01:00
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
2014-01-11 08:02:17 +01:00
|
|
|
|
#if (DEBUG)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2013-12-26 17:39:07 +01:00
|
|
|
|
}
|
2013-12-20 18:20:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-15 15:45:02 +01:00
|
|
|
|
private string InvokeFunc(string func, params string[] para)
|
2014-01-10 17:19:14 +01:00
|
|
|
|
{
|
2014-01-15 15:45:02 +01:00
|
|
|
|
string json;
|
|
|
|
|
|
|
|
|
|
PyObject[] paras = { };
|
|
|
|
|
if (para != null && para.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
paras = para.Select(o => new PyString(o)).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-10 17:19:14 +01:00
|
|
|
|
IntPtr gs = PythonEngine.AcquireLock();
|
|
|
|
|
|
|
|
|
|
PyObject module = PythonEngine.ImportModule(moduleName);
|
2014-01-15 15:45:02 +01:00
|
|
|
|
if (module.HasAttr(func))
|
|
|
|
|
{
|
|
|
|
|
PyObject res = paras.Length > 0 ? module.InvokeMethod(func, paras) : module.InvokeMethod(func);
|
|
|
|
|
json = Runtime.GetManagedString(res.Handle);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string error = string.Format("Python Invoke failed: {0} doesn't has function {1}",
|
|
|
|
|
metadata.ExecuteFilePath, func);
|
|
|
|
|
Log.Error(error);
|
|
|
|
|
#if (DEBUG)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException(error);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2014-01-10 17:19:14 +01:00
|
|
|
|
|
|
|
|
|
PythonEngine.ReleaseLock(gs);
|
|
|
|
|
|
|
|
|
|
return json;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-03 16:52:36 +01:00
|
|
|
|
public void Init(PluginInitContext context)
|
2013-12-20 18:20:17 +01:00
|
|
|
|
{
|
2014-01-11 08:02:17 +01:00
|
|
|
|
|
2013-12-20 18:20:17 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|