21 lines
888 B
Python
21 lines
888 B
Python
import sys
|
|
|
|
|
|
def check_config_value_exists(config_part, key, check_type=None, allow_empty=False, choices: list = None, default=None) -> bool:
|
|
if default and key not in config_part.keys():
|
|
return default
|
|
else:
|
|
if key not in config_part.keys():
|
|
print(f'Config key not found: "{key}"')
|
|
sys.exit(1)
|
|
if not allow_empty and config_part[key] is None or config_part[key] == '':
|
|
print(f'Config key "{key}" must not be empty.')
|
|
sys.exit(1)
|
|
if check_type and not isinstance(config_part[key], check_type):
|
|
print(f'Config key "{key}" must be type "{check_type}", not "{type(config_part[key])}".')
|
|
sys.exit(1)
|
|
if choices and config_part[key] not in choices:
|
|
print(f'Invalid choice for config key "{key}". Choices: {choices}')
|
|
sys.exit(1)
|
|
return True
|