Skip to content
Snippets Groups Projects
Commit e41e36b1 authored by James Graham's avatar James Graham
Browse files

Add proper error message if a config file contains no sections

parent 577dfc6b
No related branches found
No related tags found
No related merge requests found
......@@ -8,12 +8,22 @@ import os
import collections
class DuplicateSectionError(Exception):
class DuplicateSectionError(KeyError):
"""
Exception used to indicate that a section has appeared twice in a file.
"""
def __repr__(self):
return "Section {0} appears twice in file {1}.".format(*self.args)
def __init__(self, section, filename):
msg = "Section '{0}' appears twice in file '{1}'.".format(section, filename)
super(DuplicateSectionError, self).__init__(msg)
class NoSectionError(KeyError):
"""
Exception used to indicate that a file contains no sections.
"""
def __init__(self, filename):
msg = "File '{0}' contains no '[]' section headers.".format(filename)
super(NoSectionError, self).__init__(msg)
class CFG(collections.OrderedDict):
......@@ -55,7 +65,10 @@ class CFG(collections.OrderedDict):
continue
toks = tuple(line.split())
try:
self[curr_section].append(toks)
except KeyError as e:
raise NoSectionError(self.filename) from e
def __enter__(self):
return self
......
import unittest
from pycgtool.parsers.cfg import DuplicateSectionError
from pycgtool.parsers.cfg import DuplicateSectionError, NoSectionError
from pycgtool.parsers import CFG
......@@ -38,6 +38,10 @@ class TestParsersCFG(unittest.TestCase):
self.assertTrue("DOPC" in cfg)
self.assertTrue("GLY" in cfg)
def test_error_no_sections(self):
with self.assertRaises(NoSectionError):
CFG("test/data/nosections.cfg")
if __name__ == '__main__':
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment