pvl/hosts/host.py
changeset 691 760bab5a959f
parent 689 c258e3ff6d32
child 694 d34fa1090221
equal deleted inserted replaced
690:517527835381 691:760bab5a959f
    72 def parse_dhcp_boot(boot):
    72 def parse_dhcp_boot(boot):
    73     """
    73     """
    74         Parse the dhcp boot=... option
    74         Parse the dhcp boot=... option
    75 
    75 
    76         >>> print parse_dhcp_boot(None)
    76         >>> print parse_dhcp_boot(None)
    77         {}
    77         None
    78         >>> print parse_dhcp_boot({'filename': '/foo'})
    78         >>> print parse_dhcp_boot({'filename': '/foo'})
    79         {'filename': '/foo'}
    79         {'filename': '/foo'}
    80         >>> print parse_dhcp_boot({'filename': '/foo', 'next-server': 'bar'})
    80         >>> print parse_dhcp_boot({'filename': '/foo', 'next-server': 'bar'})
    81         {'next-server': 'bar', 'filename': '/foo'}
    81         {'next-server': 'bar', 'filename': '/foo'}
    82         >>> print parse_dhcp_boot('/foo')
    82         >>> print parse_dhcp_boot('/foo')
    88         >>> print parse_dhcp_boot('foo')
    88         >>> print parse_dhcp_boot('foo')
    89         Traceback (most recent call last):
    89         Traceback (most recent call last):
    90             ...
    90             ...
    91         ValueError: invalid boot=foo
    91         ValueError: invalid boot=foo
    92     """
    92     """
    93 
    93     
       
    94     # normalize to dict
    94     if not boot:
    95     if not boot:
    95         return { }
    96         boot = { }
    96 
    97     elif not isinstance(boot, dict):
    97     elif isinstance(boot, dict):
    98         boot = { None: boot }
    98         if set(boot) <= set(('filename', 'next-server')):
    99     else:
    99             return boot
   100         boot = dict(boot)
   100         else:
   101     
   101             raise ValueError("invalid boot={boot}".format(boot=boot))
   102     # support either an instanced dict or a plain str or a mixed instanced-with-plain-str
   102 
   103     boot_str = boot.pop(None, None)
   103     elif boot.startswith('/'):
   104 
   104         return {'filename': boot}
   105     if not (set(boot) <= set(('filename', 'next-server', None))):
   105 
   106         raise ValueError("Invalid boot.*: {instances}".format(instances=' '.join(boot)))
   106     elif boot.endswith(':'):
   107 
   107         return {'next-server': boot[:-1]}
   108     # any boot= given overrides boot.* fields
   108 
   109     if not boot_str:
   109     elif ':' in boot :
   110         pass
   110         next_server, filename = boot.split(':', 1)
   111     elif boot_str.startswith('/'):
   111         return {'next-server': next_server, 'filename': filename}
   112         boot['filename'] = boot_str
       
   113 
       
   114     elif boot_str.endswith(':'):
       
   115         boot['next-server'] = boot_str[:-1]
       
   116 
       
   117     elif ':' in boot_str:
       
   118         boot['next-server'], boot['filename'] = boot_str.split(':', 1)
   112 
   119 
   113     else :
   120     else :
   114         raise ValueError("invalid boot={boot}".format(boot=boot))
   121         raise ValueError("invalid boot={boot}".format(boot=boot_str))
       
   122     
       
   123     return boot
   115 
   124 
   116 def parse_str(value):
   125 def parse_str(value):
   117     """
   126     """
   118         Normalize optional string value.
   127         Normalize optional string value.
   119     """
   128     """