|
@@ -3,20 +3,12 @@
|
|
import configparser
|
|
import configparser
|
|
|
|
|
|
class SectionValidator(object):
|
|
class SectionValidator(object):
|
|
- def __init__(self, name, subsections, identification_func=None):
|
|
|
|
|
|
+ def __init__(self, subsections):
|
|
"""
|
|
"""
|
|
- name
|
|
|
|
- a descriptive name of the section
|
|
|
|
subsections
|
|
subsections
|
|
list of tuples containing the names and whether the
|
|
list of tuples containing the names and whether the
|
|
subsection is mandatory
|
|
subsection is mandatory
|
|
- identy_func
|
|
|
|
- returns True if this is the correct Validator for the
|
|
|
|
- section name. If it is left out the descriptive name will be
|
|
|
|
- used to identify the section
|
|
|
|
"""
|
|
"""
|
|
- self.name = name
|
|
|
|
- self.identification_func = identification_func
|
|
|
|
self.subsections = subsections
|
|
self.subsections = subsections
|
|
|
|
|
|
def check(self, config, section_name):
|
|
def check(self, config, section_name):
|
|
@@ -26,20 +18,8 @@ class SectionValidator(object):
|
|
|
|
|
|
raises InvalidConfig if something inside the section is wrong
|
|
raises InvalidConfig if something inside the section is wrong
|
|
"""
|
|
"""
|
|
- was_identified = False
|
|
|
|
- if self._identify(section_name):
|
|
|
|
- was_identified = True
|
|
|
|
- self._check_mandatory_fields(section_name, config[section_name])
|
|
|
|
- self._check_invalid_subsections(section_name, config[section_name])
|
|
|
|
- return was_identified
|
|
|
|
-
|
|
|
|
- def _identify(self, section_name):
|
|
|
|
- # if no identification function is set, just compare the section
|
|
|
|
- # name with the name of the validator
|
|
|
|
- if self.identification_func is None:
|
|
|
|
- return section_name == self.name
|
|
|
|
- else:
|
|
|
|
- return self.identification_func(section_name)
|
|
|
|
|
|
+ self._check_mandatory_fields(section_name, config[section_name])
|
|
|
|
+ self._check_invalid_subsections(section_name, config[section_name])
|
|
|
|
|
|
def _check_mandatory_fields(self, section_name, subsection):
|
|
def _check_mandatory_fields(self, section_name, subsection):
|
|
for subsection_name, mandatory in self.subsections:
|
|
for subsection_name, mandatory in self.subsections:
|
|
@@ -54,7 +34,6 @@ class SectionValidator(object):
|
|
raise InvalidConfig(err_msg)
|
|
raise InvalidConfig(err_msg)
|
|
|
|
|
|
def _check_invalid_subsections(self, section_name, section):
|
|
def _check_invalid_subsections(self, section_name, section):
|
|
- # check subsection names
|
|
|
|
for subsection in section:
|
|
for subsection in section:
|
|
subsection_name = str(subsection)
|
|
subsection_name = str(subsection)
|
|
valid_subsection_names = [s[0] for s in self.subsections]
|
|
valid_subsection_names = [s[0] for s in self.subsections]
|
|
@@ -70,46 +49,35 @@ class SectionValidator(object):
|
|
# contains all configuration sections and subsections
|
|
# contains all configuration sections and subsections
|
|
# the subsections are a tuple with their name and a boolean, which
|
|
# the subsections are a tuple with their name and a boolean, which
|
|
# tells us whether the option is mandatory
|
|
# tells us whether the option is mandatory
|
|
-CONFIG_VALIDATORS = [
|
|
|
|
- SectionValidator('Application',
|
|
|
|
- [
|
|
|
|
- ('name', True),
|
|
|
|
- ('version', True),
|
|
|
|
- ('entry_point', True),
|
|
|
|
- ('script', False),
|
|
|
|
- ('icon', False),
|
|
|
|
- ('console', False),
|
|
|
|
- ],
|
|
|
|
- ),
|
|
|
|
- SectionValidator('Build',
|
|
|
|
- [
|
|
|
|
- ('directory', False),
|
|
|
|
- ('installer_name', False),
|
|
|
|
- ('nsi_template', False),
|
|
|
|
- ]
|
|
|
|
- ),
|
|
|
|
- SectionValidator('Include',
|
|
|
|
- [
|
|
|
|
- ('packages', False),
|
|
|
|
- ('files', False),
|
|
|
|
- ]
|
|
|
|
- ),
|
|
|
|
- SectionValidator('Python',
|
|
|
|
- [
|
|
|
|
- ('version', True),
|
|
|
|
- ('bitness', False),
|
|
|
|
- ]
|
|
|
|
- ),
|
|
|
|
- SectionValidator('Shortcut ... ',
|
|
|
|
- [
|
|
|
|
- ('entry_point', True),
|
|
|
|
- ('script', False),
|
|
|
|
- ('icon', False),
|
|
|
|
- ('console', False),
|
|
|
|
- ],
|
|
|
|
- lambda s: s.startswith('Shortcut ')
|
|
|
|
- ),
|
|
|
|
-]
|
|
|
|
|
|
+CONFIG_VALIDATORS = {
|
|
|
|
+ 'Application': SectionValidator([
|
|
|
|
+ ('name', True),
|
|
|
|
+ ('version', True),
|
|
|
|
+ ('entry_point', True),
|
|
|
|
+ ('script', False),
|
|
|
|
+ ('icon', False),
|
|
|
|
+ ('console', False),
|
|
|
|
+ ]),
|
|
|
|
+ 'Build': SectionValidator([
|
|
|
|
+ ('directory', False),
|
|
|
|
+ ('installer_name', False),
|
|
|
|
+ ('nsi_template', False),
|
|
|
|
+ ]),
|
|
|
|
+ 'Include': SectionValidator([
|
|
|
|
+ ('packages', False),
|
|
|
|
+ ('files', False),
|
|
|
|
+ ]),
|
|
|
|
+ 'Python': SectionValidator([
|
|
|
|
+ ('version', True),
|
|
|
|
+ ('bitness', False),
|
|
|
|
+ ]),
|
|
|
|
+ 'Shortcut': SectionValidator([
|
|
|
|
+ ('entry_point', True),
|
|
|
|
+ ('script', False),
|
|
|
|
+ ('icon', False),
|
|
|
|
+ ('console', False),
|
|
|
|
+ ]),
|
|
|
|
+}
|
|
|
|
|
|
class InvalidConfig(ValueError):
|
|
class InvalidConfig(ValueError):
|
|
pass
|
|
pass
|
|
@@ -118,17 +86,12 @@ def read_and_validate(config_file):
|
|
config = configparser.ConfigParser()
|
|
config = configparser.ConfigParser()
|
|
config.read(config_file)
|
|
config.read(config_file)
|
|
for section in config.sections():
|
|
for section in config.sections():
|
|
- # each section *must* be identified by at least one validator
|
|
|
|
- # otherwise it is an invalid section (e.g. has an invalid name)
|
|
|
|
- was_identified = False
|
|
|
|
- for validator in CONFIG_VALIDATORS:
|
|
|
|
- was_identified = validator.check(config, section)
|
|
|
|
- # if we already found the correct validator and it did not
|
|
|
|
- # raise InvalidConfig, we can skip the other validators
|
|
|
|
- if was_identified:
|
|
|
|
- break
|
|
|
|
- if not was_identified:
|
|
|
|
- valid_section_names = [v.name for v in CONFIG_VALIDATORS]
|
|
|
|
|
|
+ if section in CONFIG_VALIDATORS:
|
|
|
|
+ CONFIG_VALIDATORS[section].check(config, section)
|
|
|
|
+ elif section.startswith('Shortcut '):
|
|
|
|
+ CONFIG_VALIDATORS['Shortcut'].check(config, section)
|
|
|
|
+ else:
|
|
|
|
+ valid_section_names = CONFIG_VALIDATORS.keys()
|
|
err_msg = ("{0} is not a valid section header. Must "
|
|
err_msg = ("{0} is not a valid section header. Must "
|
|
"be one of these: {1}").format(
|
|
"be one of these: {1}").format(
|
|
section,
|
|
section,
|