Skip to content

Instantly share code, notes, and snippets.

@meonkeys
Created February 27, 2011 07:52
Show Gist options
  • Save meonkeys/845993 to your computer and use it in GitHub Desktop.
Save meonkeys/845993 to your computer and use it in GitHub Desktop.
main program
Plugin architecture based on "sub-class discovery method" described in a talk
by Dr. André Roberge. The talk is called "Plugins and monkeypatching:
increasing flexibility, dealing with inflexibility".
Here's the talk recorded at PyCon 2009 in Chicago:
http://us.pycon.org/2009/conference/schedule/event/47/
Plugin writer HOWTO:
1. subclass "Base"
2. implement sayHi()
3. place your plugin (the .py file) in this directory
4. run main.py
Note: written using python 2.6.5 on Ubuntu 10.04
See also:
* http://washort.twistedmatrix.com/2011/01/introducing-exocet.html / https://launchpad.net/exocet (recommended by dash/Allen Short in #python)
* http://stackoverflow.com/questions/932069/building-a-minimal-plugin-architecture-in-python
* especially http://stackoverflow.com/questions/932069/building-a-minimal-plugin-architecture-in-python/933245#933245
* http://yapsy.sf.net/
class Base(object):
pass
#!/usr/bin/python -tt
from base import Base
import os
plugins = []
def init_plugins():
plugin_files = [x[:-3] for x in os.listdir('.') if x.endswith('.py')]
for plugin in plugin_files:
dummy = __import__(plugin)
for plugin in Base.__subclasses__():
plugins.append(plugin)
def run_plugins():
for plugin in plugins:
x = plugin()
x.sayHi()
if __name__ == '__main__':
init_plugins()
run_plugins()
from base import Base
class A(Base):
def sayHi(this):
print "Hi, this is class A!"
from base import Base
class B(Base):
def sayHi(this):
print "Hi, this is class B!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment