Due to the requirements of my work, I needed to design an open software system, and immediately thought of the Python language. As a result, I designed a simple and easily extensible plugin system.
if choice == "1": plugin_name = input("Please input the name of plugin: ") manager.load_plugin(plugin_name)
elif choice == "2": manager.run_plugins()
elif choice == "3": break
Then, you create a directory named ‘plugins.’ In this directory, you can add your plugin, which must define a class named ‘MyPlugin’ and must have a function named ‘run’. Here is the simple code:
1 2 3 4 5 6 7
#my_plugin
classMyPlugin: def__init__(self): pass defrun(self): print("MyPlugin is running")
The following is the running process:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1. Add Plugin 2. Run Plugin 3. Quit Your Select: 1 Please input the name of plugin: my_plugin plugin my_plugin loaded. 1. Add Plugin 2. Run Plugin 3. Quit Your Select: 2 MyPlugin is running 1. Add Plugin 2. Run Plugin 3. Quit Your Select: 3 PS C:\Users\xxx\Desktop\yyy\plugin-system>
You can use this model to design a big and an opened software system.