pvl/hosts/host.py
changeset 689 c258e3ff6d32
parent 503 a56456f901e8
child 691 760bab5a959f
--- a/pvl/hosts/host.py	Mon Mar 02 13:30:15 2015 +0200
+++ b/pvl/hosts/host.py	Mon Mar 02 17:58:24 2015 +0200
@@ -69,6 +69,50 @@
 
     return ':'.join('%02x' % int(x, 16) for x in value.split(':'))
 
+def parse_dhcp_boot(boot):
+    """
+        Parse the dhcp boot=... option
+
+        >>> print parse_dhcp_boot(None)
+        {}
+        >>> print parse_dhcp_boot({'filename': '/foo'})
+        {'filename': '/foo'}
+        >>> print parse_dhcp_boot({'filename': '/foo', 'next-server': 'bar'})
+        {'next-server': 'bar', 'filename': '/foo'}
+        >>> print parse_dhcp_boot('/foo')
+        {'filename': '/foo'}
+        >>> print parse_dhcp_boot('bar:/foo')
+        {'next-server': 'bar', 'filename': '/foo'}
+        >>> print parse_dhcp_boot('bar:')
+        {'next-server': 'bar'}
+        >>> print parse_dhcp_boot('foo')
+        Traceback (most recent call last):
+            ...
+        ValueError: invalid boot=foo
+    """
+
+    if not boot:
+        return { }
+
+    elif isinstance(boot, dict):
+        if set(boot) <= set(('filename', 'next-server')):
+            return boot
+        else:
+            raise ValueError("invalid boot={boot}".format(boot=boot))
+
+    elif boot.startswith('/'):
+        return {'filename': boot}
+
+    elif boot.endswith(':'):
+        return {'next-server': boot[:-1]}
+
+    elif ':' in boot :
+        next_server, filename = boot.split(':', 1)
+        return {'next-server': next_server, 'filename': filename}
+
+    else :
+        raise ValueError("invalid boot={boot}".format(boot=boot))
+
 def parse_str(value):
     """
         Normalize optional string value.
@@ -133,7 +177,7 @@
                 forward     = parse_str(forward),
                 reverse     = parse_str(reverse),
                 down        = parse_bool(down),
-                boot        = boot,
+                boot        = parse_dhcp_boot(boot),
                 extensions  = extensions,
         )