|
@@ -3,13 +3,13 @@
|
|
|
import configparser
|
|
|
|
|
|
class SectionValidator(object):
|
|
|
- def __init__(self, subsections):
|
|
|
+ def __init__(self, keys):
|
|
|
"""
|
|
|
- subsections
|
|
|
+ keys
|
|
|
list of tuples containing the names and whether the
|
|
|
- subsection is mandatory
|
|
|
+ key is mandatory
|
|
|
"""
|
|
|
- self.subsections = subsections
|
|
|
+ self.keys = keys
|
|
|
|
|
|
def check(self, config, section_name):
|
|
|
"""
|
|
@@ -19,35 +19,35 @@ class SectionValidator(object):
|
|
|
raises InvalidConfig if something inside the section is wrong
|
|
|
"""
|
|
|
self._check_mandatory_fields(section_name, config[section_name])
|
|
|
- self._check_invalid_subsections(section_name, config[section_name])
|
|
|
+ self._check_invalid_keys(section_name, config[section_name])
|
|
|
|
|
|
- def _check_mandatory_fields(self, section_name, subsection):
|
|
|
- for subsection_name, mandatory in self.subsections:
|
|
|
+ def _check_mandatory_fields(self, section_name, key):
|
|
|
+ for key_name, mandatory in self.keys:
|
|
|
if mandatory:
|
|
|
try:
|
|
|
- subsection[subsection_name]
|
|
|
+ key[key_name]
|
|
|
except KeyError:
|
|
|
err_msg = ("The section '{0}' must contain a "
|
|
|
- "subsection '{1}'!").format(
|
|
|
+ "key '{1}'!").format(
|
|
|
section_name,
|
|
|
- subsection_name)
|
|
|
+ key_name)
|
|
|
raise InvalidConfig(err_msg)
|
|
|
|
|
|
- def _check_invalid_subsections(self, section_name, section):
|
|
|
- for subsection in section:
|
|
|
- subsection_name = str(subsection)
|
|
|
- valid_subsection_names = [s[0] for s in self.subsections]
|
|
|
- is_valid_subsection = subsection_name in valid_subsection_names
|
|
|
- if not is_valid_subsection:
|
|
|
- err_msg = ("'{0}' is not a valid subsection name for '{1}'. Must "
|
|
|
+ def _check_invalid_keys(self, section_name, section):
|
|
|
+ for key in section:
|
|
|
+ key_name = str(key)
|
|
|
+ valid_key_names = [s[0] for s in self.keys]
|
|
|
+ is_valid_key = key_name in valid_key_names
|
|
|
+ if not is_valid_key:
|
|
|
+ err_msg = ("'{0}' is not a valid key name for '{1}'. Must "
|
|
|
"be one of these: {2}").format(
|
|
|
- subsection_name,
|
|
|
+ key_name,
|
|
|
section_name,
|
|
|
- ', '.join(valid_subsection_names))
|
|
|
+ ', '.join(valid_key_names))
|
|
|
raise InvalidConfig(err_msg)
|
|
|
|
|
|
-# contains all configuration sections and subsections
|
|
|
-# the subsections are a tuple with their name and a boolean, which
|
|
|
+# contains all configuration sections and keys
|
|
|
+# the keys are a tuple with their name and a boolean, which
|
|
|
# tells us whether the option is mandatory
|
|
|
CONFIG_VALIDATORS = {
|
|
|
'Application': SectionValidator([
|