PowerToys/WinAlfred/PluginLoader/PythonPluginWrapper.cs

52 lines
1.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2013-12-23 19:21:51 +08:00
using System.Runtime.InteropServices;
2013-12-23 23:53:38 +08:00
using Newtonsoft.Json;
using WinAlfred.Plugin;
namespace WinAlfred.PluginLoader
{
public class PythonPluginWrapper : IPlugin
{
2013-12-23 19:21:51 +08:00
private PluginMetadata metadata;
[DllImport("PyWinAlfred.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
2013-12-27 00:39:07 +08:00
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();
2013-12-23 19:21:51 +08:00
public PythonPluginWrapper(PluginMetadata metadata)
{
2013-12-23 19:21:51 +08:00
this.metadata = metadata;
}
public List<Result> Query(Query query)
{
2013-12-27 00:39:07 +08:00
try
2013-12-26 20:18:08 +08:00
{
2013-12-27 00:39:07 +08:00
string s = Marshal.PtrToStringAnsi(ExecPython(metadata.PluginDirecotry, metadata.ExecuteFileName.Replace(".py", ""), "query", query.RawQuery));
List<PythonResult> o = JsonConvert.DeserializeObject<List<PythonResult>>(s);
List<Result> r = new List<Result>();
foreach (PythonResult pythonResult in o)
{
PythonResult ps = pythonResult;
ps.Action = () => ExecPython(metadata.PluginDirecotry, metadata.ExecuteFileName.Replace(".py", ""), ps.ActionName, ps.ActionPara);
r.Add(ps);
}
return r;
2013-12-26 20:18:08 +08:00
}
2013-12-27 00:39:07 +08:00
catch (Exception)
{
throw;
}
}
public void Init()
{
InitPythonEnv();
}
}
}