2014-12-07 03:35:20 +01:00
|
|
|
from collections import OrderedDict, namedtuple
|
2014-12-02 07:14:35 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
# Python 2
|
|
|
|
from ConfigParser import ConfigParser
|
|
|
|
except ImportError:
|
|
|
|
# Python 3
|
|
|
|
from configparser import ConfigParser
|
2014-10-31 06:34:03 +01:00
|
|
|
|
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
Section_format = namedtuple('Section_format', ('name', 'options'))
|
|
|
|
Config_option = namedtuple('Config_option', ('name', 'value_type', 'required'))
|
2014-10-31 06:34:03 +01:00
|
|
|
|
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
def option(name, value_type=str, required=True):
|
|
|
|
'''
|
|
|
|
Given a config file option name, an expected type for its value, and whether it's required,
|
|
|
|
return a Config_option capturing that information.
|
|
|
|
'''
|
|
|
|
return Config_option(name, value_type, required)
|
2014-10-31 06:34:03 +01:00
|
|
|
|
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
CONFIG_FORMAT = (
|
|
|
|
Section_format(
|
|
|
|
'location',
|
|
|
|
(
|
|
|
|
option('source_directories'),
|
|
|
|
option('repository'),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Section_format(
|
|
|
|
'retention',
|
|
|
|
(
|
|
|
|
option('keep_within', required=False),
|
|
|
|
option('keep_hourly', int, required=False),
|
|
|
|
option('keep_daily', int, required=False),
|
|
|
|
option('keep_weekly', int, required=False),
|
|
|
|
option('keep_monthly', int, required=False),
|
|
|
|
option('keep_yearly', int, required=False),
|
|
|
|
option('prefix', required=False),
|
|
|
|
),
|
2015-05-11 07:00:31 +02:00
|
|
|
),
|
|
|
|
Section_format(
|
|
|
|
'consistency',
|
|
|
|
(
|
|
|
|
option('checks', required=False),
|
|
|
|
),
|
2014-12-07 03:35:20 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def validate_configuration_format(parser, config_format):
|
2014-10-31 06:34:03 +01:00
|
|
|
'''
|
2014-12-07 03:35:20 +01:00
|
|
|
Given an open ConfigParser and an expected config file format, validate that the parsed
|
|
|
|
configuration file has the expected sections, that any required options are present in those
|
|
|
|
sections, and that there aren't any unexpected options.
|
2014-12-02 07:35:25 +01:00
|
|
|
|
2015-05-11 07:00:31 +02:00
|
|
|
A section is required if any of its contained options are required.
|
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
Raise ValueError if anything is awry.
|
2014-10-31 06:34:03 +01:00
|
|
|
'''
|
2015-05-11 07:00:31 +02:00
|
|
|
section_names = set(parser.sections())
|
|
|
|
required_section_names = tuple(
|
|
|
|
section.name for section in config_format
|
|
|
|
if any(option.required for option in section.options)
|
|
|
|
)
|
2014-10-31 06:34:03 +01:00
|
|
|
|
2015-05-11 07:00:31 +02:00
|
|
|
unknown_section_names = section_names - set(
|
|
|
|
section_format.name for section_format in config_format
|
|
|
|
)
|
|
|
|
if unknown_section_names:
|
2014-10-31 06:34:03 +01:00
|
|
|
raise ValueError(
|
2015-05-11 07:00:31 +02:00
|
|
|
'Unknown config sections found: {}'.format(', '.join(unknown_section_names))
|
|
|
|
)
|
|
|
|
|
|
|
|
missing_section_names = set(required_section_names) - section_names
|
|
|
|
if missing_section_names:
|
|
|
|
raise ValueError(
|
|
|
|
'Missing config sections: {}'.format(', '.join(missing_section_names))
|
2014-10-31 06:34:03 +01:00
|
|
|
)
|
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
for section_format in config_format:
|
2015-05-11 07:00:31 +02:00
|
|
|
if section_format.name not in section_names:
|
|
|
|
continue
|
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
option_names = parser.options(section_format.name)
|
|
|
|
expected_options = section_format.options
|
2014-10-31 06:34:03 +01:00
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
unexpected_option_names = set(option_names) - set(option.name for option in expected_options)
|
|
|
|
|
|
|
|
if unexpected_option_names:
|
|
|
|
raise ValueError(
|
|
|
|
'Unexpected options found in config section {}: {}'.format(
|
|
|
|
section_format.name,
|
|
|
|
', '.join(sorted(unexpected_option_names)),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
missing_option_names = tuple(
|
|
|
|
option.name for option in expected_options if option.required
|
|
|
|
if option.name not in option_names
|
|
|
|
)
|
|
|
|
|
|
|
|
if missing_option_names:
|
2014-10-31 06:34:03 +01:00
|
|
|
raise ValueError(
|
2014-12-07 03:35:20 +01:00
|
|
|
'Required options missing from config section {}: {}'.format(
|
|
|
|
section_format.name,
|
|
|
|
', '.join(missing_option_names)
|
2014-10-31 06:34:03 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
|
2015-05-11 07:00:31 +02:00
|
|
|
# Describes a parsed configuration, where each attribute is the name of a configuration file section
|
|
|
|
# and each value is a dict of that section's parsed options.
|
|
|
|
Parsed_config = namedtuple('Config', (section_format.name for section_format in CONFIG_FORMAT))
|
|
|
|
|
|
|
|
|
2014-12-07 03:35:20 +01:00
|
|
|
def parse_section_options(parser, section_format):
|
|
|
|
'''
|
|
|
|
Given an open ConfigParser and an expected section format, return the option values from that
|
|
|
|
section as a dict mapping from option name to value. Omit those options that are not present in
|
|
|
|
the parsed options.
|
|
|
|
|
|
|
|
Raise ValueError if any option values cannot be coerced to the expected Python data type.
|
|
|
|
'''
|
|
|
|
type_getter = {
|
|
|
|
str: parser.get,
|
|
|
|
int: parser.getint,
|
|
|
|
}
|
|
|
|
|
|
|
|
return OrderedDict(
|
|
|
|
(option.name, type_getter[option.value_type](section_format.name, option.name))
|
|
|
|
for option in section_format.options
|
|
|
|
if parser.has_option(section_format.name, option.name)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_configuration(config_filename):
|
|
|
|
'''
|
2015-05-11 07:00:31 +02:00
|
|
|
Given a config filename of the expected format, return the parsed configuration as Parsed_config
|
|
|
|
data structure.
|
2014-12-07 03:35:20 +01:00
|
|
|
|
|
|
|
Raise IOError if the file cannot be read, or ValueError if the format is not as expected.
|
|
|
|
'''
|
|
|
|
parser = ConfigParser()
|
2015-06-14 20:00:46 +02:00
|
|
|
parser.read(config_filename)
|
2014-12-07 03:35:20 +01:00
|
|
|
|
|
|
|
validate_configuration_format(parser, CONFIG_FORMAT)
|
|
|
|
|
2015-05-11 07:00:31 +02:00
|
|
|
return Parsed_config(
|
|
|
|
*(
|
|
|
|
parse_section_options(parser, section_format)
|
|
|
|
for section_format in CONFIG_FORMAT
|
|
|
|
)
|
2014-10-31 06:34:03 +01:00
|
|
|
)
|