config.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from collections import OrderedDict, namedtuple
  2. try:
  3. # Python 2
  4. from ConfigParser import ConfigParser
  5. except ImportError:
  6. # Python 3
  7. from configparser import ConfigParser
  8. Section_format = namedtuple('Section_format', ('name', 'options'))
  9. Config_option = namedtuple('Config_option', ('name', 'value_type', 'required'))
  10. def option(name, value_type=str, required=True):
  11. '''
  12. Given a config file option name, an expected type for its value, and whether it's required,
  13. return a Config_option capturing that information.
  14. '''
  15. return Config_option(name, value_type, required)
  16. CONFIG_FORMAT = (
  17. Section_format(
  18. 'location',
  19. (
  20. option('source_directories'),
  21. option('repository'),
  22. ),
  23. ),
  24. Section_format(
  25. 'retention',
  26. (
  27. option('keep_within', required=False),
  28. option('keep_hourly', int, required=False),
  29. option('keep_daily', int, required=False),
  30. option('keep_weekly', int, required=False),
  31. option('keep_monthly', int, required=False),
  32. option('keep_yearly', int, required=False),
  33. option('prefix', required=False),
  34. ),
  35. )
  36. )
  37. def validate_configuration_format(parser, config_format):
  38. '''
  39. Given an open ConfigParser and an expected config file format, validate that the parsed
  40. configuration file has the expected sections, that any required options are present in those
  41. sections, and that there aren't any unexpected options.
  42. Raise ValueError if anything is awry.
  43. '''
  44. section_names = parser.sections()
  45. required_section_names = tuple(section.name for section in config_format)
  46. if set(section_names) != set(required_section_names):
  47. raise ValueError(
  48. 'Expected config sections {} but found sections: {}'.format(
  49. ', '.join(required_section_names),
  50. ', '.join(section_names)
  51. )
  52. )
  53. for section_format in config_format:
  54. option_names = parser.options(section_format.name)
  55. expected_options = section_format.options
  56. unexpected_option_names = set(option_names) - set(option.name for option in expected_options)
  57. if unexpected_option_names:
  58. raise ValueError(
  59. 'Unexpected options found in config section {}: {}'.format(
  60. section_format.name,
  61. ', '.join(sorted(unexpected_option_names)),
  62. )
  63. )
  64. missing_option_names = tuple(
  65. option.name for option in expected_options if option.required
  66. if option.name not in option_names
  67. )
  68. if missing_option_names:
  69. raise ValueError(
  70. 'Required options missing from config section {}: {}'.format(
  71. section_format.name,
  72. ', '.join(missing_option_names)
  73. )
  74. )
  75. def parse_section_options(parser, section_format):
  76. '''
  77. Given an open ConfigParser and an expected section format, return the option values from that
  78. section as a dict mapping from option name to value. Omit those options that are not present in
  79. the parsed options.
  80. Raise ValueError if any option values cannot be coerced to the expected Python data type.
  81. '''
  82. type_getter = {
  83. str: parser.get,
  84. int: parser.getint,
  85. }
  86. return OrderedDict(
  87. (option.name, type_getter[option.value_type](section_format.name, option.name))
  88. for option in section_format.options
  89. if parser.has_option(section_format.name, option.name)
  90. )
  91. def parse_configuration(config_filename):
  92. '''
  93. Given a config filename of the expected format, return the parsed configuration as a tuple of
  94. (location config, retention config) where each config is a dict of that section's options.
  95. Raise IOError if the file cannot be read, or ValueError if the format is not as expected.
  96. '''
  97. parser = ConfigParser()
  98. parser.readfp(open(config_filename))
  99. validate_configuration_format(parser, CONFIG_FORMAT)
  100. return tuple(
  101. parse_section_options(parser, section_format)
  102. for section_format in CONFIG_FORMAT
  103. )