grub: Prettier type checking when writing grub defaults

This commit is contained in:
Anders Ingemann 2016-06-05 13:20:34 +02:00
parent e030b9f84f
commit 2ffc4dd6c3

View file

@ -200,25 +200,19 @@ class WriteGrubConfig(Task):
def run(cls, info): def run(cls, info):
grub_config_contents = '' grub_config_contents = ''
for key, value in info.grub_config.items(): for key, value in info.grub_config.items():
if value is None:
continue
if isinstance(value, str): if isinstance(value, str):
grub_config_contents += '{}="{}"\n'.format(key, value) grub_config_contents += '{}="{}"\n'.format(key, value)
continue elif isinstance(value, int):
if isinstance(value, int):
grub_config_contents += '{}={}\n'.format(key, value) grub_config_contents += '{}={}\n'.format(key, value)
continue elif isinstance(value, bool):
if isinstance(value, bool):
grub_config_contents += '{}="{}"\n'.format(key, str(value).lower()) grub_config_contents += '{}="{}"\n'.format(key, str(value).lower())
continue elif isinstance(value, list):
if isinstance(value, list): if len(value) > 0:
if len(value) == 0: args_list = ' '.join(map(str, value))
continue grub_config_contents += '{}="{}"\n'.format(key, args_list)
args_list = ' '.join(map(str, value)) elif value is not None:
grub_config_contents += '{}="{}"\n'.format(key, args_list) raise TaskError('Don\'t know how to handle type {}, '
continue 'when creating grub config'.format(type(value)))
raise TaskError('Don\'t know how to handle type {}, '
'when creating grub config'.format(type(value)))
grub_defaults = os.path.join(info.root, 'etc/default/grub') grub_defaults = os.path.join(info.root, 'etc/default/grub')
with open(grub_defaults, 'w') as grub_defaults_handle: with open(grub_defaults, 'w') as grub_defaults_handle:
grub_defaults_handle.write(grub_config_contents) grub_defaults_handle.write(grub_config_contents)