pvl.hosts-import: ugly indexed foo.X keys for multi-value fields..
authorTero Marttila <terom@paivola.fi>
Mon, 16 Dec 2013 18:36:37 +0200
changeset 268 560ba0544254
parent 267 8c0c1b6e6aff
child 269 713d0495288e
pvl.hosts-import: ugly indexed foo.X keys for multi-value fields..
bin/pvl.hosts-import
--- a/bin/pvl.hosts-import	Mon Dec 16 18:36:09 2013 +0200
+++ b/bin/pvl.hosts-import	Mon Dec 16 18:36:37 2013 +0200
@@ -1,10 +1,10 @@
 #!/usr/bin/env python
 
 """
-    Manipulate host definitions for dns/dhcp.
+    Import hosts from existing BIND or dhcpd files.
 """
 
-import pvl.args, optparse
+import pvl.args
 import pvl.dns.zone
 import pvl.dhcp.config
 import pvl.ldap.args
@@ -143,7 +143,7 @@
         hostname, domain = fixed_address.split('.', 1)
 
     #if suffix :
-    #    yield hostname, 'ethernet:{suffix}'.format(suffix=suffix), ethernet
+    #    yield hostname, ('ethernet', suffix), ethernet
     if hostname and ethernet :
         yield hostname, 'ethernet', ethernet
     else :
@@ -308,18 +308,18 @@
         
         log.info("%s: %s (%s)", host, owner, comment)
         
-        yield 'owner-comment', comment
+        yield 'comment-owner', comment
         yield 'owner', owner,
 
     else :
         log.warn("%s: unknown owner: %s", host, info)
-        yield 'comment', "owner: {group} / {owner}".format(
+        yield 'comment-owner', "{group} / {owner}".format(
                 group   = info.get('group', ''),
                 owner   = info.get('owner', ''),
         )
     
     if info.get('host') :
-        yield 'comment', info['host']
+        yield 'comment-host', info['host']
 
 def process_hosts_comments (options, import_hosts) :
     """
@@ -354,12 +354,12 @@
 
     if options.import_zone_hosts:
         for info in import_zone_hosts(options,
-                pvl.args.apply_file(options.import_zone_hosts)) :
+                pvl.args.apply_file(options.import_zone_hosts, 'r', options.input_charset)) :
             yield info
     
     if options.import_dhcp_hosts:
         for info in import_dhcp_conf(options,
-                pvl.args.apply_file(options.import_dhcp_hosts)) :
+                pvl.args.apply_file(options.import_dhcp_hosts, 'r', options.input_charset)) :
             yield info
        
 def import_hosts (options) :
@@ -389,7 +389,7 @@
         
         # sort by IP
         if ip :
-            sort = ip = ip[0]
+            sort = ip[0]
         else :
             # fake, to sort correctly
             sort = ipaddr.IPAddress(0)
@@ -412,7 +412,7 @@
     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', ()) :
+        for comment in fields.get('comment-host', ()):
             print >>file, u"# {comment}".format(comment=comment)
 
         print >>file, u"[{host}]".format(host=host)
@@ -420,12 +420,23 @@
         for field, fmt in (
                 ('ip',              None),
                 ('ethernet',        None),
-                ('owner',           u"\t{field:15} = {value} # {fields[owner-comment][0]}"),
+                ('owner',           u"\t{field:15} = {value} # {fields[comment-owner][0]}"),
+                ('alias',           None),
         ) :
             if not fmt :
                 fmt = u"\t{field:15} = {value}"
+            
+            values = fields.get(field, ())
 
-            for value in fields.get(field, ()) :
+            if len(values) > 1 :
+                for index, value in enumerate(values, 1) :
+                    print >>file, 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