# HG changeset patch # User Tero Marttila # Date 1424796982 -7200 # Node ID a19438b781d51f4c767d5b8fecba8d7f0daa0c6f # Parent 5ab0ec8200c3fb50899f662b49305daa37d0d9b4 pvl.hosts.config: cleanup apply_host_config diff -r 5ab0ec8200c3 -r a19438b781d5 pvl/hosts/config.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") diff -r 5ab0ec8200c3 -r a19438b781d5 pvl/hosts/tests.py --- 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'))),