mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 11:16:51 +02:00
try to use python.net as the bridge.
This commit is contained in:
@@ -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)
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>();
|
||||
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user