configreader.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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
  92. def read_shortcuts_config(cfg):
  93. """Read and verify the shortcut definitions from the config file.
  94. There is one shortcut per 'Shortcut <name>' section, and one for the
  95. Application section.
  96. Returns a list of dictionaries with the fields from the shortcut sections.
  97. The optional 'icon' and 'console' fields will be filled with their
  98. default values if not supplied.
  99. """
  100. shortcuts = {}
  101. def _check_shortcut(name, sc, section):
  102. if ('entry_point' not in sc) and ('script' not in sc):
  103. raise InvalidConfig('Section {} has neither entry_point nor script.'.format(section))
  104. elif ('entry_point' in sc) and ('script' in sc):
  105. raise InvalidConfig('Section {} has both entry_point and script.'.format(section))
  106. # Copy to a regular dict so it can hold a boolean value
  107. sc2 = dict(sc)
  108. if 'icon' not in sc2:
  109. from . import DEFAULT_ICON
  110. sc2['icon'] = DEFAULT_ICON
  111. sc2['console'] = sc.getboolean('console', fallback=False)
  112. shortcuts[name] = sc2
  113. for section in cfg.sections():
  114. if section.startswith("Shortcut "):
  115. name = section[len("Shortcut "):]
  116. _check_shortcut(name, cfg[section], section)
  117. appcfg = cfg['Application']
  118. _check_shortcut(appcfg['name'], appcfg, 'Application')
  119. return shortcuts