pvl/dhcp/config.py
changeset 476 4b792c44cf05
parent 256 63b285ceae41
child 478 517c359a683b
equal deleted inserted replaced
475:a76571e27c6f 476:4b792c44cf05
   217                 log.debug("%s", item)
   217                 log.debug("%s", item)
   218 
   218 
   219         assert not self.block
   219         assert not self.block
   220 
   220 
   221         return self.items, self.blocks
   221         return self.items, self.blocks
       
   222 
       
   223 def build_field (value):
       
   224     """
       
   225         Build a single field as part of a dhcp.conf line.
       
   226 
       
   227         >>> print build_field('foo')
       
   228         foo
       
   229         >>> print build_field('foo bar')
       
   230         "foo bar"
       
   231     """
       
   232 
       
   233     value = str(value)
       
   234 
       
   235     if any(c.isspace() for c in value):
       
   236         # quoted
       
   237         return '"{value}"'.format(value=value)
       
   238     else:
       
   239         return value
       
   240 
       
   241 def build_line (item, end, indent=0):
       
   242     """
       
   243         Build a structured line.
       
   244 
       
   245         >>> print build_line(['foo'], ';')
       
   246         foo;
       
   247         >>> print build_line(['host', 'foo'], ' {')
       
   248         host foo {
       
   249         >>> print build_line(['foo', 'bar quux'], ';', indent=1)
       
   250             foo "bar quux";
       
   251     """
       
   252 
       
   253     return '    '*indent + ' '.join(build_field(field) for field in item) + end
       
   254 
       
   255 def build_item (item, **opts):
       
   256     """
       
   257         Build a single parameter line.
       
   258 
       
   259         >>> print build_item(['foo'])
       
   260         foo;
       
   261     """
       
   262 
       
   263     return build_line(item, end=';', **opts)
       
   264 
       
   265 def build_block (block, items, blocks=(), indent=0, comment=None):
       
   266     """
       
   267         Build a complete block.
       
   268 
       
   269         >>> for line in build_block(('host', 'foo'), [('hardware', 'ethernet', '00:11:22:33:44:55')], comment="Testing"): print line
       
   270         # Testing
       
   271         host foo {
       
   272             hardware ethernet 00:11:22:33:44:55;
       
   273         }
       
   274         >>> for line in build_block(('group', ), [('next-server', 'booter')], [ \
       
   275                     (('host', 'foo'), [('hardware', 'ethernet', '00:11:22:33:44:55')], ()) \
       
   276                 ]): print line
       
   277         group {
       
   278             next-server booter;
       
   279         <BLANKLINE>
       
   280             host foo {
       
   281                 hardware ethernet 00:11:22:33:44:55;
       
   282             }
       
   283         }
       
   284     """
       
   285 
       
   286     if comment:
       
   287         yield build_line((), end="# {comment}".format(comment=comment), indent=indent)
       
   288 
       
   289     yield build_line(block, end=' {', indent=indent)
       
   290     
       
   291     for item in items:
       
   292         yield build_item(item, indent=indent+1)
       
   293 
       
   294     for subblock, subitems, subblocks in blocks:
       
   295         yield ''
       
   296 
       
   297         for line in build_block(subblock, subitems, subblocks, indent=indent+1):
       
   298             yield line
       
   299 
       
   300     yield build_line((), end='}', indent=indent)
       
   301