Use a callback for getting settings from ConfigFile, so we can validate them as they come in.
This commit is contained in:
parent
5c8b1e7435
commit
a63c1358ca
|
@ -207,7 +207,7 @@ class TerminatorConfValuestoreRC (TerminatorConfValuestore):
|
||||||
self.rcfilename = os.path.join(directory, "terminator/config")
|
self.rcfilename = os.path.join(directory, "terminator/config")
|
||||||
dbg(" VS_RCFile: config file located at %s" % self.rcfilename)
|
dbg(" VS_RCFile: config file located at %s" % self.rcfilename)
|
||||||
if os.path.exists (self.rcfilename):
|
if os.path.exists (self.rcfilename):
|
||||||
ini = ConfigFile(self.rcfilename)
|
ini = ConfigFile(self.rcfilename, self._rc_set_callback())
|
||||||
try:
|
try:
|
||||||
ini.parse()
|
ini.parse()
|
||||||
except ParsedWithErrors, e:
|
except ParsedWithErrors, e:
|
||||||
|
@ -226,50 +226,48 @@ Some lines have been ignored.""") % escape(repr(self.rcfilename))
|
||||||
dialog.run()
|
dialog.run()
|
||||||
dialog.destroy()
|
dialog.destroy()
|
||||||
|
|
||||||
for key in ini.settings:
|
def _rc_set_callback(self):
|
||||||
try:
|
def callback(section, key, value):
|
||||||
value = ini.settings[key]
|
if section is None:
|
||||||
# Check if this is actually a key we care about
|
|
||||||
if not Defaults.has_key (key):
|
if not Defaults.has_key (key):
|
||||||
# We should really mention this to the user
|
raise ValueError("Unknown configuration option %s" % repr(key))
|
||||||
continue
|
|
||||||
|
|
||||||
deftype = Defaults[key].__class__.__name__
|
deftype = Defaults[key].__class__.__name__
|
||||||
if key.endswith('_color'):
|
if key.endswith('_color'):
|
||||||
try:
|
try:
|
||||||
gtk.gdk.color_parse(value)
|
gtk.gdk.color_parse(value)
|
||||||
self.values[key] = value
|
self.values[key] = value
|
||||||
except ValueError:
|
except ValueError:
|
||||||
err(_("Setting %s value %s not a valid colour; ignoring") % (key,repr(value)))
|
raise ValueError(_("Setting %s value %s not a valid colour; ignoring") % (key,repr(value)))
|
||||||
continue
|
|
||||||
elif deftype == 'bool':
|
elif deftype == 'bool':
|
||||||
if value.lower () in ('true', 'yes', 'on'):
|
if value.lower () in ('true', 'yes', 'on'):
|
||||||
self.values[key] = True
|
self.values[key] = True
|
||||||
elif value.lower () in ('false', 'no', 'off'):
|
elif value.lower () in ('false', 'no', 'off'):
|
||||||
self.values[key] = False
|
self.values[key] = False
|
||||||
else:
|
else:
|
||||||
err(_("Boolean setting %s expecting one of: yes, no, true, false, on, off") % key)
|
raise ValueError(_("Boolean setting %s expecting one of: yes, no, true, false, on, off") % key)
|
||||||
continue
|
|
||||||
elif deftype == 'int':
|
elif deftype == 'int':
|
||||||
self.values[key] = int (value)
|
self.values[key] = int (value)
|
||||||
elif deftype == 'float':
|
elif deftype == 'float':
|
||||||
self.values[key] = float (value)
|
self.values[key] = float (value)
|
||||||
elif deftype == 'list':
|
elif deftype == 'list':
|
||||||
err (_(" VS_RCFile: Reading list values from .config/terminator/config is not currently supported"))
|
raise ValueError(_("Reading list values from terminator_config(5) is not currently supported"))
|
||||||
continue
|
|
||||||
elif deftype == 'dict':
|
elif deftype == 'dict':
|
||||||
if type(value) != dict:
|
if type(value) != dict:
|
||||||
err(_("Value %s should be a section name, not a setting") % key)
|
raise ValueError(_("Setting %s should be a section name") % repr(key))
|
||||||
continue
|
|
||||||
self.values[key] = value
|
self.values[key] = value
|
||||||
else:
|
else:
|
||||||
self.values[key] = value
|
self.values[key] = value
|
||||||
|
|
||||||
dbg (" VS_RCFile: Set value '%s' to %s" % (key, repr(self.values[key])))
|
dbg (" VS_RCFile: Set value '%s' to %s" % (key, repr(self.values[key])))
|
||||||
except Exception, e:
|
elif section == 'keybindings':
|
||||||
dbg (" VS_RCFile: %s Exception handling: %s" % (type(e), key))
|
self.values.setdefault(section, {})
|
||||||
pass
|
if not Defaults[section].has_key(key):
|
||||||
dbg("Config parsed as: %s" % repr(self.values))
|
raise ValueError("Keybinding name %s is unknown" % repr(key))
|
||||||
|
else:
|
||||||
|
self.values[section][key] = value
|
||||||
|
else:
|
||||||
|
raise ValueError("Section name %s is unknown" % repr(section))
|
||||||
|
return callback
|
||||||
|
|
||||||
class TerminatorConfValuestoreGConf (TerminatorConfValuestore):
|
class TerminatorConfValuestoreGConf (TerminatorConfValuestore):
|
||||||
profile = ""
|
profile = ""
|
||||||
|
|
|
@ -62,10 +62,10 @@ Some lines have been ignored.
|
||||||
|
|
||||||
|
|
||||||
class ConfigFile:
|
class ConfigFile:
|
||||||
def __init__(self, filename = None, errors_are_fatal = False):
|
def __init__(self, filename = None, callback = None, errors_are_fatal = False):
|
||||||
|
self.callback = callback
|
||||||
self.errors_are_fatal = errors_are_fatal
|
self.errors_are_fatal = errors_are_fatal
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
self.settings = {}
|
|
||||||
self.errors = []
|
self.errors = []
|
||||||
|
|
||||||
def _call_if_match(self, re, callable, group = 0):
|
def _call_if_match(self, re, callable, group = 0):
|
||||||
|
@ -164,18 +164,8 @@ class ConfigFile:
|
||||||
|
|
||||||
def _value(self, value):
|
def _value(self, value):
|
||||||
dbg("Value %s" % repr(value))
|
dbg("Value %s" % repr(value))
|
||||||
self._currvalue = value
|
|
||||||
|
|
||||||
def _line_ok(self):
|
|
||||||
if self._currvalue is None: return
|
|
||||||
try:
|
try:
|
||||||
if self._currsection is not None:
|
self.callback(self._currsection, self._currsetting, value)
|
||||||
try:
|
except ValueError, e:
|
||||||
self.settings.setdefault(self._currsection, {})[self._currsetting] = self._currvalue
|
raise ConfigSyntaxError(str(e), self)
|
||||||
except TypeError, e:
|
|
||||||
raise ConfigSyntaxError(_("Section %s is being used as a setting name" % repr(self._currsection)), self)
|
|
||||||
else:
|
|
||||||
self.settings[self._currsetting] = self._currvalue
|
|
||||||
finally:
|
|
||||||
self._currvalue = None
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue