Skip to content

Instantly share code, notes, and snippets.

@h0rn3t
Forked from BradWhittington/plugin_module.py
Created July 9, 2020 06:38
Show Gist options
  • Save h0rn3t/0dcd72ea5c71a516dd9dda06748808b7 to your computer and use it in GitHub Desktop.
Save h0rn3t/0dcd72ea5c71a516dd9dda06748808b7 to your computer and use it in GitHub Desktop.
Plugin pattern for python classes
plugins = {}
def get_input_plugins():
return plugins['input'].items()
class Plugin(object):
plugin_class = None
@classmethod
def register(cls, name):
plugins[cls.plugin_class][name] = cls
class InputPlugin(Plugin):
plugin_class = 'input'
def process_input(self, something):
raise NotImplementedError
class ExamplePlugin(InputPlugin):
def process_input(self, something):
return str(something)
ExamplePlugin.register('example')
@h0rn3t
Copy link
Author

h0rn3t commented Jul 9, 2020

ExamplePlugin.register('example')

Why not just use cls.name as name argument? Then we just call ExamplePlugin.register()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment