Make the config loading and saving significantly more functional, but also horrifyingly complex, with very little error handling

This commit is contained in:
Chris Jones 2009-12-26 19:43:01 +00:00
parent 825abbb658
commit f09e9947cd
1 changed files with 72 additions and 14 deletions

View File

@ -45,6 +45,7 @@ import sys
from copy import copy from copy import copy
from configobj import ConfigObj from configobj import ConfigObj
from borg import Borg from borg import Borg
from factory import Factory
from util import dbg, get_config_dir, dict_diff from util import dbg, get_config_dir, dict_diff
DEFAULTS = { DEFAULTS = {
@ -172,7 +173,7 @@ DEFAULTS = {
'ignore_hosts' : ['localhost','127.0.0.0/8','*.local'], 'ignore_hosts' : ['localhost','127.0.0.0/8','*.local'],
}, },
}, },
'layout': { 'layouts': {
}, },
'plugins': { 'plugins': {
}, },
@ -198,6 +199,8 @@ class Config(object):
def set_profile(self, profile): def set_profile(self, profile):
"""Set our profile (which usually means change it)""" """Set our profile (which usually means change it)"""
self.profile = profile self.profile = profile
if not self.base.profiles.has_key(profile):
self.base.profiles[profile] = copy(DEFAULTS['profiles']['default'])
def save(self): def save(self):
"""Cause ConfigBase to save our config to file""" """Cause ConfigBase to save our config to file"""
@ -205,10 +208,13 @@ class Config(object):
class ConfigBase(Borg): class ConfigBase(Borg):
"""Class to provide access to our user configuration""" """Class to provide access to our user configuration"""
loaded = None
sections = None
global_config = None global_config = None
profiles = None profiles = None
keybindings = None keybindings = None
plugins = None plugins = None
layouts = None
def __init__(self): def __init__(self):
"""Class initialiser""" """Class initialiser"""
@ -220,6 +226,11 @@ class ConfigBase(Borg):
def prepare_attributes(self): def prepare_attributes(self):
"""Set up our borg environment""" """Set up our borg environment"""
if self.loaded is None:
self.loaded = False
if self.sections is None:
self.sections = ['global_config', 'keybindings', 'profiles',
'layouts', 'plugins']
if self.global_config is None: if self.global_config is None:
self.global_config = copy(DEFAULTS['global_config']) self.global_config = copy(DEFAULTS['global_config'])
if self.profiles is None: if self.profiles is None:
@ -229,44 +240,91 @@ class ConfigBase(Borg):
self.keybindings = copy(DEFAULTS['keybindings']) self.keybindings = copy(DEFAULTS['keybindings'])
if self.plugins is None: if self.plugins is None:
self.plugins = {} self.plugins = {}
if self.layouts is None:
self.layouts = copy(DEFAULTS['layouts'])
def load(self): def load(self):
"""Load configuration data from our various sources""" """Load configuration data from our various sources"""
sections = ['global_config', 'keybindings', 'profiles', 'plugins'] if self.loaded is True:
configfile = open(os.path.join(get_config_dir(), 'epic-config'), 'r') dbg('ConfigBase::load: config already loaded')
return
filename = os.path.join(get_config_dir(), 'epic-config')
try:
configfile = open(filename, 'r')
except Exception, ex:
dbg('ConfigBase::load: Unable to open %s (%s)' % (filename, ex))
return
parser = ConfigObj(configfile) parser = ConfigObj(configfile)
for section_name in sections: for section_name in self.sections:
dbg('ConfigBase::load: Processing section: %s' % section_name)
section = getattr(self, section_name) section = getattr(self, section_name)
if section_name == 'profiles':
for profile in parser[section_name]:
dbg('ConfigBase::load: Processing profile: %s' % profile)
if not section.has_key(section_name):
section[profile] = copy(DEFAULTS['profiles']['default'])
section[profile].update(parser[section_name][profile])
elif section_name == ['layouts', 'plugins']:
for part in parser[section_name]:
dbg('ConfigBase::load: Processing %s: %s' % (section_name,
part))
section[part] = parser[section_name][part]
else:
try:
section.update(parser[section_name]) section.update(parser[section_name])
except KeyError, ex:
dbg('ConfigBase::load: skipping loading missing section %s' %
section_name)
self.loaded = True
def save(self): def save(self):
"""Save the config to a file""" """Save the config to a file"""
sections = ['global_config', 'keybindings', 'profiles', 'plugins'] dbg('ConfigBase::save: saving config')
parser = ConfigObj() parser = ConfigObj()
parser.indent_type = ' ' parser.indent_type = ' '
for section_name in sections:
for section_name in ['global_config', 'keybindings']:
dbg('ConfigBase::save: Processing section: %s' % section_name)
section = getattr(self, section_name) section = getattr(self, section_name)
parser[section_name] = dict_diff(DEFAULTS[section_name], section) parser[section_name] = dict_diff(DEFAULTS[section_name], section)
parser['profiles'] = {}
for profile in self.profiles:
dbg('ConfigBase::save: Processing profile: %s' % profile)
parser['profiles'][profile] = dict_diff(DEFAULTS['profiles']['default'],
self.profiles[profile])
parser['layouts'] = {}
for layout in self.layouts:
dbg('ConfigBase::save: Processing layout: %s' % layout)
parser['layouts'][layout] = self.layouts[layout]
parser['plugins'] = {}
for plugin in self.plugins:
dbg('ConfigBase::save: Processing plugin: %s' % plugin)
parser['plugins'][plugin] = self.plugins[plugin]
parser.write(open(os.path.join(get_config_dir(), 'epic-config'), 'w')) parser.write(open(os.path.join(get_config_dir(), 'epic-config'), 'w'))
def get_item(self, key, profile='default', plugin=None): def get_item(self, key, profile='default', plugin=None):
"""Look up a configuration item""" """Look up a configuration item"""
dbg('ConfigBase::get_item: Lookup %s (profile=%s, plugin=%s)' % (key, dbg('ConfigBase::get_item: %s:%s' % (profile, key))
profile, plugin))
if self.global_config.has_key(key): if self.global_config.has_key(key):
dbg('ConfigBase::get_item: found in globals: %s' % dbg('ConfigBase::get_item: found in globals: %s' %
self.global_config[key]) self.global_config[key])
return(self.global_config[key]) return(self.global_config[key])
elif self.profiles[profile].has_key(key): elif self.profiles[profile].has_key(key):
dbg('ConfigBase::get_item: found in profile: %s' % dbg('ConfigBase::get_item: found in profile %s (%s)' % (
self.profiles[profile][key]) profile, self.profiles[profile][key]))
return(self.profiles[profile][key]) return(self.profiles[profile][key])
elif key == 'keybindings': elif key == 'keybindings':
return(self.keybindings) return(self.keybindings)
elif plugin is not None and self.plugins[plugin].has_key(key): elif plugin is not None and self.plugins[plugin].has_key(key):
dbg('ConfigBase::get_item: found in plugin: %s' % dbg('ConfigBase::get_item: found in plugin %s (%s)' % (
self.plugins[plugin][key]) plugin, self.plugins[plugin][key]))
return(self.plugins[plugin][key]) return(self.plugins[plugin][key])
else: else:
raise KeyError('ConfigBase::get_item: unknown key %s' % key) raise KeyError('ConfigBase::get_item: unknown key %s' % key)
@ -279,7 +337,7 @@ class ConfigBase(Borg):
if self.global_config.has_key(key): if self.global_config.has_key(key):
self.global_config[key] = value self.global_config[key] = value
elif self.profiles[profile].has_key(key): elif self.profiles[profile].has_key(key):
self.profiles[profile] = value self.profiles[profile][key] = value
elif key == 'keybindings': elif key == 'keybindings':
self.keybindings = value self.keybindings = value
elif plugin is not None and self.plugins[plugin].has_key(key): elif plugin is not None and self.plugins[plugin].has_key(key):