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. {{{#!python #!/usr/bin/env python #!/usr/bin/env python ''' fooboo.py - mainline of trial of dummy plugins The aim is to try to imitate the plugin system of Bazaar, where a plugin placed in ...plugins/foo/ directory is recognised as "foo". Plugin "foo" simply says "Hallo from Foo". Similarly, boo says "hallo from Boo". ''' import os def prolog(): 'Return list of subdirectories in plugins directory.' temp = os.listdir(os.getcwd() + '/plugins') return [v for v in temp if not '.' in v] def import_plugins(listdirs): '''Import all available modules and their packages which are in plugins/ directory''' module = {} for v in listdirs: pak = 'plugins.' + v + '.' + v + '_mod' print 'pak = ', pak # sample pak = plugins.foo.foo_mod try: plug = __import__(pak) # This imports foo.mod, foo and plugins. plugins are assigned to plug. module[v] = plug except: print 'Failed to import ', v return module def main(): 'Do the main coordinating job.' listdirs = prolog() print listdirs module = import_plugins(listdirs) print module foo_inst = module['foo'].foo.foo_mod.Foo() foo_inst.sayhi() print 'Hey, Boo, speak to me too!' boo_inst = module['boo'].boo.boo_mod.Boo() boo_inst.sayhi() if __name__ == '__main__': 'Just call the main function.' 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 _mod. The 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, {{{#!python #!/usr/bin/env python ''' foo_mod.py - module.''' class Foo: def __init__(self): return def sayhi(sel): print 'Hi from Mr Foo.' }}} and {{{#!python #!/usr/bin/env python ''' boo_mod.py - module.''' class Boo: def __init__(self): return def sayhi(self): print 'I am Mr. Boo - boo to you too.' }}} Try it and watch the "conversation". return to [[easmy]]