--- a/bin/pvl.dhcp-conf Sun Mar 01 22:26:29 2015 +0200
+++ b/bin/pvl.dhcp-conf Sun Mar 01 22:27:03 2015 +0200
@@ -6,125 +6,81 @@
Takes a conf file as input, and gives a conf file as output.
"""
+import logging; log = logging.getLogger('pvl.dhcp-conf')
+import optparse
+import os.path
import pvl.args
import pvl.dhcp.config
-import os.path
-import optparse
-import logging; log = logging.getLogger('pvl.dhcp-conf')
+def process_dhcp_items (block, items,
+ include_path = None,
+):
+ """
+ Yield items for output from given input items.
-def apply_input (options, conf) :
- """
- Parse (items, blocks) from given dhcp.conf path.
+ include_path - rewrite includes to be relative to given path.
"""
- file = pvl.args.apply_file(conf, 'r')
- return pvl.dhcp.config.DHCPConfigParser.load(file)
-
-QUOTE_ITEMS = set((
- 'filename',
-))
-
-def process_dhcp_items (options, block, items) :
- """
- Yield items for output from given input items
- """
-
- for item in items :
+ for item in items:
name, args = item[0], item[1:]
log.debug("%s: %s: %s", block, name, args)
- if name == 'include' :
+ if name == 'include':
include, = args
- if options.include_path :
- include = os.path.join(options.include_path, include)
- log.info("include: %s", include)
+ if include_path:
+ include = os.path.join(include_path, include)
- yield 'include', '"{include}"'.format(include=include)
+ log.info("%s: include: %s", block, include)
- elif name in QUOTE_ITEMS :
- yield name, ' '.join('"{arg}"'.format(arg=arg) for arg in args)
+ yield 'include', include
- else :
+ else:
yield item
-def process_dhcp_block (options, block, items, blocks) :
+def process_dhcp_block (block, **opts):
"""
- Yield block for output for given input block.
+ Return Block for output from given input Block.
"""
- log.debug("%s -> %s %s", block, items, blocks)
-
- items = list(process_dhcp_items(options, block, items))
- blocks = [process_dhcp_block(options, subblock, subitems, subblocks) for subblock, subitems, subblocks in blocks]
-
- log.debug("%s <- %s %s", block, items, blocks)
-
- return block, items, blocks
-
-def process_dhcp_conf (options, conf) :
- """
- Yield (items, blocks) for output for given input (items, blocks).
- """
- items, blocks = conf
-
- log.debug("-> %s %s", items, blocks)
-
- _, items, blocks = process_dhcp_block(options, None, items, blocks)
-
- log.debug("<- %s %s", items, blocks)
-
- return items, blocks
+ log.debug("%s <- %s %s", block, block.items, block.blocks)
-def process_output (options, items, blocks) :
- """
- Generate output lines from given (items, blocks).
- """
-
- for item in items :
- yield '\t'.join(item) + ';'
+ block = pvl.dhcp.config.Block(block.key,
+ items = list(process_dhcp_items(block, block.items, **opts)),
+ blocks = [process_dhcp_block(subblock, **opts) for subblock in block.blocks]
+ )
- for block, subitems, subblocks in blocks :
- yield ' '.join(block) + ' {'
- for line in process_output(options, subitems, subblocks) :
- yield '\t' + line
- yield '}'
+ log.debug("%s -> %s %s", block, block.items, block.blocks)
+
+ return block
-def apply_output (options, conf) :
- """
- Write output line for given (items, blocks) to --output-conf.
- """
-
- file = pvl.args.apply_file(options.output_conf, 'w')
-
- items, blocks = conf
-
- for line in process_output(options, items, blocks) :
- print >>file, line
-
-def main (argv) :
+def main (argv):
parser = optparse.OptionParser(__doc__)
parser.add_option_group(pvl.args.parser(parser))
- parser.add_option('--output-conf', metavar='FILE',
- help="Output conf to file; default stdout")
parser.add_option('--include-path', metavar='PATH',
help="Adjust includes to use given path prefix")
- options, args = parser.parse_args(argv[1:])
- pvl.args.apply(options)
-
- for conf in args :
- # input
- conf = apply_input(options, conf)
+ options, args = pvl.args.parse(parser, argv)
+
+ for file in pvl.args.apply_files(args):
+ try:
+ conf = pvl.dhcp.config.DHCPConfigParser.load(file)
+ except pvl.dhcp.config.DHCPConfigError as error:
+ log.error("%s", error)
+ return 3
# process
- conf = process_dhcp_conf(options, conf)
+ conf = process_dhcp_block(conf,
+ include_path = options.include_path,
+ )
# output
- apply_output(options, conf)
+ for line in pvl.dhcp.config.build_block(conf):
+ print line
+
+ return 0
if __name__ == '__main__':
pvl.args.main(main)