pvl.hosts.config: cleanup apply_host_config
authorTero Marttila <tero.marttila@aalto.fi>
Tue, 24 Feb 2015 18:56:22 +0200
changeset 449 a19438b781d5
parent 448 5ab0ec8200c3
child 450 1d86e3909678
pvl.hosts.config: cleanup apply_host_config
pvl/hosts/config.py
pvl/hosts/tests.py
--- a/pvl/hosts/config.py	Tue Feb 24 18:55:47 2015 +0200
+++ b/pvl/hosts/config.py	Tue Feb 24 18:56:22 2015 +0200
@@ -50,7 +50,7 @@
     def __str__ (self):
         return "{self.config}:{self.error.line_number}: {self.error.message}".format(self=self)        
 
-def apply_host_expand (options, path, range, name, domain, **params):
+def apply_host_expand (options, range, name, domain, **params):
     """
         Expand a templated host item.
     """
@@ -61,13 +61,7 @@
     params = {param: pvl.dns.zone.parse_generate_field(value) for param, value in params.iteritems()}
 
     for i in range:
-        host = Host.build(name(i), domain,
-                **{param: value(i) for param, value in params.iteritems()}
-        )
-        
-        log.info("%s: [%d] %s", path, i, host)
-
-        yield host
+        yield name(i), {param: value(i) for param, value in params.iteritems()}
 
 def parse_expand(name):
     """
@@ -86,26 +80,26 @@
         # if there's a second {...} token, it will be re-composed into ${...}
         name = pre + "$" + post
         
+        # TODO: raise HostConfigError
         range = pvl.dns.zone.parse_generate_range(range)
     else:
         range = None
 
     return name, range
 
-def apply_host_config (options, path, parent, name,
+def apply_host_config (options, parent, name,
         domain=None,
-        **params
+        **config
 ):
     """
         Yield Hosts from a given config section.
 
             options     global options
-            path        config file for errors
             parent      parent filename/section containing this host item, or None.
                         used for domain
             name        name of the section (or file) containing this host item.
                         used for hostname
-            **params    host parameters
+            **config    host parameters
     """
     
     if domain:
@@ -120,20 +114,18 @@
         log.debug("%s: default domain to --hosts-domain: %s", name, options.hosts_domain)
         domain = options.hosts_domain
     else:
-        raise HostsConfigError(path, "{name}: no domain given".format(name=name))
+        raise HostConfigError(None, "{name}: no domain given".format(name=name))
     
     # expand 
     name, range = parse_expand(name)
     
     if range:
-        for host in apply_host_expand(options, path, range, name, domain, **params):
-            yield host
+        configs_iter = apply_host_expand(options, range, name, domain, **config)
     else:
-        host = Host.build(name, domain, **params)
-        
-        log.info("%s: %s", path, host)
-
-        yield host
+        configs_iter = [(name, config)]
+    
+    for name, config in configs_iter:
+        yield Host.build(name, domain, **config)
 
 def apply_host_includes (options, config_path, include):
     """
@@ -211,9 +203,17 @@
     elif parent:
         # this is a host section
         log.debug("%s: %s@%s", path, name, parent)
+        
+        try:
+            for host in apply_host_config(options, parent, name, **section):
+                log.info("%s: %s", path, host)
 
-        for host in apply_host_config(options, path, parent, name, **section):
-            yield host
+                yield host
+
+        except HostConfigError as error:
+            # add in config path
+            error.config = path
+            raise
 
     else:
         raise HostConfigError(path, "No sections in config")
--- a/pvl/hosts/tests.py	Tue Feb 24 18:55:47 2015 +0200
+++ b/pvl/hosts/tests.py	Tue Feb 24 18:56:22 2015 +0200
@@ -53,7 +53,7 @@
         self.assertHostsEqual(config.apply_hosts_file(self.options, conf_file), expected)
 
     def testApplyHostsExpand(self):
-        self.assertHostsEqual(config.apply_host_config(self.options, 'etc/hosts/asdf', 'asdf', 'asdf{1-3}', ip='10.100.100.$'), [
+        self.assertHostsEqual(config.apply_host_config(self.options, 'asdf', 'asdf{1-3}', ip='10.100.100.$'), [
                 ('asdf1@asdf', dict(ip=ipaddr.IPAddress('10.100.100.1'))),
                 ('asdf2@asdf', dict(ip=ipaddr.IPAddress('10.100.100.2'))),
                 ('asdf3@asdf', dict(ip=ipaddr.IPAddress('10.100.100.3'))),