# HG changeset patch # User Tero Marttila # Date 1425317799 -7200 # Node ID c60924eca18520183b4b064c27d284813406b458 # Parent d34fa1090221b70819db3293c13282d4d63875ba pvl.dhcp: support config.Field for pre-quoted values diff -r d34fa1090221 -r c60924eca185 pvl/dhcp/config.py --- a/pvl/dhcp/config.py Mon Mar 02 19:36:10 2015 +0200 +++ b/pvl/dhcp/config.py Mon Mar 02 19:36:39 2015 +0200 @@ -68,6 +68,17 @@ yield item +class Field (object): + """ + Pre-quoted fields for use in DHCP confs. + """ + + def __init__(self, token): + self.token = token + + def __str__(self): + return self.token + def quote (value, context=None): """ Build a single field as part of a dhcp.conf line. @@ -90,9 +101,13 @@ 192.0.2.1 >>> print quote('00:11:22:33:44:55', context=('hardware', 'ethernet')) 00:11:22:33:44:55 + >>> print quote(Field('1:00:11:22:33:44:55')) + 1:00:11:22:33:44:55 """ - if isinstance(value, int): + if isinstance(value, Field): + return str(value) + elif isinstance(value, int): return str(value) elif context in UNQUOTED_CONTEXT: return str(value) diff -r d34fa1090221 -r c60924eca185 pvl/dhcp/tests.py --- a/pvl/dhcp/tests.py Mon Mar 02 19:36:10 2015 +0200 +++ b/pvl/dhcp/tests.py Mon Mar 02 19:36:39 2015 +0200 @@ -146,3 +146,25 @@ } """.strip().splitlines() ) + + def testBuildConfItem(self): + self.assertLinesEqual( + config.build_block(config.Block(None, [], [ + config.Block(('host', 'foo'), [ + ('option', 'host-name', "foo.test"), + ('fixed-address', '192.0.2.1'), + ('hardware', 'ethernet', '00:11:22:33:44:55'), + ]), + config.Block(None, [ + ('subclass', 'debian', config.Field('1:00:11:22:33:44:55')), + ]), + ])), + """ +host foo { + option host-name "foo.test"; + fixed-address 192.0.2.1; + hardware ethernet 00:11:22:33:44:55; +} +subclass debian 1:00:11:22:33:44:55; + """.strip().splitlines() + )