pvl.dhcp.config: fix block stack to have .load() actually return the top-level block
authorTero Marttila <terom@paivola.fi>
Mon, 02 Mar 2015 01:29:56 +0200
changeset 682 60dbd952a15e
parent 681 3da02c7e5781
child 683 fcc67f492a02
pvl.dhcp.config: fix block stack to have .load() actually return the top-level block
pvl/dhcp/config.py
pvl/dhcp/tests.py
--- a/pvl/dhcp/config.py	Mon Mar 02 01:24:48 2015 +0200
+++ b/pvl/dhcp/config.py	Mon Mar 02 01:29:56 2015 +0200
@@ -121,7 +121,7 @@
         return ' '.join(self.key)
 
     def __repr__ (self):
-        return "Block({self.key!r}, items={self.items!r}, blocks={self.blocks!r}".format(self=self)
+        return "Block({self.key!r}, items={self.items!r}, blocks={self.blocks!r})".format(self=self)
 
 class DHCPConfigParser (object):
     """
@@ -138,7 +138,7 @@
             Parse an complete file, returning the top-level Block.
 
             >>> DHCPConfigParser.load(['foo;', 'bar {', '\tasdf "quux";', '}'], name='test')
-            Block(None, items=[('foo', )], blocks=[Block(('bar', ), items=[('asdf', 'quux')], blocks=[])])
+            Block(None, items=[('foo',)], blocks=[Block(('bar',), items=[('asdf', 'quux')], blocks=[])])
         """
         
         if name is None:
@@ -155,7 +155,10 @@
                 raise
 
         if parser.token:
-            raise DHCPConfError(parser, "Trailing data: {token}".format(token=token), line=lineno)
+            raise DHCPConfError(parser, "Trailing data: {token}".format(token=parser.token), line=lineno)
+        
+        if parser.stack:
+            raise DHCPConfError(parser, "Unterminated block: {stack}".format(stack=parser.stack), line=lineno)
 
         return parser.block 
 
@@ -218,9 +221,9 @@
 
                 log.debug("open block: %s > %s", self.block, block)
                 
+                self.stack.append(self.block)
                 self.block.blocks.append(block)
                 self.block = block
-                self.stack.append(block)
             
             # must be within block!
             elif token == 'item' :
--- a/pvl/dhcp/tests.py	Mon Mar 02 01:24:48 2015 +0200
+++ b/pvl/dhcp/tests.py	Mon Mar 02 01:29:56 2015 +0200
@@ -92,15 +92,17 @@
         hardware ethernet   00:11:22:33:44:55;
     }
 }
-        """)), (('group', ), [
-            ('next-server', 'boot.test'),
-            ('filename', "/debian/wheezy/pxelinux.0"),
-            ('include', "hosts/test.conf"),
-        ], [
-            (('host', 'foo'), [
-                ('fixed-address', '192.0.2.1'),
-                ('hardware', 'ethernet', '00:11:22:33:44:55'),
-            ], [])
+        """)), (None, [], [
+            (('group', ), [
+                ('next-server', 'boot.test'),
+                ('filename', "/debian/wheezy/pxelinux.0"),
+                ('include', "hosts/test.conf"),
+            ], [
+                (('host', 'foo'), [
+                    ('fixed-address', '192.0.2.1'),
+                    ('hardware', 'ethernet', '00:11:22:33:44:55'),
+                ], [])
+            ]),
         ]))
 
 class ConfigBuildTest(unittest.TestCase):