try to use python.net as the bridge.

This commit is contained in:
qianlifeng
2014-01-11 00:19:14 +08:00
parent 935d26f956
commit 76009ca6eb
78 changed files with 14592 additions and 81 deletions

View File

@@ -17,4 +17,4 @@
<appender-ref ref="LogFileAppender"/>
</root>
</log4net>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
<startup><supportedRuntime version="v2.0.50727"/></startup></configuration>

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
@@ -9,12 +10,14 @@ using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Win32;
using Python.Runtime;
using WinAlfred.Commands;
using WinAlfred.Helper;
using WinAlfred.Plugin;
using WinAlfred.PluginLoader;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using MessageBox = System.Windows.MessageBox;
using Python;
using Timer = System.Threading.Timer;
namespace WinAlfred
@@ -37,6 +40,12 @@ namespace WinAlfred
resultCtrl.resultItemChangedEvent += resultCtrl_resultItemChangedEvent;
ThreadPool.SetMaxThreads(30, 10);
InitProgressbarAnimation();
Closed += MainWindow_Closed;
}
void MainWindow_Closed(object sender, EventArgs e)
{
PythonEngine.Shutdown();
}
private void InitProgressbarAnimation()
@@ -180,6 +189,8 @@ namespace WinAlfred
//MessageBox.Show(engine.Evaluate("5 * 10 + 2").ToString());
}
private void TbQuery_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)

View File

@@ -19,16 +19,17 @@ namespace WinAlfred.PluginLoader
foreach (IPlugin plugin in plugins.Select(pluginPair => pluginPair.Plugin))
{
IPlugin plugin1 = plugin;
//ThreadPool.QueueUserWorkItem(o =>);
plugin1.Init(new PluginInitContext()
ThreadPool.QueueUserWorkItem(o =>
{
Plugins = plugins,
ChangeQuery = s => window.ChangeQuery(s),
CloseApp = window.CloseApp,
HideApp = window.HideApp,
ShowApp = window.ShowApp,
ShowMsg = (title, subTitle, iconPath) => window.ShowMsg(title, subTitle, iconPath)
plugin1.Init(new PluginInitContext()
{
Plugins = plugins,
ChangeQuery = s => window.ChangeQuery(s),
CloseApp = window.CloseApp,
HideApp = window.HideApp,
ShowApp = window.ShowApp,
ShowMsg = (title, subTitle, iconPath) => window.ShowMsg(title, subTitle, iconPath)
});
});
}
}

View File

@@ -1,12 +1,20 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Python.Runtime;
using WinAlfred.Plugin;
namespace WinAlfred.PluginLoader
{
public class PythonPluginLoader : BasePluginLoader
{
static PythonPluginLoader()
{
PythonEngine.Initialize();
PythonEngine.BeginAllowThreads();
}
public override List<PluginPair> LoadPlugin()
{
List<PluginPair> plugins = new List<PluginPair>();

View File

@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Documents;
using Newtonsoft.Json;
using Python.Runtime;
using WinAlfred.Plugin;
namespace WinAlfred.PluginLoader
@@ -10,11 +12,6 @@ namespace WinAlfred.PluginLoader
{
private PluginMetadata metadata;
[DllImport("PyWinAlfred.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private extern static IntPtr ExecPython(string directory, string file, string method, string para);
[DllImport("PyWinAlfred.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private extern static void InitPythonEnv();
public PythonPluginWrapper(PluginMetadata metadata)
{
this.metadata = metadata;
@@ -24,16 +21,12 @@ namespace WinAlfred.PluginLoader
{
try
{
string s = Marshal.PtrToStringAnsi(ExecPython(metadata.PluginDirecotry, metadata.ExecuteFileName.Replace(".py", ""), "query", query.RawQuery));
string s = InvokeFunc(metadata.PluginDirecotry, metadata.ExecuteFileName.Replace(".py", ""),"query",query.RawQuery);
if (string.IsNullOrEmpty(s))
{
return new List<Result>();
}
if (s.StartsWith("PYTHONERROR"))
{
throw new ArgumentException(s);
}
List<PythonResult> o = JsonConvert.DeserializeObject<List<PythonResult>>(s);
List<Result> r = new List<Result>();
foreach (PythonResult pythonResult in o)
@@ -41,7 +34,7 @@ namespace WinAlfred.PluginLoader
PythonResult ps = pythonResult;
if (!string.IsNullOrEmpty(ps.ActionName))
{
ps.Action = () => ExecPython(metadata.PluginDirecotry, metadata.ExecuteFileName.Replace(".py", ""), ps.ActionName, ps.ActionPara);
ps.Action = () => InvokeFunc(metadata.PluginDirecotry, metadata.ExecuteFileName.Replace(".py", ""), ps.ActionName, ps.ActionPara);
}
r.Add(ps);
}
@@ -55,9 +48,27 @@ namespace WinAlfred.PluginLoader
}
private string InvokeFunc(string path, string moduleName,string func, string para)
{
IntPtr gs = PythonEngine.AcquireLock();
IntPtr pyStrPtr = Runtime.PyString_FromString(path);
IntPtr SysDotPath = Runtime.PySys_GetObject("path");
Runtime.PyList_Append(SysDotPath, pyStrPtr);
PyObject module = PythonEngine.ImportModule(moduleName);
module = PythonEngine.ReloadModule(module);
PyObject res = module.InvokeMethod(func, new PyString(para));
string json = Runtime.GetManagedString(res.Handle);
PythonEngine.ReleaseLock(gs);
return json;
}
public void Init(PluginInitContext context)
{
InitPythonEnv();
}
}
}

View File

@@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>WinAlfred</RootNamespace>
<AssemblyName>WinAlfred</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
@@ -26,6 +26,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -167,6 +168,10 @@
<AppDesigner Include="Properties\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Pythonnet.Runtime\Python.Runtime.csproj">
<Project>{097b4ac0-74e9-4c58-bcf8-c69746ec8271}</Project>
<Name>Python.Runtime</Name>
</ProjectReference>
<ProjectReference Include="..\WinAlfred.Plugin.System\WinAlfred.Plugin.System.csproj">
<Project>{69ce0206-cb41-453d-88af-df86092ef9b8}</Project>
<Name>WinAlfred.Plugin.System</Name>

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="log4net" version="2.0.3" targetFramework="net35" requireReinstallation="True" />
<package id="Newtonsoft.Json" version="5.0.8" targetFramework="net35" requireReinstallation="True" />
<package id="log4net" version="2.0.3" targetFramework="net35" />
<package id="Newtonsoft.Json" version="5.0.8" targetFramework="net35" />
</packages>