bin/pvl.hosts-import
changeset 285 e18aaec99e54
parent 281 b236f689ba22
child 288 2f2f92e4c58e
--- a/bin/pvl.hosts-import	Mon Dec 16 20:50:10 2013 +0200
+++ b/bin/pvl.hosts-import	Mon Dec 16 21:10:20 2013 +0200
@@ -113,7 +113,8 @@
                 yield rr.name, 'comment', rr.comment
 
             if rr.origin :
-                yield rr.name, 'domain', rr.origin
+                # not a fqdn
+                yield rr.name, 'domain', rr.origin.rstrip('.')
 
         elif rr.type == 'CNAME' :
             host, = rr.data
@@ -445,45 +446,58 @@
 
 def export_hosts (options, hosts) :
     """
-        Export hosts to file.
+        Generate hosts config lines for given hosts.
     """
 
-    file = pvl.args.apply_file(options.output_hosts, 'w', options.output_charset)
+    FMT = u"\t{field:15} = {value}"
+
+    yield u"[{domain}]".format(domain=options.hosts_domain)
 
     # filter + sort
     hosts = [(host, fields) for sort, host, fields in sorted(process_export_hosts(options, hosts))]
 
     for host, fields in hosts :
         for comment in fields.get('comment-host', ()):
-            print >>file, u"# {comment}".format(comment=comment)
+            yield u"# {comment}".format(comment=comment)
 
-        print >>file, u"[{host}]".format(host=host)
+        yield u"[[{host}]]".format(host=host)
         
+        for domain in fields.get('domain', ()) :
+            if domain != options.hosts_domain :
+                yield FMT.format(field='domain', value=domain)
+
         for field, fmt in (
-                ('ip',              None),
-                ('ip6',             None),
-                ('ethernet',        None),
+                ('ip',              FMT),
+                ('ip6',             FMT),
+                ('ethernet',        FMT),
                 ('owner',           u"\t{field:15} = {value} # {fields[comment-owner][0]}"),
-                ('alias',           None),
-                ('boot',            None),
+                ('alias',           FMT),
+                ('boot',            FMT),
         ) :
-            if not fmt :
-                fmt = u"\t{field:15} = {value}"
-            
             values = fields.get(field, ())
 
             if len(values) > 1 :
                 for index, value in enumerate(values, 1) :
-                    print >>file, fmt.format(
+                    yield fmt.format(
                             field   = "{field}.{index}".format(field=field, index=index),
                             value   = value,
                             fields  = fields
                     )
             elif len(values) > 0 :
                 value, = values
-                print >>file, fmt.format(field=field, value=value, fields=fields)
-        
-        print >>file
+                yield fmt.format(field=field, value=value, fields=fields)
+    
+        yield ""
+   
+def apply_hosts_export (options, hosts) :
+    """
+        Export hosts to file.
+    """
+
+    file = pvl.args.apply_file(options.output_hosts, 'w', options.output_charset)
+
+    for line in export_hosts(options, hosts) :
+        print >>file, line
 
 def main (argv) :
     options, args = parse_options(argv)
@@ -499,7 +513,7 @@
    
     # output
     if options.output_hosts :
-        export_hosts(options, hosts)
+        apply_hosts_export(options, hosts)
 
 if __name__ == '__main__':
     pvl.args.main(main)