diff options
author | Lars Wirzenius <liw@liw.fi> | 2015-12-26 19:49:07 +0100 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2015-12-26 19:49:07 +0100 |
commit | 7e8d31fa8adba20a21c13c1d4a261fb0a815e6c5 (patch) | |
tree | 6a3c58c9bfc0291167b5afc56c10f958bd204e34 | |
parent | 5a97324643bd0ad6ae96fe7838f2321dad0cbb59 (diff) | |
download | cliapp-7e8d31fa8adba20a21c13c1d4a261fb0a815e6c5.tar.gz |
Give better error message if YAML config is bad
-rw-r--r-- | cliapp/__init__.py | 3 | ||||
-rw-r--r-- | cliapp/app.py | 3 | ||||
-rw-r--r-- | cliapp/settings.py | 21 | ||||
-rw-r--r-- | cliapp/settings_tests.py | 1 |
4 files changed, 27 insertions, 1 deletions
diff --git a/cliapp/__init__.py b/cliapp/__init__.py index 77aae09..4b0cede 100644 --- a/cliapp/__init__.py +++ b/cliapp/__init__.py @@ -34,7 +34,8 @@ from .util import MemoryProfileDumper from .fmt import TextFormat from .app import Application, AppException from .settings import (Settings, log_group_name, config_group_name, - perf_group_name, UnknownConfigVariable) + perf_group_name, UnknownConfigVariable, + MalformedYamlConfig) from .runcmd import runcmd, runcmd_unchecked, shell_quote, ssh_runcmd # The plugin system diff --git a/cliapp/app.py b/cliapp/app.py index 1aa35cb..cd1b576 100644 --- a/cliapp/app.py +++ b/cliapp/app.py @@ -192,6 +192,9 @@ class Application(object): except cliapp.UnknownConfigVariable, e: # pragma: no cover stderr.write('ERROR: %s\n' % str(e)) sys.exit(1) + except cliapp.MalformedYamlConfig as e: # pragma: no cover + stderr.write('ERROR: %s\n' % str(e)) + sys.exit(1) except AppException, e: log(traceback.format_exc()) stderr.write('ERROR: %s\n' % str(e)) diff --git a/cliapp/settings.py b/cliapp/settings.py index 1a88a6b..1fe1a67 100644 --- a/cliapp/settings.py +++ b/cliapp/settings.py @@ -48,6 +48,15 @@ class UnknownConfigVariable(cliapp.AppException): return self.msg +class MalformedYamlConfig(cliapp.AppException): # pragma: no cover + + def __init__(self, msg): + cliapp.AppException.__init__(self, msg) + + def __str__(self): # pragma: no cover + return self.msg + + class Setting(object): action = 'store' @@ -810,6 +819,7 @@ class Settings(object): def _read_yaml(self, pathname, f): obj = yaml.safe_load(f) + self._check_yaml(pathname, obj) config = obj.get('config', {}) for name, value in config.items(): if name not in self._settingses: @@ -826,6 +836,17 @@ class Settings(object): for option in obj[section]: section_data[option] = obj[section][option] + def _check_yaml(self, pathname, obj): # pragma: no cover + if not isinstance(obj, dict): + raise cliapp.MalformedYamlConfig( + 'Configuration file %s does not specify a key/value mapping' % + pathname) + + if 'config' not in obj: + raise cliapp.MalformedYamlConfig( + 'Configuration file %s does not have a "config" key' % + pathname) + def _generate_manpage(self, o, dummy, value, p): # pragma: no cover template = open(value).read() generator = ManpageGenerator(template, p, self._arg_synopsis, diff --git a/cliapp/settings_tests.py b/cliapp/settings_tests.py index efb7db3..9360b63 100644 --- a/cliapp/settings_tests.py +++ b/cliapp/settings_tests.py @@ -482,6 +482,7 @@ something = else def mock_open(filename, mode=None): return StringIO.StringIO('''\ +config: {} extra: something: else ''') |