pvl.dns-zone: use pvl.args.apply_files
authorTero Marttila <terom@paivola.fi>
Mon, 16 Dec 2013 11:41:37 +0200
changeset 258 1ad9cec4f556
parent 257 a6964f9bda8b
child 259 65b483fb862c
pvl.dns-zone: use pvl.args.apply_files
bin/pvl.dns-zone
--- a/bin/pvl.dns-zone	Mon Dec 16 11:41:09 2013 +0200
+++ b/bin/pvl.dns-zone	Mon Dec 16 11:41:37 2013 +0200
@@ -2,9 +2,10 @@
 
 """
     Process bind zonefiles.
+
+    Takes a zonefile as input, and gives a zonefile as output.
 """
 
-import codecs
 import optparse
 
 import pvl.args
@@ -14,7 +15,6 @@
 
 import logging; log = logging.getLogger('main')
 
-
 def parse_options (argv) :
     """
         Parse command-line arguments.
@@ -38,7 +38,7 @@
     parser.add_option('-c', '--input-charset',  metavar='CHARSET',  default='utf-8', 
             help="Encoding used for input files")
 
-    parser.add_option('-o', '--output',         metavar='FILE',     default='-',
+    parser.add_option('-o', '--output',         metavar='FILE',     default=None,
             help="Write to output file; default stdout")
 
     parser.add_option('--output-charset',       metavar='CHARSET',  default='utf-8', 
@@ -243,35 +243,11 @@
         file.write(unicode(r))
         file.write('\n')
 
-def open_file (path, mode, charset) :
-    """
-        Open unicode-enabled file from path, with - using stdio.
-    """
-
-    if path == '-' :
-        # use stdin/out based on mode
-        stream, func = {
-            'r':    (sys.stdin, codecs.getreader),
-            'w':    (sys.stdout, codecs.getwriter),
-        }[mode[0]]
-
-        # wrap
-        return func(charset)(stream)
-
-    else :
-        # open
-        return codecs.open(path, mode, charset)
-
 def main (argv) :
     options, args = parse_options(argv)
-
-    if args :
-        # open files
-        input_files = [open_file(path, 'r', options.input_charset) for path in args]
-
-    else :
-        # default to stdout
-        input_files = [open_file('-', 'r', options.input_charset)]
+    
+    # open files, default to stdout
+    input_files = pvl.args.apply_files(args, 'r', options.input_charset)
    
     # process zone data
     zone = []