Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_err_header_invalid_type(self):
exception = includehandler.LoadConfigException
testvector = [
('header:', exception),
('header: 1', exception),
('header: a', exception),
('header: []', exception),
]
self.util_exception_content(testvector)
def load_config(filename):
"""
Load the configuration file and test if version is supported.
"""
(_, ext) = os.path.splitext(filename)
config = None
if ext == '.json':
import json
with open(filename, 'rb') as fds:
config = json.load(fds)
elif ext == '.yml':
import yaml
with open(filename, 'rb') as fds:
config = yaml.safe_load(fds)
else:
raise LoadConfigException('Config file extension not recognized',
filename)
validator = Draft4Validator(CONFIGSCHEMA)
validation_error = False
for error in validator.iter_errors(config):
validation_error = True
logging.error('Config file validation Error:\n%s', error)
if validation_error:
raise LoadConfigException('Error(s) occured while validating the '
'config file', filename)
try:
version_value = int(config['header']['version'])
except ValueError:
if validation_error:
raise LoadConfigException('Error(s) occured while validating the '
'config file', filename)
try:
version_value = int(config['header']['version'])
except ValueError:
# Be compatible: version string '0.10' is equivalent to file version 1
# This check is already done in the config schema so here just set the
# right version
version_value = 1
if version_value < __compatible_file_version__ or \
version_value > __file_version__:
raise LoadConfigException('This version of kas is compatible with '
'version {} to {}, file has version {}'
.format(__compatible_file_version__,
__file_version__, version_value),
filename)
return config
import yaml
with open(filename, 'rb') as fds:
config = yaml.safe_load(fds)
else:
raise LoadConfigException('Config file extension not recognized',
filename)
validator = Draft4Validator(CONFIGSCHEMA)
validation_error = False
for error in validator.iter_errors(config):
validation_error = True
logging.error('Config file validation Error:\n%s', error)
if validation_error:
raise LoadConfigException('Error(s) occured while validating the '
'config file', filename)
try:
version_value = int(config['header']['version'])
except ValueError:
# Be compatible: version string '0.10' is equivalent to file version 1
# This check is already done in the config schema so here just set the
# right version
version_value = 1
if version_value < __compatible_file_version__ or \
version_value > __file_version__:
raise LoadConfigException('This version of kas is compatible with '
'version {} to {}, file has version {}'
.format(__compatible_file_version__,
__file_version__, version_value),