pvl.hosts: allow boot=... fields to override boot.*=...
--- a/pvl/hosts/host.py Mon Mar 02 18:08:30 2015 +0200
+++ b/pvl/hosts/host.py Mon Mar 02 18:17:47 2015 +0200
@@ -74,7 +74,7 @@
Parse the dhcp boot=... option
>>> print parse_dhcp_boot(None)
- {}
+ None
>>> print parse_dhcp_boot({'filename': '/foo'})
{'filename': '/foo'}
>>> print parse_dhcp_boot({'filename': '/foo', 'next-server': 'bar'})
@@ -90,28 +90,37 @@
...
ValueError: invalid boot=foo
"""
-
+
+ # normalize to dict
if not boot:
- return { }
+ boot = { }
+ elif not isinstance(boot, dict):
+ boot = { None: boot }
+ else:
+ boot = dict(boot)
+
+ # support either an instanced dict or a plain str or a mixed instanced-with-plain-str
+ boot_str = boot.pop(None, None)
- elif isinstance(boot, dict):
- if set(boot) <= set(('filename', 'next-server')):
- return boot
- else:
- raise ValueError("invalid boot={boot}".format(boot=boot))
+ if not (set(boot) <= set(('filename', 'next-server', None))):
+ raise ValueError("Invalid boot.*: {instances}".format(instances=' '.join(boot)))
- elif boot.startswith('/'):
- return {'filename': boot}
+ # any boot= given overrides boot.* fields
+ if not boot_str:
+ pass
+ elif boot_str.startswith('/'):
+ boot['filename'] = boot_str
- elif boot.endswith(':'):
- return {'next-server': boot[:-1]}
+ elif boot_str.endswith(':'):
+ boot['next-server'] = boot_str[:-1]
- elif ':' in boot :
- next_server, filename = boot.split(':', 1)
- return {'next-server': next_server, 'filename': filename}
+ elif ':' in boot_str:
+ boot['next-server'], boot['filename'] = boot_str.split(':', 1)
else :
- raise ValueError("invalid boot={boot}".format(boot=boot))
+ raise ValueError("invalid boot={boot}".format(boot=boot_str))
+
+ return boot
def parse_str(value):
"""