configreader.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/python3
  2. import configparser
  3. class SectionValidator(object):
  4. def __init__(self, keys):
  5. """
  6. keys
  7. list of tuples containing the names and whether the
  8. key is mandatory
  9. """
  10. self.keys = keys
  11. def check(self, config, section_name):
  12. """
  13. validates the section, if this is the correct validator for it
  14. returns True if this is the correct validator for this section
  15. raises InvalidConfig if something inside the section is wrong
  16. """
  17. self._check_mandatory_fields(section_name, config[section_name])
  18. self._check_invalid_keys(section_name, config[section_name])
  19. def _check_mandatory_fields(self, section_name, key):
  20. for key_name, mandatory in self.keys:
  21. if mandatory:
  22. try:
  23. key[key_name]
  24. except KeyError:
  25. err_msg = ("The section '{0}' must contain a "
  26. "key '{1}'!").format(
  27. section_name,
  28. key_name)
  29. raise InvalidConfig(err_msg)
  30. def _check_invalid_keys(self, section_name, section):
  31. for key in section:
  32. key_name = str(key)
  33. valid_key_names = [s[0] for s in self.keys]
  34. is_valid_key = key_name in valid_key_names
  35. if not is_valid_key:
  36. err_msg = ("'{0}' is not a valid key name for '{1}'. Must "
  37. "be one of these: {2}").format(
  38. key_name,
  39. section_name,
  40. ', '.join(valid_key_names))
  41. raise InvalidConfig(err_msg)
  42. # contains all configuration sections and keys
  43. # the keys are a tuple with their name and a boolean, which
  44. # tells us whether the option is mandatory
  45. CONFIG_VALIDATORS = {
  46. 'Application': SectionValidator([
  47. ('name', True),
  48. ('version', True),
  49. ('entry_point', False),
  50. ('script', False),
  51. ('icon', False),
  52. ('console', False),
  53. ]),
  54. 'Build': SectionValidator([
  55. ('directory', False),
  56. ('installer_name', False),
  57. ('nsi_template', False),
  58. ]),
  59. 'Include': SectionValidator([
  60. ('packages', False),
  61. ('files', False),
  62. ]),
  63. 'Python': SectionValidator([
  64. ('version', True),
  65. ('bitness', False),
  66. ]),
  67. 'Shortcut': SectionValidator([
  68. ('entry_point', False),
  69. ('script', False),
  70. ('icon', False),
  71. ('console', False),
  72. ]),
  73. }
  74. class InvalidConfig(ValueError):
  75. pass
  76. def read_and_validate(config_file):
  77. config = configparser.ConfigParser()
  78. config.read(config_file)
  79. for section in config.sections():
  80. if section in CONFIG_VALIDATORS:
  81. CONFIG_VALIDATORS[section].check(config, section)
  82. elif section.startswith('Shortcut '):
  83. CONFIG_VALIDATORS['Shortcut'].check(config, section)
  84. else:
  85. valid_section_names = CONFIG_VALIDATORS.keys()
  86. err_msg = ("{0} is not a valid section header. Must "
  87. "be one of these: {1}").format(
  88. section,
  89. ', '.join(['"%s"' % n for n in valid_section_names]))
  90. raise InvalidConfig(err_msg)
  91. return config