config.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. from collections import OrderedDict, namedtuple
  2. try:
  3. # Python 2
  4. from ConfigParser import RawConfigParser
  5. except ImportError:
  6. # Python 3
  7. from configparser import RawConfigParser
  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. def validate_configuration_format(parser, config_format):
  17. '''
  18. Given an open RawConfigParser and an expected config file format, validate that the parsed
  19. configuration file has the expected sections, that any required options are present in those
  20. sections, and that there aren't any unexpected options.
  21. A section is required if any of its contained options are required.
  22. Raise ValueError if anything is awry.
  23. '''
  24. section_names = set(parser.sections())
  25. required_section_names = tuple(
  26. section.name for section in config_format
  27. if any(option.required for option in section.options)
  28. )
  29. unknown_section_names = section_names - set(
  30. section_format.name for section_format in config_format
  31. )
  32. if unknown_section_names:
  33. raise ValueError(
  34. 'Unknown config sections found: {}'.format(', '.join(unknown_section_names))
  35. )
  36. missing_section_names = set(required_section_names) - section_names
  37. if missing_section_names:
  38. raise ValueError(
  39. 'Missing config sections: {}'.format(', '.join(missing_section_names))
  40. )
  41. for section_format in config_format:
  42. if section_format.name not in section_names:
  43. continue
  44. option_names = parser.options(section_format.name)
  45. expected_options = section_format.options
  46. unexpected_option_names = set(option_names) - set(option.name for option in expected_options)
  47. if unexpected_option_names:
  48. raise ValueError(
  49. 'Unexpected options found in config section {}: {}'.format(
  50. section_format.name,
  51. ', '.join(sorted(unexpected_option_names)),
  52. )
  53. )
  54. missing_option_names = tuple(
  55. option.name for option in expected_options if option.required
  56. if option.name not in option_names
  57. )
  58. if missing_option_names:
  59. raise ValueError(
  60. 'Required options missing from config section {}: {}'.format(
  61. section_format.name,
  62. ', '.join(missing_option_names)
  63. )
  64. )
  65. def parse_section_options(parser, section_format):
  66. '''
  67. Given an open RawConfigParser and an expected section format, return the option values from that
  68. section as a dict mapping from option name to value. Omit those options that are not present in
  69. the parsed options.
  70. Raise ValueError if any option values cannot be coerced to the expected Python data type.
  71. '''
  72. type_getter = {
  73. str: parser.get,
  74. int: parser.getint,
  75. }
  76. return OrderedDict(
  77. (option.name, type_getter[option.value_type](section_format.name, option.name))
  78. for option in section_format.options
  79. if parser.has_option(section_format.name, option.name)
  80. )
  81. def parse_configuration(config_filename, config_format):
  82. '''
  83. Given a config filename and an expected config file format, return the parsed configuration
  84. as a namedtuple with one attribute for each parsed section.
  85. Raise IOError if the file cannot be read, or ValueError if the format is not as expected.
  86. '''
  87. parser = RawConfigParser()
  88. parser.read(config_filename)
  89. validate_configuration_format(parser, config_format)
  90. # Describes a parsed configuration, where each attribute is the name of a configuration file
  91. # section and each value is a dict of that section's parsed options.
  92. Parsed_config = namedtuple('Parsed_config', (section_format.name for section_format in config_format))
  93. return Parsed_config(
  94. *(
  95. parse_section_options(parser, section_format)
  96. for section_format in config_format
  97. )
  98. )