I am writing a Flask application that supports plugin architecture. Each plugin lives in a separate folder and is a module that has at least one class that subclasses the Plugin class. For security reasons, I do not want to download all plugins when the flash drive application is initially launched. Instead, the user can enable plugins from the flash application. As soon as he does this, we save the note in the database, which adds a whitelist to the application for download. However, we should still remember which plugins are disabled and the proven views for these plugins. I do this by creating a dummy class for plugins that are not included that do not load any custom code.
Each plugin has its own Blueprint. We register this when loading plugins. Blueprint defines the route to enable the plugin. It all looks like this:
for plugin_name in os.listdir(plugin_dir): plugin_path = os.path.join(plugin_paths, plugin_name) module_name = "plugins.{}.__init__".format(plugin_name) plugin_enabled = ask_db_whether_plugin_is_enabled(plugin_name) if os.path.isdir(plugin_path) and plugin_enabled: module = __import__(module_name) for plugin in load_plugins_from_module(module): app.register_blueprint(plugin.blueprint, url_prefix='/plugins') else: PluginCls = type(identifier, (Plugin, ), {}) disabled_plugin = PluginCls() app.register_blueprint(disabled_plugin.blueprint, url_prefix='/plugins')
load_plugins_from_module as follows:
def load_plugins_from_module(module): def is_plugin(c): return inspect.isclass(c) and \ issubclass(c, Plugin) and \ c != Plugin for name, objects in inspect.getmembers(module, lambda c: inspect.ismodule(c)): for name, PluginCls in inspect.getmembers(objects, is_plugin): plugin = PluginCls() yield plugin
Now the question is: when I change the plugin to on, I basically want to restart
module = __import__(module_name) for plugin in load_plugins_from_module(module): app.register_blueprint(plugin.blueprint, url_prefix='/plugins')
for this plug-in module so that it becomes active and registers all routes that were defined in the subclass plug-in. This will increase the AssertionError because I cannot change the drawings at runtime. What would be good for this? Can I reload the application from the application? Can I modify an existing project at runtime?
Thank you for your help!