Last active
May 23, 2024 18:23
-
-
Save Jerakin/2f37085ade1e82cecf707dc778dda178 to your computer and use it in GitHub Desktop.
Simple example of a dynamic plugin system.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
In my opinion the strenght of a system like this lies in that you can easily | |
provide additional interfaces in later versions which can be accessed to | |
users using the new version, however old versions can still run with under | |
same plugin system. | |
License - MIT | |
""" | |
def my_plugin(interface_api, interface_config): | |
print(interface_api, interface_config) | |
# interface_api interface_config | |
def my_plugin_2(interface_config, interface_api): | |
print(interface_api, interface_config) | |
# interface_api interface_config | |
def my_plugin_3(interface_config): | |
print(interface_config) | |
# interface_config | |
class InterfaceAPI: ... | |
class InterfaceConfig: ... | |
class PluginManger: | |
def __init__(self, api: InterfaceAPI, config: InterfaceConfig): | |
self.plugins = [] | |
self.interface_map = { | |
"interface_api": api, | |
"interface_config": config | |
} | |
def load(self, plugin): | |
"""Loads the plugin and supplies any asked for interfaces.""" | |
interfaces = plugin.__code__.co_varnames | |
plugin(*{name: self.interface_map[name] for name in interfaces if name in self.interface_map}) | |
pm = PluginManger(InterfaceAPI(), InterfaceConfig()) | |
pm.load(my_plugin) | |
pm.load(my_plugin_2) | |
pm.load(my_plugin_3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment