changeset 682 | 60dbd952a15e |
parent 681 | 3da02c7e5781 |
child 685 | 668f934bb958 |
681:3da02c7e5781 | 682:60dbd952a15e |
---|---|
119 |
119 |
120 def __str__ (self): |
120 def __str__ (self): |
121 return ' '.join(self.key) |
121 return ' '.join(self.key) |
122 |
122 |
123 def __repr__ (self): |
123 def __repr__ (self): |
124 return "Block({self.key!r}, items={self.items!r}, blocks={self.blocks!r}".format(self=self) |
124 return "Block({self.key!r}, items={self.items!r}, blocks={self.blocks!r})".format(self=self) |
125 |
125 |
126 class DHCPConfigParser (object): |
126 class DHCPConfigParser (object): |
127 """ |
127 """ |
128 Simple parser for ISC dhcpd conf files. |
128 Simple parser for ISC dhcpd conf files. |
129 |
129 |
136 def load (cls, file, name=None): |
136 def load (cls, file, name=None): |
137 """ |
137 """ |
138 Parse an complete file, returning the top-level Block. |
138 Parse an complete file, returning the top-level Block. |
139 |
139 |
140 >>> DHCPConfigParser.load(['foo;', 'bar {', '\tasdf "quux";', '}'], name='test') |
140 >>> DHCPConfigParser.load(['foo;', 'bar {', '\tasdf "quux";', '}'], name='test') |
141 Block(None, items=[('foo', )], blocks=[Block(('bar', ), items=[('asdf', 'quux')], blocks=[])]) |
141 Block(None, items=[('foo',)], blocks=[Block(('bar',), items=[('asdf', 'quux')], blocks=[])]) |
142 """ |
142 """ |
143 |
143 |
144 if name is None: |
144 if name is None: |
145 name = file.name |
145 name = file.name |
146 |
146 |
153 except DHCPConfigError as error: |
153 except DHCPConfigError as error: |
154 error.line = lineno |
154 error.line = lineno |
155 raise |
155 raise |
156 |
156 |
157 if parser.token: |
157 if parser.token: |
158 raise DHCPConfError(parser, "Trailing data: {token}".format(token=token), line=lineno) |
158 raise DHCPConfError(parser, "Trailing data: {token}".format(token=parser.token), line=lineno) |
159 |
|
160 if parser.stack: |
|
161 raise DHCPConfError(parser, "Unterminated block: {stack}".format(stack=parser.stack), line=lineno) |
|
159 |
162 |
160 return parser.block |
163 return parser.block |
161 |
164 |
162 def __init__ (self, name=None): |
165 def __init__ (self, name=None): |
163 self.name = name |
166 self.name = name |
216 if token == 'open': |
219 if token == 'open': |
217 block = Block(args) |
220 block = Block(args) |
218 |
221 |
219 log.debug("open block: %s > %s", self.block, block) |
222 log.debug("open block: %s > %s", self.block, block) |
220 |
223 |
224 self.stack.append(self.block) |
|
221 self.block.blocks.append(block) |
225 self.block.blocks.append(block) |
222 self.block = block |
226 self.block = block |
223 self.stack.append(block) |
|
224 |
227 |
225 # must be within block! |
228 # must be within block! |
226 elif token == 'item' : |
229 elif token == 'item' : |
227 log.debug("block %s item: %s", self.block, args) |
230 log.debug("block %s item: %s", self.block, args) |
228 |
231 |