add PyWinAlfred

This commit is contained in:
qianlifeng
2013-12-23 19:21:51 +08:00
parent 57b381311c
commit fffb97ea8d
11 changed files with 293 additions and 54 deletions

View File

@@ -66,12 +66,26 @@ namespace WinAlfred.PluginLoader
if (!AllowedLanguage.IsAllowed(metadata.Language))
{
Log.Error(string.Format("Parse ini {0} failed: invalid language {1}", iniPath, metadata.Language));
string error = string.Format("Parse ini {0} failed: invalid language {1}", iniPath,
metadata.Language);
Log.Error(error);
#if (DEBUG)
{
throw new WinAlfredException(error);
}
#endif
return null;
}
if (!File.Exists(metadata.ExecuteFile))
{
Log.Error(string.Format("Parse ini {0} failed: ExecuteFile didn't exist {1}", iniPath, metadata.ExecuteFile));
string error = string.Format("Parse ini {0} failed: ExecuteFile didn't exist {1}", iniPath,
metadata.ExecuteFile);
Log.Error(error);
#if (DEBUG)
{
throw new WinAlfredException(error);
}
#endif
return null;
}
@@ -80,6 +94,11 @@ namespace WinAlfred.PluginLoader
catch (Exception e)
{
Log.Error(string.Format("Parse ini {0} failed: {1}", iniPath, e.Message));
#if (DEBUG)
{
throw;
}
#endif
return null;
}
}

View File

@@ -13,7 +13,7 @@ namespace WinAlfred.PluginLoader
List<PluginMetadata> metadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.Python.ToUpper()).ToList();
foreach (PluginMetadata metadata in metadatas)
{
PythonPluginWrapper python = new PythonPluginWrapper(metadata.ExecuteFile);
PythonPluginWrapper python = new PythonPluginWrapper(metadata);
PluginPair pair = new PluginPair()
{
Plugin = python,

View File

@@ -1,52 +1,34 @@
using System;
using System.Collections.Generic;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System.Runtime.InteropServices;
using WinAlfred.Plugin;
namespace WinAlfred.PluginLoader
{
public class PythonPluginWrapper : IPlugin
{
private static ScriptEngine engine;
private static ScriptScope scope;
private object pythonInstance;
private PluginMetadata metadata;
[DllImport("PyWinAlfred.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public extern static void ExecPython(string directory, string file, string query);
static PythonPluginWrapper()
{
//creating engine and stuff
engine = Python.CreateEngine();
scope = engine.CreateScope();
var paths = engine.GetSearchPaths();
paths.Add(AppDomain.CurrentDomain.BaseDirectory + @"PythonEnv\2.7\Lib\");
engine.SetSearchPaths(paths);
}
public PythonPluginWrapper(string file)
public PythonPluginWrapper(PluginMetadata metadata)
{
pythonInstance = GetPythonClassInstance(file, "winAlfred");
this.metadata = metadata;
}
private object GetPythonClassInstance(string file, string className)
{
ScriptSource source = engine.CreateScriptSourceFromFile(file);
CompiledCode compiled = source.Compile();
//now executing this code (the code should contain a class)
compiled.Execute(scope);
//now creating an object that could be used to access the stuff inside a python script
return engine.Operations.Invoke(scope.GetVariable(className));
}
public List<Result> Query(Query query)
{
List<Result> results = new List<Result>();
object invokeMember = engine.Operations.InvokeMember(pythonInstance, "query", query.RawQuery);
ExecPython(metadata.PluginDirecotry, metadata.ExecuteFile.Replace(".py", ""), query.RawQuery);
results.Add(new Result()
{
Title = invokeMember.ToString()
});
return results;
}