add AutoAjustScore option for results

This commit is contained in:
qianlifeng
2014-01-15 22:45:02 +08:00
parent 93d8f66afc
commit 56118a7142
15 changed files with 106 additions and 147 deletions

View File

@@ -21,7 +21,7 @@ namespace WinAlfred.PluginLoader
try
{
Assembly asm = Assembly.LoadFile(metadata.ExecuteFilePath);
List<Type> types = asm.GetTypes().Where(o => o.IsClass && o.GetInterfaces().Contains(typeof(IPlugin)) || o.GetInterfaces().Contains(typeof(ISystemPlugin))).ToList();
List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && (o.BaseType == typeof(BaseSystemPlugin) || o.GetInterfaces().Contains(typeof(IPlugin)))).ToList();
if (types.Count == 0)
{
Log.Error(string.Format("Cound't load plugin {0}: didn't find the class who implement IPlugin",
@@ -57,7 +57,7 @@ namespace WinAlfred.PluginLoader
private void InitPlugin(List<PluginPair> plugins)
{
}
}
}

View File

@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Documents;
using Newtonsoft.Json;
using Python.Runtime;
using WinAlfred.Helper;
using WinAlfred.Plugin;
namespace WinAlfred.PluginLoader
@@ -12,17 +15,19 @@ namespace WinAlfred.PluginLoader
{
private PluginMetadata metadata;
private string moduleName;
public PythonPluginWrapper(PluginMetadata metadata)
{
this.metadata = metadata;
moduleName = metadata.ExecuteFileName.Replace(".py", "");
}
public List<Result> Query(Query query)
{
try
{
string jsonResult = InvokeFunc(metadata.PluginDirecotry, metadata.ExecuteFileName.Replace(".py", ""), "query", query.RawQuery);
string jsonResult = InvokeFunc("query", query.RawQuery);
if (string.IsNullOrEmpty(jsonResult))
{
return new List<Result>();
@@ -35,7 +40,7 @@ namespace WinAlfred.PluginLoader
PythonResult ps = pythonResult;
if (!string.IsNullOrEmpty(ps.ActionName))
{
ps.Action = () => InvokeFunc(metadata.PluginDirecotry, metadata.ExecuteFileName.Replace(".py", ""), ps.ActionName, ps.ActionPara);
ps.Action = () => InvokeFunc(ps.ActionName, ps.ActionPara);
}
r.Add(ps);
}
@@ -49,16 +54,37 @@ namespace WinAlfred.PluginLoader
}
#endif
}
}
private string InvokeFunc(string path, string moduleName, string func, string para)
private string InvokeFunc(string func, params string[] para)
{
string json;
PyObject[] paras = { };
if (para != null && para.Length > 0)
{
paras = para.Select(o => new PyString(o)).ToArray();
}
IntPtr gs = PythonEngine.AcquireLock();
PyObject module = PythonEngine.ImportModule(moduleName);
PyObject res = module.InvokeMethod(func, new PyString(para));
string json = Runtime.GetManagedString(res.Handle);
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
}
PythonEngine.ReleaseLock(gs);