Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #---------- test_a_plugin.py
- class Plugin:
- def __init__(self):
- print 'instantiating test_a_plugin.py.Plugin class'
- def run(self):
- print 'run() function for test_a'
- #---------- test_b_plugin.py
- class Plugin:
- def __init__(self):
- print 'instantiating test_b_plugin.py.Plugin class'
- def run(self):
- print 'run() function for test_b'
- #---------- mytest.py
- import os
- import os.path
- import fnmatch
- PLUGIN_GLOB = '*_plugin.py'
- def get_plugin_class_objects(plugin_dir):
- '''To use, place plugin files in the same directory as this script
- and name them so that they match the PLUGIN_GLOB.'''
- class_objects = []
- files = [f for f in os.listdir(os.getcwd()) if fnmatch.fnmatch(f, PLUGIN_GLOB)]
- print files
- for f in files:
- (base, ext) = os.path.splitext(os.path.basename(f))
- if ext != '.py':
- continue
- plugin_module = __import__(base)
- # Hard coded to always expect each module to contain a class named 'Plugin'
- class_objects.append(plugin_module.Plugin)
- return(class_objects)
- def main():
- plugin_class_objs = get_plugin_class_objects(PLUGIN_DIR)
- plugin_objs = [c() for c in plugin_class_objs]
- # do "something" with each plugin...
- for o in plugin_objs:
- o.run()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement