Package doapfiend :: Package plugins :: Module base
[hide private]
[frames] | no frames]

Source Code for Module doapfiend.plugins.base

 1   
 2  # pylint: disable-msg=W0201,W0511 
 3  #XXX Attribute 'conf' defined outside __init__ 
 4   
 5  """ 
 6   
 7  Base plugin class 
 8  ================= 
 9   
10  All plugins should inherit doapfiend.plugins.Plugin 
11   
12  """ 
13   
14   
15  import textwrap 
16   
17 -class Plugin(object):
18 """Base class for doapfiend plugins. It's not necessary to subclass this 19 class to create a plugin; however, all plugins must implement 20 `add_options(self, parser)` and `configure(self, options, 21 conf)`, and must have the attributes `enabled` and `name`. 22 23 Plugins should not be enabled by default. 24 25 Subclassing Plugin will give your plugin some friendly default 26 behavior: 27 28 - A --with-$name option will be added to the command line 29 interface to enable the plugin. The plugin class's docstring 30 will be used as the help for this option. 31 - The plugin will not be enabled unless this option is selected by 32 the user. 33 """ 34 enabled = False 35 enable_opt = None 36 name = None 37
38 - def __init__(self):
39 self.conf = None 40 if self.name is None: 41 self.name = self.__class__.__name__.lower() 42 if self.enable_opt is None: 43 self.enable_opt = "enable_plugin_%s" % self.name
44
45 - def add_options(self, parser):
46 """Add command-line options for this plugin. 47 48 The base plugin class adds --with-$name by default, used to enable the 49 plugin. 50 """ 51 parser.add_option("--with-%s" % self.name, 52 action="store_true", 53 dest=self.enable_opt, 54 help="Enable plugin %s: %s" % 55 (self.__class__.__name__, self.help()) 56 )
57
58 - def configure(self, options, conf):
59 """Configure the plugin and system, based on selected options. 60 61 The base plugin class sets the plugin to enabled if the enable option 62 for the plugin (self.enable_opt) is true. 63 """ 64 self.conf = conf 65 self.options = options 66 if hasattr(options, self.enable_opt): 67 self.enabled = getattr(options, self.enable_opt)
68
69 - def help(self):
70 """Return help for this plugin. This will be output as the help 71 section of the --with-$name option that enables the plugin. 72 """ 73 if self.__class__.__doc__: 74 # doc sections are often indented; compress the spaces 75 return textwrap.dedent(self.__class__.__doc__) 76 return "(no help available)"
77