configreader.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/python3
  2. import configparser
  3. class SectionValidator(object):
  4. def __init__(self, subsections):
  5. """
  6. subsections
  7. list of tuples containing the names and whether the
  8. subsection is mandatory
  9. """
  10. self.subsections = subsections
  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_subsections(section_name, config[section_name])
  19. def _check_mandatory_fields(self, section_name, subsection):
  20. for subsection_name, mandatory in self.subsections:
  21. if mandatory:
  22. try:
  23. subsection[subsection_name]
  24. except KeyError:
  25. err_msg = ("The section '{0}' must contain a "
  26. "subsection '{1}'!").format(
  27. section_name,
  28. subsection_name)
  29. raise InvalidConfig(err_msg)
  30. def _check_invalid_subsections(self, section_name, section):
  31. for subsection in section:
  32. subsection_name = str(subsection)
  33. valid_subsection_names = [s[0] for s in self.subsections]
  34. is_valid_subsection = subsection_name in valid_subsection_names
  35. if not is_valid_subsection:
  36. err_msg = ("'{0}' is not a valid subsection name for '{1}'. Must "
  37. "be one of these: {2}").format(
  38. subsection_name,
  39. section_name,
  40. ', '.join(valid_subsection_names))
  41. raise InvalidConfig(err_msg)
  42. # contains all configuration sections and subsections
  43. # the subsections 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', True),
  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', True),
  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