[Yum-devel] single line config files for plugins
Michael E Brown
Michael_E_Brown at dell.com
Mon May 7 23:15:30 UTC 2007
Here is a *tested* patch that seems to do the job.
Plugins can remove the need for a default config file by setting up a
ConfigParser object in their module initialization code and naming it
'module_default_config'
sample your-plugin.py:
===========================
import ConfigParser as cp
module_default_config = cp.ConfigParser()
module_default_config.add_section("main")
module_default_config.set("main", "enabled", "0")
===========================
If the plugin does not have this code, then normal fallback to config
file happens.
Additionally, this means that modules can contain their own defaults for
any configuration parameters.
--
Michael
-------------- next part --------------
--- plugins.py-orig 2007-05-07 18:09:21.000000000 -0500
+++ ./plugins.py-meb 2007-05-07 18:09:13.000000000 -0500
@@ -176,18 +176,18 @@
dir, modname = os.path.split(modulefile)
modname = modname.split('.py')[0]
- conf = self._getpluginconf(modname)
- if not conf or not config.getOption(conf, 'main', 'enabled',
- config.BoolOption(False)):
- self.verbose_logger.debug('"%s" plugin is disabled', modname)
- return
-
fp, pathname, description = imp.find_module(modname, [dir])
try:
module = imp.load_module(modname, fp, pathname, description)
finally:
fp.close()
+ conf = self._getpluginconf(modname, module)
+ if not conf or not config.getOption(conf, 'main', 'enabled',
+ config.BoolOption(False)):
+ self.verbose_logger.debug('"%s" plugin is disabled', modname)
+ return
+
# Check API version required by the plugin
if not hasattr(module, 'requires_api_version'):
raise Errors.ConfigError(
@@ -234,29 +234,28 @@
(modname, getattr(module, funcname))
)
- def _getpluginconf(self, modname):
+ def _getpluginconf(self, modname, moduleobj):
'''Parse the plugin specific configuration file and return a
IncludingConfigParser instance representing it. Returns None if there
was an error reading or parsing the configuration file.
'''
+
+ parser = ConfigParser.ConfigParser()
+ if hasattr(moduleobj, "module_default_config"):
+ parser = moduleobj.module_default_config
+
+ confpp_obj = None
for dir in self.pluginconfpath:
- conffilename = os.path.join(dir, modname + ".conf")
+ conffilename= os.path.join(dir, modname + ".conf")
if os.access(conffilename, os.R_OK):
- # Found configuration file
- break
- self.verbose_logger.log(logginglevels.INFO_2, "Configuration file %s not found" % conffilename)
- else: # for
- # Configuration files for the plugin not found
- self.verbose_logger.log(logginglevels.INFO_2, "Unable to find configuration file for plugin %s"
- % modname)
- return None
- parser = ConfigParser.ConfigParser()
- confpp_obj = ConfigPreProcessor(conffilename)
- try:
- parser.readfp(confpp_obj)
- except ParsingError, e:
- raise Errors.ConfigError("Couldn't parse %s: %s" % (conffilename,
- str(e)))
+ try:
+ confpp_obj = ConfigPreProcessor(conffilename)
+ parser.readfp(confpp_obj)
+ except ParsingError, e:
+ raise Errors.ConfigError("Couldn't parse %s: %s" % (conffilename,
+ str(e)))
+ break
+
return parser
def setCmdLine(self, opts, commands):
More information about the Yum-devel
mailing list