plugins

Describe easmy/plugins here.

Plugins

Bazaar comes with a wonderful plugins system. The plugins when placed in .bazaar/plugins/ subdirectory, immediately become available to the user. What a fine way of enabling contribution from the community!

I am not a professional programmer, though I have programmed and taught programming for many years in many different programming languages. I was intrigued by the plugins system, though decided to do write a very simple program that would incorporate the capabilities of plugins. It is very short and I will give a listing of it RSN. But first I better learn something about the rules of this wiki.

   1 #!/usr/bin/env python
   2 #!/usr/bin/env python
   3 ''' fooboo.py - mainline of trial of dummy plugins
   4 The aim is to try to imitate the plugin system of Bazaar, where a plugin placed in
   5 ...plugins/foo/ directory is recognised as "foo".
   6 Plugin "foo" simply says "Hallo from Foo". Similarly, boo says "hallo from Boo".
   7 '''
   8 import os
   9 
  10 def prolog():
  11     'Return list of subdirectories in plugins directory.'   
  12     temp = os.listdir(os.getcwd() + '/plugins')
  13     return [v for v in temp if not '.' in v] 
  14     
  15 def import_plugins(listdirs):
  16     '''Import all available modules and their packages which are in 
  17     plugins/ directory'''
  18     module = {}
  19     for v in listdirs:
  20         pak = 'plugins.' + v + '.' + v + '_mod'
  21         print 'pak = ', pak
  22         # sample pak = plugins.foo.foo_mod
  23         try:
  24             plug = __import__(pak)
  25             # This imports foo.mod, foo and plugins. plugins are assigned to plug.
  26             module[v] = plug
  27         except:
  28             print 'Failed to import ', v
  29     return module
  30 
  31 def main():
  32     'Do the main coordinating job.'
  33     listdirs = prolog()
  34     print listdirs
  35     module = import_plugins(listdirs)
  36     print module
  37     foo_inst = module['foo'].foo.foo_mod.Foo()
  38     foo_inst.sayhi()
  39     print 'Hey, Boo, speak to me too!'
  40     boo_inst = module['boo'].boo.boo_mod.Boo()
  41     boo_inst.sayhi()
  42     
  43 if __name__ == '__main__':
  44     'Just call the main function.'
  45     main()

Suppose this "mainline" is in a directory foo_boo on your PC. Directly under foo_boo is a plugins directory. In it is an empty init.py file. That makes plugins a python package. Directly under plugins are foo and boo directories. Each has an empty init.py to make them sub-packages. In each there is a module - foo_mod.py and boo_mod.py:

This simple plugins system enables addition of plugins which will be recognised by the calling program as soon as they are placed in a plugins subdirectory. Some fairly simple requirements: the module that can be called from the main program is expected to be of the name <plugin_directory>_mod. The <plugin directory> must be a simple character string without any dots. Try to add a moo plugin with moo_mod module and perhaps the same class and procedure name as other plugins. Also, try to get init.py to respond to invocation of the package. It is activated at the time the package is loaded, If you wish, make it say something in a print statement,

   1 #!/usr/bin/env python
   2 ''' foo_mod.py - module.'''
   3 
   4 class Foo:
   5     def __init__(self):
   6         return
   7     
   8     def sayhi(sel):
   9         print 'Hi from Mr Foo.'

and

   1 #!/usr/bin/env python
   2 ''' boo_mod.py - module.'''
   3 
   4 class Boo:
   5     def __init__(self):
   6         return
   7     
   8     def sayhi(self):
   9         print 'I am Mr. Boo - boo to you too.'

Try it and watch the "conversation".

return to easmy

easmy/plugins (last edited 2009-10-26 13:56:16 by ta-1-28)