Allow the plugins to use a default configuration too

through overloading the class variable default_config.
also fix a bug that would add meaningless sections to plugin
configurations.
This commit is contained in:
mathieui 2014-12-10 23:12:17 +01:00
parent 4c7a470dc8
commit 003c28e953
No known key found for this signature in database
GPG key ID: C59F84CEEFD616E3
3 changed files with 21 additions and 9 deletions

View file

@ -153,9 +153,11 @@ class Config(RawConfigParser):
except TypeError: # python < 3.2 sucks
RawConfigParser.read(self, self.file_name)
# Check config integrity and fix it if its wrong
for section in ('bindings', 'var'):
if not self.has_section(section):
self.add_section(section)
# only when the object is the main config
if self.__class__ is Config:
for section in ('bindings', 'var'):
if not self.has_section(section):
self.add_section(section)
def get(self, option, default=None, section=DEFSECTION):
"""

View file

@ -305,7 +305,7 @@ def completion_set(self, the_input):
end_list = ['%s|%s' % (plugin_name, section) for section in plugin.config.sections()]
else:
end_list = set(config.options('Poezio'))
end_list = end_list.union(set(config.default.get('Poezio', {})))
end_list.update(config.default.get('Poezio', {}))
end_list = list(end_list)
end_list.sort()
elif n == 2:
@ -314,7 +314,10 @@ def completion_set(self, the_input):
if not plugin_name in self.plugin_manager.plugins:
return the_input.new_completion([''], n, quotify=True)
plugin = self.plugin_manager.plugins[plugin_name]
end_list = plugin.config.options(section or plugin_name)
end_list = set(plugin.config.options(section or plugin_name))
end_list.update(plugin.config.default.get(section or plugin_name, {}))
end_list = list(end_list)
end_list.sort()
elif not config.has_option('Poezio', args[1]):
if config.has_section(args[1]):
end_list = config.options(args[1])

View file

@ -19,12 +19,12 @@ class PluginConfig(config.Config):
They are accessible inside the plugin with self.config
and behave like the core Config object.
"""
def __init__(self, filename, module_name):
config.Config.__init__(self, filename)
def __init__(self, filename, module_name, default=None):
config.Config.__init__(self, filename, default=default)
self.module_name = module_name
self.read()
def get(self, option, default, section=None):
def get(self, option, default=None, section=None):
if not section:
section = self.module_name
return config.Config.get(self, option, default, section)
@ -364,12 +364,19 @@ class BasePlugin(object, metaclass=SafetyMetaclass):
Class that all plugins derive from.
"""
default_config = None
def __init__(self, plugin_api, core, plugins_conf_dir):
self.core = core
# More hack; luckily we'll never have more than one core object
SafetyMetaclass.core = core
conf = os.path.join(plugins_conf_dir, self.__module__+'.cfg')
self.config = PluginConfig(conf, self.__module__)
try:
self.config = PluginConfig(conf, self.__module__,
default=self.default_config)
except Exception:
log.debug('Error while creating the plugin config', exc_info=True)
self.config = PluginConfig(conf, self.__module__)
self._api = plugin_api[self.name]
self.init()