add IronPython environment to execute python plugins

This commit is contained in:
qianlifeng 2013-12-21 01:20:17 +08:00
parent f25f4f7dc8
commit 422e19724a
20 changed files with 578 additions and 108 deletions

View File

@ -0,0 +1,4 @@
import os

View File

@ -0,0 +1,8 @@
[plugin]
ActionKeyword = movie
Name = douban
Author = qianlifeng
Version = 0.1
Language = python
Description = test
ExecuteFile = main.py

View File

@ -8,91 +8,274 @@ namespace WinAlfred.Plugin.Everything
{
public sealed class EverythingAPI
{
const int EVERYTHING_OK = 0;
const int EVERYTHING_ERROR_MEMORY = 1;
const int EVERYTHING_ERROR_IPC = 2;
const int EVERYTHING_ERROR_REGISTERCLASSEX = 3;
const int EVERYTHING_ERROR_CREATEWINDOW = 4;
const int EVERYTHING_ERROR_CREATETHREAD = 5;
const int EVERYTHING_ERROR_INVALIDINDEX = 6;
const int EVERYTHING_ERROR_INVALIDCALL = 7;
#region Const
const string EVERYTHING_DLL_NAME = "Everything.dll";
#endregion
[DllImport("Everything.dll")]
#region DllImport
[DllImport(EVERYTHING_DLL_NAME)]
private static extern int Everything_SetSearch(string lpSearchString);
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern void Everything_SetMatchPath(bool bEnable);
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern void Everything_SetMatchCase(bool bEnable);
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern void Everything_SetMatchWholeWord(bool bEnable);
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern void Everything_SetRegex(bool bEnable);
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern void Everything_SetMax(int dwMax);
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern void Everything_SetOffset(int dwOffset);
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern bool Everything_GetMatchPath();
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern bool Everything_GetMatchCase();
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern bool Everything_GetMatchWholeWord();
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern bool Everything_GetRegex();
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern UInt32 Everything_GetMax();
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern UInt32 Everything_GetOffset();
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern string Everything_GetSearch();
[DllImport("Everything.dll")]
private static extern int Everything_GetLastError();
[DllImport(EVERYTHING_DLL_NAME)]
private static extern StateCode Everything_GetLastError();
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern bool Everything_Query();
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern void Everything_SortResultsByPath();
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern int Everything_GetNumFileResults();
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern int Everything_GetNumFolderResults();
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern int Everything_GetNumResults();
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern int Everything_GetTotFileResults();
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern int Everything_GetTotFolderResults();
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern int Everything_GetTotResults();
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern bool Everything_IsVolumeResult(int nIndex);
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern bool Everything_IsFolderResult(int nIndex);
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern bool Everything_IsFileResult(int nIndex);
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern void Everything_GetResultFullPathName(int nIndex, StringBuilder lpString, int nMaxCount);
[DllImport("Everything.dll")]
[DllImport(EVERYTHING_DLL_NAME)]
private static extern void Everything_Reset();
#endregion
public void Search(string query)
#region Enum
enum StateCode
{
int i;
const int bufsize = 260;
StringBuilder buf = new StringBuilder(bufsize);
OK,
MemoryError,
IPCError,
RegisterClassExError,
CreateWindowError,
CreateThreadError,
InvalidIndexError,
InvalidCallError
}
#endregion
Everything_SetSearch(query);
Everything_Query();
// loop through the results, adding each result to the listbox.
for (i = 0; i < Everything_GetNumResults(); i++)
#region Property
/// <summary>
/// Gets or sets a value indicating whether [match path].
/// </summary>
/// <value><c>true</c> if [match path]; otherwise, <c>false</c>.</value>
public Boolean MatchPath
{
// get the result's full path and file name.
Everything_GetResultFullPathName(i, buf, bufsize);
get
{
return Everything_GetMatchPath();
}
set
{
Everything_SetMatchPath(value);
}
}
/// <summary>
/// Gets or sets a value indicating whether [match case].
/// </summary>
/// <value><c>true</c> if [match case]; otherwise, <c>false</c>.</value>
public Boolean MatchCase
{
get
{
return Everything_GetMatchCase();
}
set
{
Everything_SetMatchCase(value);
}
}
/// <summary>
/// Gets or sets a value indicating whether [match whole word].
/// </summary>
/// <value><c>true</c> if [match whole word]; otherwise, <c>false</c>.</value>
public Boolean MatchWholeWord
{
get
{
return Everything_GetMatchWholeWord();
}
set
{
Everything_SetMatchWholeWord(value);
}
}
/// <summary>
/// Gets or sets a value indicating whether [enable regex].
/// </summary>
/// <value><c>true</c> if [enable regex]; otherwise, <c>false</c>.</value>
public Boolean EnableRegex
{
get
{
return Everything_GetRegex();
}
set
{
Everything_SetRegex(value);
}
}
#endregion
#region Public Method
/// <summary>
/// Resets this instance.
/// </summary>
public void Reset()
{
Everything_Reset();
}
/// <summary>
/// Searches the specified key word.
/// </summary>
/// <param name="keyWord">The key word.</param>
/// <returns></returns>
public IEnumerable<string> Search(string keyWord)
{
return Search(keyWord, 0, int.MaxValue);
}
/// <summary>
/// Searches the specified key word.
/// </summary>
/// <param name="keyWord">The key word.</param>
/// <param name="offset">The offset.</param>
/// <param name="maxCount">The max count.</param>
/// <returns></returns>
public IEnumerable<string> Search(string keyWord, int offset, int maxCount)
{
if (string.IsNullOrEmpty(keyWord))
throw new ArgumentNullException("keyWord");
if (offset < 0)
throw new ArgumentOutOfRangeException("offset");
if (maxCount < 0)
throw new ArgumentOutOfRangeException("maxCount");
Everything_SetSearch(keyWord);
Everything_SetOffset(offset);
Everything_SetMax(maxCount);
if (!Everything_Query())
{
switch (Everything_GetLastError())
{
case StateCode.CreateThreadError:
throw new CreateThreadException();
case StateCode.CreateWindowError:
throw new CreateWindowException();
case StateCode.InvalidCallError:
throw new InvalidCallException();
case StateCode.InvalidIndexError:
throw new InvalidIndexException();
case StateCode.IPCError:
throw new IPCErrorException();
case StateCode.MemoryError:
throw new MemoryErrorException();
case StateCode.RegisterClassExError:
throw new RegisterClassExException();
}
yield break;
}
const int bufferSize = 256;
StringBuilder buffer = new StringBuilder(bufferSize);
for (int idx = 0; idx < Everything_GetNumResults(); ++idx)
{
Everything_GetResultFullPathName(idx, buffer, bufferSize);
yield return buffer.ToString();
}
}
#endregion
}
/// <summary>
///
/// </summary>
public class MemoryErrorException : ApplicationException
{
}
/// <summary>
///
/// </summary>
public class IPCErrorException : ApplicationException
{
}
/// <summary>
///
/// </summary>
public class RegisterClassExException : ApplicationException
{
}
/// <summary>
///
/// </summary>
public class CreateWindowException : ApplicationException
{
}
/// <summary>
///
/// </summary>
public class CreateThreadException : ApplicationException
{
}
/// <summary>
///
/// </summary>
public class InvalidIndexException : ApplicationException
{
}
/// <summary>
///
/// </summary>
public class InvalidCallException : ApplicationException
{
}
}

View File

@ -9,17 +9,18 @@ namespace WinAlfred.Plugin.Everything
{
EverythingAPI api = new EverythingAPI();
public string GetActionName()
{
return "ev";
}
public List<Result> Query(Query query)
{
var results = new List<Result>();
if (query.ActionParameters.Count > 0)
if (query.ActionParameters.Count > 0 && query.ActionParameters[0].Length > 0)
{
api.Search(query.ActionParameters[0]);
IEnumerable<string> enumerable = api.Search(query.ActionParameters[0]);
foreach (string s in enumerable)
{
Result r = new Result();
r.Title = s;
results.Add(r);
}
}
return results;

View File

@ -20,6 +20,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@ -29,6 +30,24 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />

View File

@ -0,0 +1,8 @@
[plugin]
ActionKeyword = ev
Name = everything
Author = qianlifeng
Version = 0.1
Language = csharp
Description = test
ExecuteFile = WinAlfred.Plugin.Everything.dll

View File

@ -4,7 +4,6 @@ namespace WinAlfred.Plugin
{
public interface IPlugin
{
string GetActionName();
List<Result> Query(Query query);
void Init();
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WinAlfred.Plugin
{
public class PluginPair
{
public IPlugin Plugin { get; set; }
public PluginMetadata Metadata { get; set; }
}
}

View File

@ -12,5 +12,7 @@ namespace WinAlfred.Plugin
public string Version { get; set; }
public string Language { get; set; }
public string Description { get; set; }
public string ExecuteFile { get; set; }
public string ActionKeyword { get; set; }
}
}

View File

@ -20,6 +20,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@ -29,6 +30,24 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
@ -40,6 +59,7 @@
<ItemGroup>
<Compile Include="AllowedLanguage.cs" />
<Compile Include="IPlugin.cs" />
<Compile Include="Plugin.cs" />
<Compile Include="PluginMetadata.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Query.cs" />

View File

@ -14,25 +14,39 @@ EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2770F339-701C-4698-8C9F-0FE007DFDFC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2770F339-701C-4698-8C9F-0FE007DFDFC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2770F339-701C-4698-8C9F-0FE007DFDFC5}.Debug|x86.ActiveCfg = Debug|Any CPU
{2770F339-701C-4698-8C9F-0FE007DFDFC5}.Debug|x86.Build.0 = Debug|Any CPU
{2770F339-701C-4698-8C9F-0FE007DFDFC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2770F339-701C-4698-8C9F-0FE007DFDFC5}.Release|Any CPU.Build.0 = Release|Any CPU
{2770F339-701C-4698-8C9F-0FE007DFDFC5}.Release|x86.ActiveCfg = Release|Any CPU
{FF742965-9A80-41A5-B042-D6C7D3A21708}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FF742965-9A80-41A5-B042-D6C7D3A21708}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FF742965-9A80-41A5-B042-D6C7D3A21708}.Debug|x86.ActiveCfg = Debug|Any CPU
{FF742965-9A80-41A5-B042-D6C7D3A21708}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FF742965-9A80-41A5-B042-D6C7D3A21708}.Release|Any CPU.Build.0 = Release|Any CPU
{FF742965-9A80-41A5-B042-D6C7D3A21708}.Release|x86.ActiveCfg = Release|Any CPU
{8451ECDD-2EA4-4966-BB0A-7BBC40138E80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8451ECDD-2EA4-4966-BB0A-7BBC40138E80}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8451ECDD-2EA4-4966-BB0A-7BBC40138E80}.Debug|x86.ActiveCfg = Debug|Any CPU
{8451ECDD-2EA4-4966-BB0A-7BBC40138E80}.Debug|x86.Build.0 = Debug|Any CPU
{8451ECDD-2EA4-4966-BB0A-7BBC40138E80}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8451ECDD-2EA4-4966-BB0A-7BBC40138E80}.Release|Any CPU.Build.0 = Release|Any CPU
{8451ECDD-2EA4-4966-BB0A-7BBC40138E80}.Release|x86.ActiveCfg = Release|Any CPU
{230AE83F-E92E-4E69-8355-426B305DA9C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{230AE83F-E92E-4E69-8355-426B305DA9C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{230AE83F-E92E-4E69-8355-426B305DA9C0}.Debug|x86.ActiveCfg = Debug|Any CPU
{230AE83F-E92E-4E69-8355-426B305DA9C0}.Debug|x86.Build.0 = Debug|Any CPU
{230AE83F-E92E-4E69-8355-426B305DA9C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{230AE83F-E92E-4E69-8355-426B305DA9C0}.Release|Any CPU.Build.0 = Release|Any CPU
{230AE83F-E92E-4E69-8355-426B305DA9C0}.Release|x86.ActiveCfg = Release|x86
{230AE83F-E92E-4E69-8355-426B305DA9C0}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -29,6 +29,7 @@
private void InitializeComponent()
{
this.tbQuery = new System.Windows.Forms.TextBox();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// tbQuery
@ -40,11 +41,21 @@
this.tbQuery.TabIndex = 0;
this.tbQuery.TextChanged += new System.EventHandler(this.TbQuery_TextChanged);
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(12, 39);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(471, 352);
this.listBox1.TabIndex = 1;
//
// FrmMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(495, 45);
this.ClientSize = new System.Drawing.Size(495, 404);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.tbQuery);
this.MaximizeBox = false;
this.MinimizeBox = false;
@ -61,6 +72,7 @@
#endregion
private System.Windows.Forms.TextBox tbQuery;
private System.Windows.Forms.ListBox listBox1;
}

View File

@ -6,6 +6,7 @@ using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinAlfred.Helper;
using WinAlfred.Plugin;
using WinAlfred.PluginLoader;
@ -13,7 +14,7 @@ namespace WinAlfred
{
public partial class FrmMain : Form
{
public List<IPlugin> plugins = new List<IPlugin>();
public List<PluginPair> plugins = new List<PluginPair>();
private List<Result> results = new List<Result>();
public FrmMain()
@ -30,11 +31,34 @@ namespace WinAlfred
private void TbQuery_TextChanged(object sender, EventArgs e)
{
results.Clear();
foreach (IPlugin plugin in plugins)
foreach (PluginPair pair in plugins)
{
results.AddRange(plugin.Query(new Query(tbQuery.Text)));
Query q = new Query(tbQuery.Text);
if (pair.Metadata.ActionKeyword == q.ActionName)
{
try
{
results.AddRange(pair.Plugin.Query(q));
}
catch (Exception queryException)
{
Log.Error(string.Format("Plugin {0} query failed: {1}", pair.Metadata.Name, queryException.Message));
#if (DEBUG)
{
throw;
}
#endif
throw;
}
}
}
var s = results.OrderByDescending(o => o.Score);
listBox1.Items.Clear();
foreach (Result result in results)
{
listBox1.Items.Add(result.Title);
}
}
}

View File

@ -11,9 +11,9 @@ namespace WinAlfred.Helper
{
private static ILog fileLogger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public static ILog FileLogger
public static void Error(string msg)
{
get { return fileLogger; }
fileLogger.Error(msg);
}
}
}

View File

@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using WinAlfred.Helper;
using WinAlfred.Plugin;
using log4net;
namespace WinAlfred.PluginLoader
{
@ -14,14 +12,14 @@ namespace WinAlfred.PluginLoader
private static string PluginConfigName = "plugin.ini";
protected static List<PluginMetadata> pluginMetadatas = new List<PluginMetadata>();
public abstract List<IPlugin> LoadPlugin();
public abstract List<PluginPair> LoadPlugin();
static BasePluginLoader()
{
ParsePlugins();
}
protected static void ParsePlugins()
private static void ParsePlugins()
{
ParseDirectories();
ParsePackagedPlugin();
@ -32,8 +30,7 @@ namespace WinAlfred.PluginLoader
string[] directories = Directory.GetDirectories(PluginPath);
foreach (string directory in directories)
{
string iniPath = directory + "\\" + PluginConfigName;
PluginMetadata metadata = GetMetadataFromIni(iniPath);
PluginMetadata metadata = GetMetadataFromIni(directory);
if (metadata != null) pluginMetadatas.Add(metadata);
}
}
@ -43,11 +40,13 @@ namespace WinAlfred.PluginLoader
}
private static PluginMetadata GetMetadataFromIni(string iniPath)
private static PluginMetadata GetMetadataFromIni(string directory)
{
string iniPath = directory + "\\" + PluginConfigName;
if (!File.Exists(iniPath))
{
Log.FileLogger.Error(string.Format("parse plugin {0} failed: didn't find config file.", iniPath));
Log.Error(string.Format("parse plugin {0} failed: didn't find config file.", iniPath));
return null;
}
@ -61,10 +60,17 @@ namespace WinAlfred.PluginLoader
metadata.Description = ini.GetSetting("plugin", "Description");
metadata.Language = ini.GetSetting("plugin", "Language");
metadata.Version = ini.GetSetting("plugin", "Version");
metadata.ActionKeyword = ini.GetSetting("plugin", "ActionKeyword");
metadata.ExecuteFile = AppDomain.CurrentDomain.BaseDirectory + directory + "\\" + ini.GetSetting("plugin", "ExecuteFile");
if (!AllowedLanguage.IsAllowed(metadata.Language))
{
Log.FileLogger.Error(string.Format("Parse ini {0} failed: invalid language {1}", iniPath, metadata.Language));
Log.Error(string.Format("Parse ini {0} failed: invalid language {1}", iniPath, metadata.Language));
return null;
}
if (!File.Exists(metadata.ExecuteFile))
{
Log.Error(string.Format("Parse ini {0} failed: ExecuteFile didn't exist {1}", iniPath, metadata.ExecuteFile));
return null;
}
@ -72,7 +78,7 @@ namespace WinAlfred.PluginLoader
}
catch (Exception e)
{
Log.FileLogger.Error(string.Format("Parse ini {0} failed: {1}", iniPath, e.Message));
Log.Error(string.Format("Parse ini {0} failed: {1}", iniPath, e.Message));
return null;
}
}

View File

@ -1,18 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using WinAlfred.Helper;
using WinAlfred.Plugin;
namespace WinAlfred.PluginLoader
{
public class CSharpPluginLoader:BasePluginLoader
public class CSharpPluginLoader : BasePluginLoader
{
private List<IPlugin> plugins = new List<IPlugin>();
public override List<PluginPair> LoadPlugin()
{
List<PluginPair> plugins = new List<PluginPair>();
public override List<IPlugin> LoadPlugin()
List<PluginMetadata> metadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp.ToUpper()).ToList();
foreach (PluginMetadata metadata in metadatas)
{
try
{
Assembly asm = Assembly.LoadFile(metadata.ExecuteFile);
List<Type> types = asm.GetTypes().Where(o => 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", metadata.Name));
continue;
}
if (types.Count > 1)
{
Log.Error(string.Format("Cound't load plugin {0}: find more than one class who implement IPlugin, there should only one class implement IPlugin", metadata.Name));
continue;
}
PluginPair pair = new PluginPair()
{
Plugin = Activator.CreateInstance(types[0]) as IPlugin,
Metadata = metadata
};
plugins.Add(pair);
}
catch (Exception e)
{
Log.Error(string.Format("Cound't load plugin {0}: {1}", metadata.Name, e.Message));
#if (DEBUG)
{
throw;
}
#endif
}
}
return plugins;
}
}

View File

@ -2,18 +2,33 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using WinAlfred.Helper;
using WinAlfred.Plugin;
namespace WinAlfred.PluginLoader
{
public class PythonPluginLoader :BasePluginLoader
public class PythonPluginLoader : BasePluginLoader
{
private List<IPlugin> plugins = new List<IPlugin>();
public override List<IPlugin> LoadPlugin()
public override List<PluginPair> LoadPlugin()
{
List<PluginPair> plugins = new List<PluginPair>();
List<PluginMetadata> metadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.Python.ToUpper()).ToList();
foreach (PluginMetadata metadata in metadatas)
{
PythonPluginWrapper python = new PythonPluginWrapper(metadata.ExecuteFile);
PluginPair pair = new PluginPair()
{
Plugin = python,
Metadata = metadata
};
plugins.Add(pair);
}
return plugins;
}
}
}

View File

@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using WinAlfred.Plugin;
namespace WinAlfred.PluginLoader
{
public class PythonPluginWrapper : IPlugin
{
private static ScriptEngine engine;
private static ScriptScope scope;
private object pythonInstance;
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)
{
pythonInstance = GetPythonClassInstance(file, "winAlfred");
}
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);
results.Add(new Result()
{
Title = invokeMember.ToString()
});
return results;
}
public void Init()
{
}
}
}

View File

@ -31,10 +31,52 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="IronPython">
<HintPath>..\packages\IronPython.2.7.4\lib\Net35\IronPython.dll</HintPath>
</Reference>
<Reference Include="IronPython.Modules">
<HintPath>..\packages\IronPython.2.7.4\lib\Net35\IronPython.Modules.dll</HintPath>
</Reference>
<Reference Include="IronPython.SQLite">
<HintPath>..\packages\IronPython.2.7.4\lib\Net35\IronPython.SQLite.dll</HintPath>
</Reference>
<Reference Include="log4net">
<HintPath>..\packages\log4net.2.0.3\lib\net35-full\log4net.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Dynamic">
<HintPath>..\packages\IronPython.2.7.4\lib\Net35\Microsoft.Dynamic.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Scripting">
<HintPath>..\packages\IronPython.2.7.4\lib\Net35\Microsoft.Scripting.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Scripting.AspNet">
<HintPath>..\packages\IronPython.2.7.4\lib\Net35\Microsoft.Scripting.AspNet.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Scripting.Core">
<HintPath>..\packages\IronPython.2.7.4\lib\Net35\Microsoft.Scripting.Core.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Scripting.Metadata">
<HintPath>..\packages\IronPython.2.7.4\lib\Net35\Microsoft.Scripting.Metadata.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
@ -57,6 +99,7 @@
<Compile Include="PluginLoader\BasePluginLoader.cs" />
<Compile Include="Helper\IniParser.cs" />
<Compile Include="PluginLoader\PythonPluginLoader.cs" />
<Compile Include="PluginLoader\PythonPluginWrapper.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="FrmMain.resx">
@ -89,7 +132,6 @@
<Name>WinAlfred.Plugin</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View File

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="IronPython" version="2.7.4" targetFramework="net35" />
<package id="log4net" version="2.0.3" targetFramework="net35" />
</packages>