mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-15 19:27:56 +01:00
add IronPython environment to execute python plugins
This commit is contained in:
14
WinAlfred/FrmMain.Designer.cs
generated
14
WinAlfred/FrmMain.Designer.cs
generated
@@ -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;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<IPlugin> LoadPlugin()
|
||||
public override List<PluginPair> LoadPlugin()
|
||||
{
|
||||
List<PluginPair> plugins = new List<PluginPair>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
61
WinAlfred/PluginLoader/PythonPluginWrapper.cs
Normal file
61
WinAlfred/PluginLoader/PythonPluginWrapper.cs
Normal 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()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
@@ -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>
|
||||
Reference in New Issue
Block a user