mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 11:16:51 +02:00
add IronPython environment to execute python plugins
This commit is contained in:
@@ -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()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user