Advertisement
Guest User

Simple Python plugin snippet

a guest
Jul 28th, 2010
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 0 0
  1. #---------- test_a_plugin.py
  2. class Plugin:
  3.     def __init__(self):
  4.         print 'instantiating test_a_plugin.py.Plugin class'
  5.     def run(self):
  6.         print 'run() function for test_a'
  7.  
  8.  
  9. #---------- test_b_plugin.py
  10. class Plugin:
  11.     def __init__(self):
  12.         print 'instantiating test_b_plugin.py.Plugin class'
  13.     def run(self):
  14.         print 'run() function for test_b'
  15.  
  16.  
  17. #---------- mytest.py
  18. import os
  19. import os.path
  20. import fnmatch
  21. PLUGIN_GLOB = '*_plugin.py'
  22.  
  23. def get_plugin_class_objects(plugin_dir):
  24.     '''To use, place plugin files in the same directory as this script
  25.    and name them so that they match the PLUGIN_GLOB.'''
  26.     class_objects = []
  27.     files = [f for f in os.listdir(os.getcwd()) if fnmatch.fnmatch(f, PLUGIN_GLOB)]
  28.     print files
  29.     for f in files:
  30.         (base, ext) = os.path.splitext(os.path.basename(f))
  31.         if ext != '.py':
  32.             continue
  33.         plugin_module = __import__(base)
  34.         # Hard coded to always expect each module to contain a class named 'Plugin'
  35.         class_objects.append(plugin_module.Plugin)
  36.     return(class_objects)
  37.    
  38. def main():
  39.     plugin_class_objs = get_plugin_class_objects(PLUGIN_DIR)
  40.     plugin_objs = [c() for c in plugin_class_objs]
  41.     # do "something" with each plugin...
  42.     for o in plugin_objs:
  43.         o.run()
  44.  
  45. if __name__ == '__main__':
  46.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement