--- a/pvl/hosts.py Mon Dec 16 21:10:30 2013 +0200
+++ b/pvl/hosts.py Mon Dec 16 21:10:40 2013 +0200
@@ -8,6 +8,7 @@
import configobj
import ipaddr
import optparse
+import os.path
def optparser (parser) :
hosts = optparse.OptionGroup(parser, "Hosts input")
@@ -101,17 +102,45 @@
def __str__ (self) :
return str(self.host)
+def apply_hosts_config (options, config, name, defaults={}) :
+ """
+ Load hosts from a ConfigObj section.
+ """
+
+ scalars = dict((scalar, config[scalar]) for scalar in config.scalars)
+ params = dict(defaults, **scalars)
+
+ if config.sections :
+ # recurse; this is a domain meta-section
+ params.setdefault('domain', name)
+
+ for section in config.sections :
+ for host in apply_hosts_config(options, config[section], section, params) :
+ yield host
+
+ elif name :
+ # this is a host section
+ for host in Host.config(options, name, **config) :
+ yield host
+
+ else :
+ raise ValueError("No sections in config")
+
+
def apply_hosts_file (options, file) :
"""
Load Hosts from a file.
+
"""
+
config = configobj.ConfigObj(file,
encoding = options.hosts_charset,
)
-
- for name in config.sections :
- for host in Host.config(options, name, **config[name]) :
- yield host
+
+ # use file basename as default
+ name = os.path.basename(file.name)
+
+ return apply_hosts_config(options, config, name)
def apply_hosts (options, files) :
"""