diff --git a/mopidy/config/__init__.py b/mopidy/config/__init__.py index 66e17c5e..abb44eea 100644 --- a/mopidy/config/__init__.py +++ b/mopidy/config/__init__.py @@ -81,11 +81,22 @@ def _load(files, defaults, overrides): try: with io.open(filename, 'rb') as filehandle: parser.readfp(filehandle) + except configparser.MissingSectionHeaderError as e: + logging.warning('%s does not have a config section, not loaded.', + filename) + except configparser.ParsingError as e: + linenos = ', '.join(str(lineno) for lineno, line in e.errors) + logger.warning('%s has errors, line %s has been ignored.', + filename, linenos) except IOError: # TODO: if this is the initial load of logging config we might not # have a logger at this point, we might want to handle this better. logger.debug('Config file %s not found; skipping', filename) + # If there have been parse errors there is a python bug that causes the + # values to be lists, this little trick coerces these into strings. + parser.readfp(io.BytesIO()) + raw_config = {} for section in parser.sections(): raw_config[section] = dict(parser.items(section)) diff --git a/tests/config/config_test.py b/tests/config/config_test.py index 016398ed..84079a2e 100644 --- a/tests/config/config_test.py +++ b/tests/config/config_test.py @@ -63,6 +63,11 @@ class LoadConfigTest(unittest.TestCase): result = config._load([path_to_data_dir('file3.conf')], [], []) self.assertEqual(expected, result) + def test_load_file_with_error(self): + expected = {'foo': {'bar': 'baz'}} + result = config._load([path_to_data_dir('file4.conf')], [], []) + self.assertEqual(expected, result) + class ValidateTest(unittest.TestCase): def setUp(self): diff --git a/tests/data/file4.conf b/tests/data/file4.conf new file mode 100644 index 00000000..b499a48a --- /dev/null +++ b/tests/data/file4.conf @@ -0,0 +1,3 @@ +[foo] +bar = baz +foobar