-
-
Save magigo/f9930fffd62f014d82d4 to your computer and use it in GitHub Desktop.
通过子类的方式实现插件
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
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/ |
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
class Base(object): | |
pass |
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
#!/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() |
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
from base import Base | |
class A(Base): | |
def sayHi(this): | |
print "Hi, this is class A!" |
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
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