pvl.dhcp: support config.Field for pre-quoted values
authorTero Marttila <tero.marttila@aalto.fi>
Mon, 02 Mar 2015 19:36:39 +0200
changeset 695 c60924eca185
parent 694 d34fa1090221
child 696 55796948021e
pvl.dhcp: support config.Field for pre-quoted values
pvl/dhcp/config.py
pvl/dhcp/tests.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)
--- 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()
+        )