diff options
Diffstat (limited to 'lorrycontroller/readconf.py')
-rw-r--r-- | lorrycontroller/readconf.py | 65 |
1 files changed, 45 insertions, 20 deletions
diff --git a/lorrycontroller/readconf.py b/lorrycontroller/readconf.py index cd89a49..5e15dd5 100644 --- a/lorrycontroller/readconf.py +++ b/lorrycontroller/readconf.py @@ -234,32 +234,57 @@ class ReadConfiguration(lorrycontroller.LorryControllerRoute): class LorryControllerConfValidator(object): def validate_config(self, conf_obj): - for check in self._find_checks(): - error = check(conf_obj) - if error: - return error - return None + try: + self._check_is_list(conf_obj) + self._check_is_list_of_dicts(conf_obj) + + for section in conf_obj: + if 'type' not in section: + raise ValidationError( + 'section without type: %r' % section) + elif section['type'] in ('trove', 'troves'): + self._check_troves_section(section) + elif section['type'] == 'lorries': + self._check_lorries_section(section) + else: + raise ValidationError( + 'unknown section type %r' % section['type']) + except ValidationError as e: + return str(e) - def _find_checks(self): - return [ - getattr(self, name) - for name in dir(self) - if name.startswith('_check_')] + return None def _check_is_list(self, conf_obj): if type(conf_obj) is not list: - return 'is not a JSON list' - + raise ValidationError( + 'type %r is not a JSON list' % type(conf_obj)) + def _check_is_list_of_dicts(self, conf_obj): for item in conf_obj: if type(item) is not dict: - return 'all items must be dicts' + raise ValidationError('all items must be dicts') - def _check_sections_have_known_type(self, conf_obj): - allowed_types = ['lorries', 'trove', 'troves'] + def _check_troves_section(self, section): + self._check_has_required_fields( + section, ['trovehost', 'interval', 'ls-interval', 'prefixmap']) + self._check_prefixmap(section) - for item in conf_obj: - if 'type' not in conf_obj: - return 'must have a type field' - if conf_obj['type'] not in allowed_types: - return ('must have a known type, not %r' % conf_obj['type']) + def _check_prefixmap(self, section): + pass + + def _check_lorries_section(self, section): + self._check_has_required_fields( + section, ['interval', 'prefix', 'globs']) + + def _check_has_required_fields(self, section, fields): + for field in fields: + if field not in section: + raise ValidationError( + 'mandatory field %s missing in section %r' % + (field, section)) + + +class ValidationError(Exception): + + def __init__(self, msg): + Exception.__init__(self, msg) |