简体   繁体   English

使用Python插件可扩展性编写跨平台应用程序的最简单方法?

[英]Simplest way to write cross-platform application with Python plugin extensibility?

I am writing an application which should be able to run on Linux, Mac OS X, Windows and BSD (not necessarily as a single executable, so it doesn't have to be Java) and be extensible using simple plugins. 我正在编写一个应该能够在Linux,Mac OS X,Windows和BSD上运行的应用程序(不一定是单个可执行文件,因此它不必是Java)并且可以使用简单的插件进行扩展。

The way I want my plugins to be implemented is as a simple Python program which must implement a certain function and simply return a dictionary to the main program. 我希望我的插件实现的方式是一个简单的Python程序,它必须实现某个函数并简单地将字典返回到主程序。

Plugin installation should just be a matter of copying the plugin script file into the ./plugins directory relative to the main executable. 插件安装应该只是将插件脚本文件复制到相对于主可执行文件的./plugins目录中。

The main program should be a stand-alone executable with shared code amongst all of the above platforms, but with platform specific front-ends (so the Linux and BSD versions would just be CLI tools, the Windows version have C++ and MFC front-end, and the Mac OS X version would have a Objecive-C and Cocoa front-end). 主程序应该是一个独立的可执行文件,在所有上述平台中都有共享代码,但是具有特定于平台的前端(因此Linux和BSD版本只是CLI工具,Windows版本具有C ++和MFC前端) ,Mac OS X版本将有一个Objecive-C和Cocoa前端)。

So I guess it's really two questions: 所以我想这真的有两个问题:

  1. What's the simplest way to share common controller code between multiple front ends from: 在多个前端之间共享通用控制器代码的最简单方法是:

    a. 一种。 Objective-C on a Mac? Mac上的Objective-C?

    b. C++ on Windows? Windows上的C ++?

    c. C。 C/Python from Linux/BSD? Linux / BSD的C / Python?

  2. What's the simplest way to implement plugins from my common controller to execute custom plugins? 从我的通用控制器实现插件以执行自定义插件的最简单方法是什么?

  1. I am with fontanini here; 我在这里和fontanini在一起; use shared libraries (DLLs) for the controller logic. 使用共享库(DLL)作为控制器逻辑。 Preferably, use C/C++ for that, and be careful with RTTI (needed for dynamic_cast<> and exception handling), which may not work across DLL borders (eg you might have problems catching exceptions thrown in one DLL from another one). 最好使用C / C ++,并注意RTTI(dynamic_cast <>和异常处理所需),这可能不适用于DLL边界(例如,您可能无法捕获从另一个DLL中抛出的异常)。

    Look for good cross-platform libraries like Qt, which offer a lot of functionality (eg filesystem, processes, networking – not just GUIs, which you want to develop separately anyhow) in a platform-agnostic way. 寻找像Qt这样的优秀跨平台库,它们以平台无关的方式提供许多功能(例如文件系统,进程,网络 - 而不仅仅是GUI,你想以任何方式单独开发)。

  2. The Python/C API is the basis for making C/C++ functionality available to Python (and vice versa), and there is only little difference between extension modules and programs that offer their own functionality to an embedded Python interpreter. Python / C API是使Python可以使用C / C ++功能的基础(反之亦然),扩展模块和程序之间的差别很小,它们为嵌入式Python解释器提供了自己的功能。

    However, you might want to use a wrapper generator (all of which are based on the Python API, but require less code than using it directly) that makes your life easier. 但是,您可能希望使用包装器生成器(所有这些都基于Python API,但需要的代码少于直接使用它),这使您的生活更轻松。 Examples are: 例如:

    • boost::python (which is very convenient and powerful, but has an incomprehensible hardcore-C++ implementation ;-), and leads to larger object code due to excessive template usage), possibly using pyplusplus to generate the boost::python wrapper code directly from your header files (not sacrificing the possibility to tweak the result, eg excluding or modifying function signatures) boost :: python (非常方便和强大,但有一个难以理解的hardcore-C ++实现;-),并且由于过多的模板使用而导致更大的目标代码),可能使用pyplusplus直接生成boost :: python包装器代码来自您的头文件(不会牺牲调整结果的可能性,例如排除或修改功能签名)
    • SIP (in particular in conjunction with [Py]Qt, for which it was developed) SIP (特别是与[Py] Qt一起开发)
    • Swig (which is suitable for multiple scripting languages, but leads to APIs that rather mirror the C APIs being wrapped instead of being "pythonic") Swig (适用于多种脚本语言,但会导致API反映C包被包装而不是“pythonic”)
    • PyBindGen which is based on the same GCCXML backend as pyplusplus, but generates pure Python/C API code directly, leading to leaner bindings (but may not grok all code out of the box) PyBindGen基于与pyplusplus相同的GCCXML后端,但直接生成纯Python / C API代码,导致更精简的绑定(但可能没有开箱即用的所有代码)
  1. The simplest way to share the cross-platform Python component of your application would probably be to implement it as a command-line program, and then invoke it using the relevant system calls in each of the front-ends. 共享应用程序的跨平台Python组件的最简单方法可能是将其作为命令行程序实现,然后使用每个前端中的相关系统调用来调用它。 It's not the most robust way, but it could be sufficient. 它不是最强大的方式,但它可能就足够了。

  2. If you want plugins to just be a file containing Python code, I would recommend that they at least conform to a convention, eg by extending a class, and then have your code load them into the Python runtime using "import plugin_name". 如果你希望插件只是一个包含Python代码的文件,我建议它们至少符合约定,例如通过扩展一个类,然后让你的代码使用“import plugin_name”将它们加载到Python运行时。 This would be better than having the plugins exist as separate programs because you would be able to access the output as Python types, rather than needing to parse text from standard input. 这比将插件作为单独的程序存在更好,因为您可以以Python类型访问输出,而不需要从标准输入中解析文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM