# HG changeset patch # User Tero Marttila # Date 1387728548 -7200 # Node ID 7d2601368142a936199cfd58c81713fe92034347 # Parent bb8a18cffe8a4353d6206f7c0583ea1d278354fc pvl.hosts: implement support for domain-level include=... diff -r bb8a18cffe8a -r 7d2601368142 pvl/hosts.py --- a/pvl/hosts.py Sun Dec 22 18:08:48 2013 +0200 +++ b/pvl/hosts.py Sun Dec 22 18:09:08 2013 +0200 @@ -199,7 +199,33 @@ def __str__ (self) : return str(self.host) -def apply_hosts_config (options, config, name, defaults={}, parent=None) : +def apply_host_includes (options, config_path, include) : + """ + Yield files from a given config's include=... value + """ + + include_path = os.path.dirname(config_path) + + for include in include.split() : + path = os.path.join(include_path, include) + + if include.endswith('/') : + for name in os.listdir(path) : + file_path = os.path.join(path, name) + + if name.startswith('.') : + pass + elif not os.path.exists(file_path) : + log.warn("%s: skip nonexistant include: %s", config_path, file_path) + else : + log.info("%s: include: %s", config_path, file_path) + yield open(file_path) + + else : + log.info("%s: include: %s", config_path, path) + yield open(path) + +def apply_hosts_config (options, path, name, config, defaults={}, parent=None) : """ Load hosts from a ConfigObj section. """ @@ -207,17 +233,27 @@ scalars = dict((scalar, config[scalar]) for scalar in config.scalars) if config.sections : + # includes? + for file in apply_host_includes(options, path, scalars.pop('include', '')) : + # within our domain context + for host in apply_hosts_file(options, file, parent=name) : + yield host + # recurse; this is a domain meta-section params = dict(defaults) params.update(**scalars) # override + + log.debug("%s: %s -> %s", path, parent, name) for section in config.sections : - for host in apply_hosts_config(options, config[section], section, params, name) : + for host in apply_hosts_config(options, path, section, config[section], params, name) : yield host elif name : params = dict(defaults, **scalars) + log.debug("%s: %s: %s", path, parent, name) + # this is a host section for host in Host.config(options, name, parent, **params) : yield host @@ -226,7 +262,7 @@ raise ValueError("No sections in config") -def apply_hosts_file (options, file) : +def apply_hosts_file (options, file, parent=None) : """ Load Hosts from a file. @@ -237,9 +273,10 @@ ) # use file basename as default - name = os.path.basename(file.name) + path = file.name + name = os.path.basename(path) - return apply_hosts_config(options, config, name) + return apply_hosts_config(options, path, name, config, parent=parent) def apply_hosts (options, files) : """