mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-15 19:27:56 +01:00
add PyWinAlfred
This commit is contained in:
20
WinAlfred/App.config
Normal file
20
WinAlfred/App.config
Normal file
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<configSections>
|
||||
<section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
|
||||
</configSections>
|
||||
<log4net>
|
||||
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
|
||||
<file value="log.txt" />
|
||||
<appendToFile value="true" />
|
||||
<rollingStyle value="Date" />
|
||||
<datePattern value="yyyyMMdd-HH:mm:ss" />
|
||||
<layout type="log4net.Layout.PatternLayout">
|
||||
<conversionPattern value="Date:%date Level:%-5level Msg:%message%newline" />
|
||||
</layout>
|
||||
</appender>
|
||||
<root>
|
||||
<appender-ref ref="LogFileAppender" />
|
||||
</root>
|
||||
</log4net>
|
||||
</configuration>
|
||||
16
WinAlfred/Helper/WinAlfredException.cs
Normal file
16
WinAlfred/Helper/WinAlfredException.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace WinAlfred.Helper
|
||||
{
|
||||
public class WinAlfredException : Exception
|
||||
{
|
||||
public WinAlfredException(string msg)
|
||||
: base(msg)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -53,3 +53,4 @@ using System.Windows;
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
|
||||
@@ -36,33 +36,9 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</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.Data" />
|
||||
<Reference Include="System.Drawing" />
|
||||
@@ -83,6 +59,7 @@
|
||||
<Compile Include="Helper\IniParser.cs" />
|
||||
<Compile Include="Helper\KeyboardHook.cs" />
|
||||
<Compile Include="Helper\Log.cs" />
|
||||
<Compile Include="Helper\WinAlfredException.cs" />
|
||||
<Compile Include="PluginLoader\BasePluginLoader.cs" />
|
||||
<Compile Include="PluginLoader\CSharpPluginLoader.cs" />
|
||||
<Compile Include="PluginLoader\PythonPluginLoader.cs" />
|
||||
@@ -132,6 +109,7 @@
|
||||
<Generator>ResXFileCodeGenerator</Generator>
|
||||
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
||||
</EmbeddedResource>
|
||||
<None Include="App.config" />
|
||||
<None Include="packages.config" />
|
||||
<None Include="Properties\Settings.settings">
|
||||
<Generator>SettingsSingleFileGenerator</Generator>
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
<?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