Change python plugin interface.

This commit is contained in:
qianlifeng
2014-02-23 10:36:37 +08:00
parent a0507bd14d
commit 26294a5742
5 changed files with 36 additions and 11 deletions

View File

@@ -27,7 +27,7 @@ def query(key):
pass pass
return json.dumps(results) return json.dumps(results)
def killProcess(pid): def killProcess(context,pid):
p = psutil.Process(int(pid)) p = psutil.Process(int(pid))
if p: if p:
p.kill() p.kill()

View File

@@ -27,7 +27,7 @@ def query(key):
results.append(res) results.append(res)
return json.dumps(results) return json.dumps(results)
def openUrl(url): def openUrl(context,url):
webbrowser.open(url) webbrowser.open(url)
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Wox.Helper
{
public class WoxPythonException : WoxException
{
public WoxPythonException(string msg) : base(msg)
{
}
}
}

View File

@@ -1,9 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Documents;
using Newtonsoft.Json; using Newtonsoft.Json;
using Python.Runtime; using Python.Runtime;
using Wox.Helper; using Wox.Helper;
@@ -13,7 +10,6 @@ namespace Wox.PluginLoader
{ {
public class PythonPluginWrapper : IPlugin public class PythonPluginWrapper : IPlugin
{ {
private PluginMetadata metadata; private PluginMetadata metadata;
private string moduleName; private string moduleName;
@@ -50,7 +46,7 @@ namespace Wox.PluginLoader
{ {
#if (DEBUG) #if (DEBUG)
{ {
throw; throw new WoxPythonException(e.Message);
} }
#endif #endif
Log.Error(string.Format("Python Plugin {0} query failed: {1}", metadata.Name, e.Message)); Log.Error(string.Format("Python Plugin {0} query failed: {1}", metadata.Name, e.Message));
@@ -82,16 +78,30 @@ namespace Wox.PluginLoader
if (module == null) if (module == null)
{ {
string error = string.Format("Python Invoke failed: {0} doesn't has module {1}", string error = string.Format("Python Invoke failed: {0} doesn't has module {1}",
metadata.ExecuteFilePath,moduleName); metadata.ExecuteFilePath, moduleName);
Log.Error(error); Log.Error(error);
return json; return json;
} }
if (module.HasAttr(func)) if (module.HasAttr(func))
{
try
{ {
PyObject res = paras.Length > 0 ? module.InvokeMethod(func, paras) : module.InvokeMethod(func); PyObject res = paras.Length > 0 ? module.InvokeMethod(func, paras) : module.InvokeMethod(func);
json = Runtime.GetManagedString(res.Handle); json = Runtime.GetManagedString(res.Handle);
} }
catch (Exception e)
{
string error = string.Format("Python Invoke failed: {0}", e.Message);
Log.Error(error);
#if (DEBUG)
{
throw new WoxPythonException(error);
}
#endif
}
}
else else
{ {
string error = string.Format("Python Invoke failed: {0} doesn't has function {1}", string error = string.Format("Python Invoke failed: {0} doesn't has function {1}",
@@ -99,7 +109,7 @@ namespace Wox.PluginLoader
Log.Error(error); Log.Error(error);
#if (DEBUG) #if (DEBUG)
{ {
throw new ArgumentException(error); throw new WoxPythonException(error);
} }
#endif #endif
} }

View File

@@ -132,6 +132,7 @@
<Compile Include="Helper\Log.cs" /> <Compile Include="Helper\Log.cs" />
<Compile Include="Helper\PluginInstaller.cs" /> <Compile Include="Helper\PluginInstaller.cs" />
<Compile Include="Helper\WoxException.cs" /> <Compile Include="Helper\WoxException.cs" />
<Compile Include="Helper\WoxPythonException.cs" />
<Compile Include="HotkeyControl.xaml.cs"> <Compile Include="HotkeyControl.xaml.cs">
<DependentUpon>HotkeyControl.xaml</DependentUpon> <DependentUpon>HotkeyControl.xaml</DependentUpon>
</Compile> </Compile>