简体   繁体   中英

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.

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.

Plugin installation should just be a matter of copying the plugin script file into the ./plugins directory relative to the main executable.

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).

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?

    b. C++ on Windows?

    c. C/Python from Linux/BSD?

  2. What's the simplest way to implement plugins from my common controller to execute custom plugins?

  1. I am with fontanini here; use shared libraries (DLLs) for the controller logic. 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).

    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.

  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.

    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. 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)
    • SIP (in particular in conjunction with [Py]Qt, for which it was developed)
    • Swig (which is suitable for multiple scripting languages, but leads to APIs that rather mirror the C APIs being wrapped instead of being "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)
  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. 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". 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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