config: Add preliminary handling of parse errors
This commit is contained in:
parent
c5b036f988
commit
9fab339941
@ -81,11 +81,22 @@ def _load(files, defaults, overrides):
|
|||||||
try:
|
try:
|
||||||
with io.open(filename, 'rb') as filehandle:
|
with io.open(filename, 'rb') as filehandle:
|
||||||
parser.readfp(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:
|
except IOError:
|
||||||
# TODO: if this is the initial load of logging config we might not
|
# 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.
|
# have a logger at this point, we might want to handle this better.
|
||||||
logger.debug('Config file %s not found; skipping', filename)
|
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 = {}
|
raw_config = {}
|
||||||
for section in parser.sections():
|
for section in parser.sections():
|
||||||
raw_config[section] = dict(parser.items(section))
|
raw_config[section] = dict(parser.items(section))
|
||||||
|
|||||||
@ -63,6 +63,11 @@ class LoadConfigTest(unittest.TestCase):
|
|||||||
result = config._load([path_to_data_dir('file3.conf')], [], [])
|
result = config._load([path_to_data_dir('file3.conf')], [], [])
|
||||||
self.assertEqual(expected, result)
|
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):
|
class ValidateTest(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|||||||
3
tests/data/file4.conf
Normal file
3
tests/data/file4.conf
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
[foo]
|
||||||
|
bar = baz
|
||||||
|
foobar
|
||||||
Loading…
Reference in New Issue
Block a user