From fffb97ea8db0dd0430770f57c27fee9a31269cc4 Mon Sep 17 00:00:00 2001 From: qianlifeng Date: Mon, 23 Dec 2013 19:21:51 +0800 Subject: [PATCH] add PyWinAlfred --- PyWinAlfred/Main.cpp | 47 ++++++ PyWinAlfred/PyWinAlfred.vcxproj | 155 ++++++++++++++++++ PyWinAlfred/PyWinAlfred.vcxproj.filters | 22 +++ WinAlfred/App.config | 20 +++ WinAlfred/Helper/WinAlfredException.cs | 16 ++ WinAlfred/PluginLoader/BasePluginLoader.cs | 23 ++- WinAlfred/PluginLoader/PythonPluginLoader.cs | 2 +- WinAlfred/PluginLoader/PythonPluginWrapper.cs | 34 +--- WinAlfred/Properties/AssemblyInfo.cs | 1 + WinAlfred/WinAlfred.csproj | 26 +-- WinAlfred/packages.config | 1 - 11 files changed, 293 insertions(+), 54 deletions(-) create mode 100644 PyWinAlfred/Main.cpp create mode 100644 PyWinAlfred/PyWinAlfred.vcxproj create mode 100644 PyWinAlfred/PyWinAlfred.vcxproj.filters create mode 100644 WinAlfred/App.config create mode 100644 WinAlfred/Helper/WinAlfredException.cs diff --git a/PyWinAlfred/Main.cpp b/PyWinAlfred/Main.cpp new file mode 100644 index 0000000000..fbbe45c01d --- /dev/null +++ b/PyWinAlfred/Main.cpp @@ -0,0 +1,47 @@ +#include +#include "Python.h" + +extern "C" __declspec(dllexport) void ExecPython(char* directory, char* file, char* query) +{ + try{ + PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pClass, *pInstance ; + + // Initialize the Python Interpreter + Py_Initialize(); + + // Build the name object + PyObject *sys = PyImport_ImportModule("sys"); + PyObject *path = PyObject_GetAttrString(sys, "path"); + PyList_Append(path, PyString_FromString(directory)); + + pName = PyString_FromString(file); + + // Load the module object + pModule = PyImport_Import(pName); + + // pDict is a borrowed reference + pDict = PyModule_GetDict(pModule); + + // pFunc is also a borrowed reference + pClass = PyDict_GetItemString(pDict,"PyWinAlfred"); + + if (PyCallable_Check(pClass)) + { + pInstance = PyObject_CallObject(pClass, NULL); + } + else + { + PyErr_Print(); + return; + } + + // Call a method of the class with two parameters + pValue = PyObject_CallMethod(pInstance,"query", "(s)",query); + + // Finish the Python Interpreter + Py_Finalize(); + } + catch(int& value){ + PyErr_Print(); + } +} \ No newline at end of file diff --git a/PyWinAlfred/PyWinAlfred.vcxproj b/PyWinAlfred/PyWinAlfred.vcxproj new file mode 100644 index 0000000000..378502315a --- /dev/null +++ b/PyWinAlfred/PyWinAlfred.vcxproj @@ -0,0 +1,155 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {D03FD663-38A8-4C1A-8431-EB44F93E7EBA} + Win32Proj + PyWinAlfred + + + + DynamicLibrary + true + v110 + Unicode + + + DynamicLibrary + true + v110 + MultiByte + + + DynamicLibrary + false + v110 + true + Unicode + + + DynamicLibrary + false + v110 + true + Unicode + + + + + + + + + + + + + + + + + + + true + C:\Python27\include;$(IncludePath) + C:\Python27\libs;$(LibraryPath) + + + true + C:\Python27\include;$(IncludePath) + C:\Python27\libs;$(LibraryPath) + + + false + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_WINDOWS;_USRDLL;PYWINALFRED_EXPORTS;%(PreprocessorDefinitions) + true + + + Windows + true + + + + + + + Level3 + Disabled + WIN32;_DEBUG;_WINDOWS;_USRDLL;PYWINALFRED_EXPORTS;%(PreprocessorDefinitions) + true + + + Windows + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_WINDOWS;_USRDLL;PYWINALFRED_EXPORTS;%(PreprocessorDefinitions) + true + + + Windows + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_WINDOWS;_USRDLL;PYWINALFRED_EXPORTS;%(PreprocessorDefinitions) + true + + + Windows + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/PyWinAlfred/PyWinAlfred.vcxproj.filters b/PyWinAlfred/PyWinAlfred.vcxproj.filters new file mode 100644 index 0000000000..3b076d1fc7 --- /dev/null +++ b/PyWinAlfred/PyWinAlfred.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/WinAlfred/App.config b/WinAlfred/App.config new file mode 100644 index 0000000000..0383125086 --- /dev/null +++ b/WinAlfred/App.config @@ -0,0 +1,20 @@ + + + +
+ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/WinAlfred/Helper/WinAlfredException.cs b/WinAlfred/Helper/WinAlfredException.cs new file mode 100644 index 0000000000..4fcd5c3574 --- /dev/null +++ b/WinAlfred/Helper/WinAlfredException.cs @@ -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) + { + + } + } +} diff --git a/WinAlfred/PluginLoader/BasePluginLoader.cs b/WinAlfred/PluginLoader/BasePluginLoader.cs index 4b379e5488..dd03940731 100644 --- a/WinAlfred/PluginLoader/BasePluginLoader.cs +++ b/WinAlfred/PluginLoader/BasePluginLoader.cs @@ -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; } } diff --git a/WinAlfred/PluginLoader/PythonPluginLoader.cs b/WinAlfred/PluginLoader/PythonPluginLoader.cs index 6f0aa83db1..1de183603a 100644 --- a/WinAlfred/PluginLoader/PythonPluginLoader.cs +++ b/WinAlfred/PluginLoader/PythonPluginLoader.cs @@ -13,7 +13,7 @@ namespace WinAlfred.PluginLoader List 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, diff --git a/WinAlfred/PluginLoader/PythonPluginWrapper.cs b/WinAlfred/PluginLoader/PythonPluginWrapper.cs index 37c0bcaeef..4d9ff8bef8 100644 --- a/WinAlfred/PluginLoader/PythonPluginWrapper.cs +++ b/WinAlfred/PluginLoader/PythonPluginWrapper.cs @@ -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 Query(Query query) { List results = new List(); - 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; } diff --git a/WinAlfred/Properties/AssemblyInfo.cs b/WinAlfred/Properties/AssemblyInfo.cs index 6f65353e52..0e09fcd589 100644 --- a/WinAlfred/Properties/AssemblyInfo.cs +++ b/WinAlfred/Properties/AssemblyInfo.cs @@ -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)] \ No newline at end of file diff --git a/WinAlfred/WinAlfred.csproj b/WinAlfred/WinAlfred.csproj index 541734e7ec..d3f4c70f67 100644 --- a/WinAlfred/WinAlfred.csproj +++ b/WinAlfred/WinAlfred.csproj @@ -36,33 +36,9 @@ 4 - - ..\packages\IronPython.2.7.4\lib\Net35\IronPython.dll - - - ..\packages\IronPython.2.7.4\lib\Net35\IronPython.Modules.dll - - - ..\packages\IronPython.2.7.4\lib\Net35\IronPython.SQLite.dll - ..\packages\log4net.2.0.3\lib\net35-full\log4net.dll - - ..\packages\IronPython.2.7.4\lib\Net35\Microsoft.Dynamic.dll - - - ..\packages\IronPython.2.7.4\lib\Net35\Microsoft.Scripting.dll - - - ..\packages\IronPython.2.7.4\lib\Net35\Microsoft.Scripting.AspNet.dll - - - ..\packages\IronPython.2.7.4\lib\Net35\Microsoft.Scripting.Core.dll - - - ..\packages\IronPython.2.7.4\lib\Net35\Microsoft.Scripting.Metadata.dll - @@ -83,6 +59,7 @@ + @@ -132,6 +109,7 @@ ResXFileCodeGenerator Resources.Designer.cs + SettingsSingleFileGenerator diff --git a/WinAlfred/packages.config b/WinAlfred/packages.config index c178c30684..7eb67aa24b 100644 --- a/WinAlfred/packages.config +++ b/WinAlfred/packages.config @@ -1,5 +1,4 @@  - \ No newline at end of file