bin/pvl.dns-zone
changeset 258 1ad9cec4f556
parent 252 0ea4450fdd40
child 293 6351acf3eb3b
equal deleted inserted replaced
257:a6964f9bda8b 258:1ad9cec4f556
     1 #!/usr/bin/env python
     1 #!/usr/bin/env python
     2 
     2 
     3 """
     3 """
     4     Process bind zonefiles.
     4     Process bind zonefiles.
       
     5 
       
     6     Takes a zonefile as input, and gives a zonefile as output.
     5 """
     7 """
     6 
     8 
     7 import codecs
       
     8 import optparse
     9 import optparse
     9 
    10 
    10 import pvl.args
    11 import pvl.args
    11 import pvl.dns.zone
    12 import pvl.dns.zone
    12 from pvl.dns import __version__
    13 from pvl.dns import __version__
    13 from pvl.dns.zone import ZoneRecord, reverse_ipv4, reverse_ipv6, fqdn
    14 from pvl.dns.zone import ZoneRecord, reverse_ipv4, reverse_ipv6, fqdn
    14 
    15 
    15 import logging; log = logging.getLogger('main')
    16 import logging; log = logging.getLogger('main')
    16 
    17 
    17 
       
    18 def parse_options (argv) :
    18 def parse_options (argv) :
    19     """
    19     """
    20         Parse command-line arguments.
    20         Parse command-line arguments.
    21     """
    21     """
    22 
    22 
    36 
    36 
    37     # input/output
    37     # input/output
    38     parser.add_option('-c', '--input-charset',  metavar='CHARSET',  default='utf-8', 
    38     parser.add_option('-c', '--input-charset',  metavar='CHARSET',  default='utf-8', 
    39             help="Encoding used for input files")
    39             help="Encoding used for input files")
    40 
    40 
    41     parser.add_option('-o', '--output',         metavar='FILE',     default='-',
    41     parser.add_option('-o', '--output',         metavar='FILE',     default=None,
    42             help="Write to output file; default stdout")
    42             help="Write to output file; default stdout")
    43 
    43 
    44     parser.add_option('--output-charset',       metavar='CHARSET',  default='utf-8', 
    44     parser.add_option('--output-charset',       metavar='CHARSET',  default='utf-8', 
    45             help="Encoding used for output files")
    45             help="Encoding used for output files")
    46 
    46 
   241 def write_zone_records (file, zone) :
   241 def write_zone_records (file, zone) :
   242     for r in zone :
   242     for r in zone :
   243         file.write(unicode(r))
   243         file.write(unicode(r))
   244         file.write('\n')
   244         file.write('\n')
   245 
   245 
   246 def open_file (path, mode, charset) :
       
   247     """
       
   248         Open unicode-enabled file from path, with - using stdio.
       
   249     """
       
   250 
       
   251     if path == '-' :
       
   252         # use stdin/out based on mode
       
   253         stream, func = {
       
   254             'r':    (sys.stdin, codecs.getreader),
       
   255             'w':    (sys.stdout, codecs.getwriter),
       
   256         }[mode[0]]
       
   257 
       
   258         # wrap
       
   259         return func(charset)(stream)
       
   260 
       
   261     else :
       
   262         # open
       
   263         return codecs.open(path, mode, charset)
       
   264 
       
   265 def main (argv) :
   246 def main (argv) :
   266     options, args = parse_options(argv)
   247     options, args = parse_options(argv)
   267 
   248     
   268     if args :
   249     # open files, default to stdout
   269         # open files
   250     input_files = pvl.args.apply_files(args, 'r', options.input_charset)
   270         input_files = [open_file(path, 'r', options.input_charset) for path in args]
       
   271 
       
   272     else :
       
   273         # default to stdout
       
   274         input_files = [open_file('-', 'r', options.input_charset)]
       
   275    
   251    
   276     # process zone data
   252     # process zone data
   277     zone = []
   253     zone = []
   278 
   254 
   279     for file in input_files :
   255     for file in input_files :